Menu

A to d not working as expected

Help
2020-08-01
2020-08-08
  • stan cartwright

    stan cartwright - 2020-08-01

    I find readad not giving 0 to 255 or my code is wrong.
    If I use a number instead of readad then I get the pulse width I want but the valve only goes down to 10ms pulses. If I give the valve_pot_val =5 then it gives 5ms pulses.

    #chip mega328p,16
    #option explicit
    #define valve portd.6
    #define strobe portd.7
    #define drop_size_pot portc.0
    #define strobe_pot portc.1
    #define ADSpeed LowSpeed
    dir valve out
    dir strobe out
    dir drop_size_pot in
    dir strobe_pot in
    dim valve_pot_val , mills_counter , strobe_pot_val as byte
    ;-------------------------------
    ;start 1ms interrupt isr
        On Interrupt Timer0Match1 Call valve_isr
        Dim OCR0  AS byte alias OCR0A
        Dim TCCR0 AS  byte alias TCCR0B
        WGM01 = 1
        OCR0 = 249 ;1ms
        TCCR0 = 0x28
        TCCR0 = TCCR0 or 0x03
    '--------------
    do
    ;read strobe pot
      strobe_pot_val = readad (strobe_pot)
      wait 17300 us
      wait strobe_pot_val 10us
      pulseout strobe,1 ms
    ;read valve pot
    ;  valve_pot_val = scale(readad (drop_size_pot, true),0,255,1,19)
    valve_pot_val =  readad (drop_size_pot) / 14+1
    loop
    ;
    Sub valve_isr ;called every mS
      mills_counter ++
      if mills_counter = 20 then
        mills_counter = 0
        set valve on
      else if mills_counter = valve_pot_val then
        set valve off
      end if
    End Sub
    

    ps...what has happened to the coding and forum editor....it erases next char as you write

     
  • stan cartwright

    stan cartwright - 2020-08-01

    in this I changed the valve_pot_val to 2
    valve_pot_val = 2 ;readad (drop_size_pot) / 14 + 1
    and I get the desired value but readad is not working as I expected
    is it an arduino issue?
    any help appreciated,cheers

     
    • Anobium

      Anobium - 2020-08-02

      It is a case of the read the manual.

      '#define strobe_pot portc.1' is a constant the sets strobe_pot to portc.1
      and, then, you use the constant to read the ADC strobe_pot_val = readad (strobe_pot)

      So, in the Help. For a normal ( also called a Single Channel ) read use.

      byte_variable = ReadAD( ANX )

      Where ANXis constant and the ANX is the constant the points to the ADC you want to read.

      So, strobe_pot_val = readad (strobe_pot) will fail the value of the constant that refers to the port will be 0 or 1.

      And, where strobe_pot_val = readad (AN1) will work as AN1 is PC1 (ADC1/PCINT9) as shown on the datasheet page 3 - http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf

      and, the same applies to the READADC( drop_size_pot ) ....

       

      Last edit: Anobium 2020-08-02
  • stan cartwright

    stan cartwright - 2020-08-02

    YES , you are correct...my mistake.I did read the help but ANX I thought refered to an a- to-d port.
    Changed this line...didn't even need scale funtion and now sweeps 1 to 19 ms pulse every 20 ms
    valve_pot_val = readad (AN0) / 14 + 1
    Thank you for you time helping me...I know how busy you must be. It is appreciated.

     
  • stan cartwright

    stan cartwright - 2020-08-02

    ps. with all respect,data sheets are too complicated. I thought the idea of basic was to dumb down the technical under the hood stuff as much as possible or else why use it?
    I wish I could look at a devices data sheet and write a lib for it.

     
  • stan cartwright

    stan cartwright - 2020-08-06

    So how to define AN0 as portc.0

    #define drop_size_pot AN0
    

    does not assemble.
    Why in my code did I get varying pot values? Not 0 to 255 but more like half the pot like 0 to 127
    but still reading something. I should have sent the values to the terminal to see what I was getting.

     
  • Anobium

    Anobium - 2020-08-06

    We need more code. How are you using the constant? What is the error?

     
  • stan cartwright

    stan cartwright - 2020-08-07
    5  #define drop_size_pot AN0 ;portc.0
    10  dir drop_size_pot in
    

    Source-File = C:\Users\stanley555\Documents\GCBasic stuff\Arduino\addefinetest.gcb

    WARNINGs / ERRORs reported by Great Cow BASIC (if Syntax Error, doubleclick on the errormessage below) <<<
    addefinetest.gcb (10): Error: 0 is not a valid I/O pin or port

     
  • Anobium

    Anobium - 2020-08-07

    Updated for you.

    Where ANX is constant and the ANX is the constant the points to the ADC you want to read within the context of the READADC, READADC10 or READADC12 commands.

    You should not use ANX outside of the context stated above.

    If you use DIR ANx IN|OUT you will get an error. Use DIR PORTn.n IN|OUT

     
    • stan cartwright

      stan cartwright - 2020-08-07

      thanks,cleared things up a bit.
      Still don't know the ad values I got from my first post but they varied.
      What were they if the defines were all messed up..a bit?
      I thought this was some 328 error.
      What was it doing when I readad portc.0 defined as valve_pot and got half range values...
      or is it just don't do it that way? :)

       
  • stan cartwright

    stan cartwright - 2020-08-07

    If I change line 10 to dir portc.0 in then I can use valve_pot_val = readad (drop_size_pot) / 14+1...
    well it assembles ok.
    It scopes ok with full 0 to 255 pot sweep.
    This works but why the define didn't work for the port dir ??

    #chip mega328p,16
    #option explicit
    #define valve portd.6
    #define strobe portd.7
    #define drop_size_pot AN0 ;portc.0
    #define strobe_pot AN1 ;portc.1
    #define ADSpeed LowSpeed
    dir valve out
    dir strobe out
    dir portc.0 in ;drop_size_pot in
    dir portc.1 in ;strobe_pot in
    dim valve_pot_val , mills_counter , strobe_pot_val as byte
    ;-------------------------------
    ;start 1ms interrupt isr
        On Interrupt Timer0Match1 Call valve_isr
        Dim OCR0  AS byte alias OCR0A
        Dim TCCR0 AS  byte alias TCCR0B
        WGM01 = 1
        OCR0 = 249 ;1ms
        TCCR0 = 0x28
        TCCR0 = TCCR0 or 0x03
    '--------------
    do
    ;read strobe pot
      strobe_pot_val = readad (strobe_pot)
      wait 17300 us
      wait strobe_pot_val 10us
      pulseout strobe,1 ms
    ;read valve pot
    ;  valve_pot_val = scale(readad (drop_size_pot, true),0,255,1,19)
    valve_pot_val =  readad (drop_size_pot) / 14+1
    loop
    ;
    Sub valve_isr ;called every mS
      mills_counter ++
      if mills_counter = 20 then
        mills_counter = 0
        set valve on
      else if mills_counter = valve_pot_val then
        set valve off
      end if
    End Sub
    
     
  • stan cartwright

    stan cartwright - 2020-08-07

    Couldn't the help just say
    If using 328 then readad port is C.
    portc.0 is AN0
    portc.1 is AN1
    portc.2 is AN2
    Always use readad (AN0)
    Do not use readad (portc.0)
    Cos it looks like ANX is not a keyword but a representation of the specific port...IMHO

     
    • Anobium

      Anobium - 2020-08-08

      Not really. We would have to do the same for about 1000 other chips.

       
  • stan cartwright

    stan cartwright - 2020-08-07

    Cos I'm using readad in a timed loop then can I ask if readad always take the same time to execute?
    If not then should it be interrupt driven?...which I am interested in at the moment, cos they seem to solve many problems.... and as Chris Roper said..needs discussing.

     
    • Anobium

      Anobium - 2020-08-08

      Yes same time to execute.

      You can use an timed interrupt to call the readad methods.

       
      • stan cartwright

        stan cartwright - 2020-08-08

        Thanks,handy to know.
        Assume the help is ok and I misunderstood.
        no one will make the mistakes I made on wrong assumptions..hopefully.
        cheers.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.