Menu

mega 328 ReadAD (ADC0)

Help
2017-02-23
2017-02-23
1 2 > >> (Page 1 of 2)
  • stan cartwright

    stan cartwright - 2017-02-23

    Hi, I can't get a-d data as in this code,just prints 0. It's copy pasted. Any advice welcome. I'm using A0 on arduino uno and don't know why the example uses dir c.0 in. Dir a.0 gives an error DRRA not explicitly defined.

    #chip mega328p,16
    #option explicit
    #include <glcd.h>
    #define HI2C_BAUD_RATE 400
    #define HI2C_DATA
    HI2CMode Master
    #define GLCD_TYPE GLCD_TYPE_SSD1306
    #define GLCD_I2C_Address 0x78
    ;
    dim volt as byte
    dir portc.0 in
    glcdcls
    start:
    volt= ReadAD (ADC0) ;read adc
    glcdprint (0,0,volt)
    wait 100 ms
    goto Start
    
     

    Last edit: Anobium 2017-02-24
  • Anobium

    Anobium - 2017-02-24

    Right. Did you look at the Help? I did.
    Did you look at the demos? Search the demos for ..\GCB@Syn\GreatCowBasic\Demos\Vendor Boards\Arduino LCD Shields\Full Demo of LCD Shield Capabilities_mega328p.gcb

    The file Full Demo of LCD Shield Capabilities_mega328p.gcb has a working and tested ADC.

        read_LCD_buttons =   btnNONE
        adc_key_in = ReadAD10(AN0)      ' read the value from the sensor
    

    So, when you change the port to AN0 things will start to work - hopefully.

    Please Help us help others by improving the Help. This may be the documentation - what can we change to ensure others do not get stuck in the future? Write a few works to be added to the Help - community knowledge is all powerful.

    Evan

     
  • stan cartwright

    stan cartwright - 2017-02-24

    I googled Full Demo of LCD Shield Capabilities_mega328p.gcb and can't find \GCB@Syn\GreatCowBasic\Demos\Vendor Boards\Arduino LCD Shields\Full Demo of LCD Shield Capabilities_mega328p.gcb
    A http:// link please. It's not a lcd problem. I copied the code from a post by joe rocci who said thanks Kent, it worked but didn't say WHAT worked.
    I changed the code to readadc10 and the display reads 65280 each power up. I'll try a 18f25k22 and see what happens. Why doesn't my code work? Using pin A0 uno board and latest GCB.

    #chip mega328p,16
    #option explicit
    #include <glcd.h>
    #define HI2C_BAUD_RATE 400
    #define HI2C_DATA
    HI2CMode Master
    #define GLCD_TYPE GLCD_TYPE_SSD1306
    #define GLCD_I2C_Address 0x78
    ;
    ;#define InternalClock 192 ;********** these were solutions from Hugh Considine
    ;#define AD_Delay 2 10us
    ;#define AD_REF_SOURCE AD_REF_AVCC
    ;#define ADSpeed LowSpeed ;*********
    dim volt as word
    dir portc.0 in
    glcdcls
    start:
    volt= ReadAD10 (An0) ;read adc
    glcdprint (0,0,volt)
    wait 100 ms
    goto Start
    
     

    Last edit: Anobium 2017-02-24
  • Anobium

    Anobium - 2017-02-24

    RE: \GCB@Syn\GreatCowBasic\Demos\Vendor Boards\Arduino LCD Shields\Full Demo of LCD Shield Capabilities_mega328p.gcb - look in your installation folders. Where the first \ is your choice of installation location

    Please post details of your circuit as this should work. I do not know but if you are getting Zero returned then something is not ok as a floating input would yield different results.

     
  • Anobium

    Anobium - 2017-02-24

    @Stan. Using your code as the basis.

    #chip mega328p,16
    #option explicit
    
    'Set up hardware serial connection
    #define USART_BAUD_RATE 9600
    #define USART_BLOCKING
    
    dim volt as byte
    dir portc.0 in
    
    hserprint "Test"
    HSerPrintCRLF
    
    
    start:
    volt= ReadAD (AN0) ;read adc
    hserprint volt
    HSerPrintCRLF
    wait 1 s
    goto Start
    

    This returns volts as for various reading - these are the correct reading as the ADC is set by 4 test switches and assiocated resistors.

    253
    102
    64
    1
    253
    
     
  • stan cartwright

    stan cartwright - 2017-02-24

    Thanks for replying. There is no wiring. The lcd works. Just a wire into A0 to pick up noise. I don't know how to use gcb terminal or windows terminal in cmd. It should work with lcd the same as terminal. I'll try a pic.

     
  • Anobium

    Anobium - 2017-02-24

    Are you saying the you do not understand the pin out of an Mega328p? The ports for a Mega328p are shown below - there is no porta.0. See http://www.learningaboutelectronics.com/images/Atmega328-pinout.png

    'Analog inputs
    'Analog 0 to 5 = AN0 to AN5 in GCBASIC
    #define ANALOG_0 PORTC.0
    #define ANALOG_1 PORTC.1
    #define ANALOG_2 PORTC.2
    #define ANALOG_3 PORTC.3
    #define ANALOG_4 PORTC.4
    #define ANALOG_5 PORTC.5
    #define RESET portc.6
    
    'Digital pins
    #define DIGITAL_0 PORTD.0
    #define DIGITAL_1 PORTD.1
    #define DIGITAL_2 PORTD.2
    #define DIGITAL_3 PORTD.3
    #define DIGITAL_4 PORTD.4
    
    #define DIGITAL_5 PORTD.5
    #define DIGITAL_6 PORTD.6
    #define DIGITAL_7 PORTD.7
    #define DIGITAL_8 PORTB.0
    
    #define DIGITAL_9 PORTB.1
    #define DIGITAL_10 PORTB.2
    #define DIGITAL_11 PORTB.3
    #define DIGITAL_12 PORTB.4
    #define DIGITAL_13 PORTB.5
    
     
  • William Roth

    William Roth - 2017-02-24

    Stan,

    There is nothing wrong with the AVR ADC Functions. They work fine. The most likely cause of your problem is incorrect or bad connections.

    The Arduio uses wacky pin numbers/names that can be confusing. For example, if you look at the 6-pin analog header on the UN0 there are inputs A0 to A5. But these have nothing to do with PORTA. These are actually connected to PORTC.0 to PortC.5.

    With Great Cow Basic, analog inputs AN0 to AN5 are mapped to the correct port.pin. AN0 correlates to "A0" as printed on the UNO R3 Board.

    I just set up an UNO R3 and it works correctly. Below is the code I used for testing and attached is a graphical diagram of the actual connections.

    ; Sample  Code
    #chip MEGA328P, 16
    
    ;Software I2C For LCD
    #define I2C_MODE Master
    #define I2C_DATA PORTC.4
    #define I2C_CLOCK PORTC.5
    #define I2C_DISABLE_INTERRUPTS ON
    
    #define I2C_Bit_Delay 2 us
    #define I2C_Clock_Delay 1 us
    #define I2C_End_Delay 1 us
    
    #define LCD_IO 10
    #define LCD_I2C_Address_1 0x7E ; default to 0x4E
    
    #define LCD_SPEED FAST
    #define LCD_Backlight_On_State  1
    #define LCD_Backlight_Off_State 0
    
    DIM TestVal as WORD  ;For ADC10
    
    CLS
    Print "Great Cow Basic"
    Locate 1,0
    PRINT "ARDUINO UNO R3"
    Wait 1 s
    
      Do
         'SHOW READAD
        TestVal = READAD(AN0)
        Locate 2,0
        Print "ADC8 =" : LCDSPACE 1
        Print TestVal : LCDSPACE 5
    
         'SHOW READAD10
        TestVal = READAD10 (AN0)
        Locate 3,0
        Print "ADC10=" :LCDSPACE 1
        Print TestVal : LCDSPACE 5
        wait 100 ms
    
    Loop
    
    END
    
     

    Last edit: William Roth 2017-02-24
  • stan cartwright

    stan cartwright - 2017-02-24

    You have gone to so much effort,thanks..to avail. Imagine you were moi. Your code William Roth. I used x loader but that's working ok. The adc should jump around from noise when not tied.
    https://youtu.be/VsU8nP8v-pU

     
  • stan cartwright

    stan cartwright - 2017-02-24

    In the vid it was the wrong file flashed but I flashed William's code and nothing on the oled because I'm using SSD1306. I don't want to download the arduino c++ sketch thing to use an example and see adc working. 2 different unos 1 smd 1 dil28 can't be us. GLCDPrint 80,32,str(ReadAD(A.0))+" " don't work.
    Yo'u'e seen the code and video so what next?

     
  • William Roth

    William Roth - 2017-02-24

    If the GLCD works then the ADC should display. I am not sure why you are trying to use a single line of code to display the ADC value. Keep it simple and break it down to several discrete lines to get it working. Then try your complex statements.

    I have no clue why you are referring to Arduino C++ or why you think you may need to download it. I also have no clue wny mentioned xloader. We do not generally use xloader.

    GCB includes AVRDUDE which uses the virutual USB com port set up by the Arduino driver. All you need to do is edit the flashAVR.bat file and then click oon hex/flash to program the UNO. If you do not have the arduino driver installed you will need to do so.

    This should be the active line in the AVRProgram.bat file. ( Change the comport as needed)

    "AVRdude\avrdude.exe" -c arduino -P COM3 -b 115200 -p AT%2 -U flash:w:%1:i

     
  • Hugh Considine

    Hugh Considine - 2017-02-25

    One suggestion, does adding this line help at all?

    #define ADSpeed LowSpeed
    

    That will slow down the conversion speed, sometimes with a clock speed of 16 MHz on an AVR the ADC doesn't have enough time to convert properly.

     
  • kent_twt4

    kent_twt4 - 2017-02-25

    Well I can appreciate Stan's problem, and frustration. No matter what I tried, I could not get the GCB Ver 0.96 ReadAD(), or the ReadAD10() functions to work. I am using a Chinese clone of the UNO R3 for hardware. Tried different ADSpeed's, different pins, nope. Even cut the Vcc Avcc trace and put in a 10uH inductor inline like the datasheet says, still no go.

    Last ditch effort went back to GCB Ver 95.008 and a miracle happened. Both ReadAD() and ReadAD10() functions work properly. Output on terminal, or GLCD_SSD1306, all good.

    So something is up between V95.008 and V96 versions, I'm just not sure what yet. The assembly for V96 seemed OK, have to compare with V95.008 and see what is missing.

     
  • Anobium

    Anobium - 2017-02-25

    I agree with Kent. Something is not correct in v0.96.0 I would not worry about finding the root cause - I have a later build ready.

    I have sent @Stan an email to pick up the latest build. I know this works as I verfied the issue as Kent describe and the fault is not present in v0.97 rc5 (still creeping to v1.0). PS I will release Tuesday after I have sorted one last piece of documention.

    @Stan. Do test and let us know the result.

     
  • Anobium

    Anobium - 2017-02-25

    Look at what we fixed ..... - Fixed AVR bug - SysReadAD variables were treated as registers incorrectly.

    I should read the release notes... as I write them!

    If Stan tests are ok. You need v0.97.00 or greater to resolve this issue.

    Anobium

     

    Last edit: Anobium 2017-02-25
  • stan cartwright

    stan cartwright - 2017-02-25

    I installed v0.97 and arduino uno rev3 readad and readad10 now work! You thought I was an idiot...so did I :). There was a post by joe rocci about arduino uno readadc in 2014 and Kent posted a link to an update but again, it must have been forgotten.Thanks for the help Evan, Are there any other fixes in V0.97?

     
    • Anobium

      Anobium - 2017-02-25

      Excellent.

      My apologies for not checking the change log. I foolishly tested on a development PC not a stock build of v0.96.00

      Re the ohter fixes: All the fixes and updates are documented in the 'release note.txt' and updates to the Help are in the Help. When you install you are shown this file. :-)

      Lots of changes - always is. Upwards and onwards.

       
  • kent_twt4

    kent_twt4 - 2017-02-25

    I forget what happened yesterday, much less in 2014, lol. Glad it is fixed. V96 a-d.h too many alias's and definetely not assembling correctly, (i.e. the already noted register problem).

     
    • Anobium

      Anobium - 2017-02-25

      The issue was in the compiler. .h was ok.

       
  • kent_twt4

    kent_twt4 - 2017-02-25

    I need to use more ??? when tossing around loose theories :-)

     
    • Anobium

      Anobium - 2017-02-25

      It was your theory that got me thinking.... I should have engaged memory first..... ☺

       
  • stan cartwright

    stan cartwright - 2017-02-25

    I tried 2 arduino unos with the same code and got different results. The 1st uno froze the display but the 2nd didn't freeze but looks differrent. please see videos and check my code for reason to freeze.
    The second uno/video is still running.
    https://youtu.be/XV0RolbMG2U
    https://youtu.be/3gYgv4Lnnrg
    #chip mega328p,16
    #option explicit
    #include <glcd.h>
    #define HI2C_BAUD_RATE 400
    #define HI2C_DATA
    HI2CMode Master
    #define GLCD_TYPE GLCD_TYPE_SSD1306
    #define GLCD_I2C_Address 0x78
    dir portc.0 in
    dim volt as byte
    dim xpos as byte
    dim buffer1 (128)
    dim buffer2 (128)
    ;
    for xpos=0 to 127
    volt = READAD (an0)
    buffer1 (xpos)=volt/4
    buffer2 (xpos)=volt/4
    next xpos
    ;
    GLCDCLS
    main:
    for xpos=0 to 127
    pset xpos,buffer1(xpos),0 ;erase old data
    pset xpos,buffer2(xpos),1 ;plot new data
    next xpos
    ;
    for xpos=0 to 127
    buffer1 (xpos)=buffer2 (xpos) ;copy new data to old data
    volt = READAD (an0)
    buffer2 (xpos)=volt/4 ;save new data
    next xpos
    ;
    goto main</glcd.h>

     

    Last edit: stan cartwright 2017-02-25
  • Anobium

    Anobium - 2017-02-25

    add

    #define ADSpeed LowSpeed
    

    or add,

    #include <UNO_mega328p.h>
    

    the first will add the second command, but, the first will provide you all the UNO port names.

     
  • Anobium

    Anobium - 2017-02-25

    I have your code hooked up to scope and signal generator - no lockups with an UNO board with a PDIP, same display etc. I am using your code.

    #define ADSpeed LowSpeed
    

    I have removed the command above on my PDIP board and it is still stable.

    Got the power supply stable? Hooked up to your PC?

     
  • stan cartwright

    stan cartwright - 2017-02-25

    define ADSpeed LowSpeed

    include <uno_mega328p.h></uno_mega328p.h>

    added and both diplays are still different. Again 2 videos to show what I mean if you don't think this thread is getting boring. There's a lot of unos out there. Details don't matter..said the African Manx cat dealer.
    https://youtu.be/3SJO1mT-pjg
    https://youtu.be/pkjuJwkbsv8
    I suppose you don't get anomalies with pics. :)

     
1 2 > >> (Page 1 of 2)

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.