Menu

How to select reference source for ADC on ATmega328 ?

Help
2015-07-11
2015-07-13
  • Friderik Back

    Friderik Back - 2015-07-11

    Hello!

    This is my first post on this forum and I have probably an very beginner question:

    I wonder how it is possible in GCBasic program on ATMega328p switch between 5 V Vcc and 1.1 V internal reference voltage for the ADC?

    I'm writing a program in GCB@Syn (last release) for the Arduino UNO, which would use keyboard shield (it requires 5 V for ADC) and LM35 temperature sensors (requires 1.1 V).

    I appreciate any help that you can provide.

    Best regards from Slovenia.

     
  • kent_twt4

    kent_twt4 - 2015-07-11

    The ATmega 328P data sheet says you need to set the ADMUX register bits REFS0 and REFS1 for the internal 1.1V reference. So you can try and see if setting those bits, prior to reading the LM35 channel, survives the adc.h manipulations? You would of course clear those bits right after the read to get back to the 5V reference.

    Set ADMUX.REFS0 On
    Set ADMUX.REFS1 On
    LM75temp = READAD(whatever pin)
    Set ADMUX.REFS0 On
    Set ADMUX.REFS1 Off
    

    EDIT: My bad, I think you need to leave REFS0 bit on for AVcc (no external ref).

     

    Last edit: kent_twt4 2015-07-11
  • Friderik Back

    Friderik Back - 2015-07-12

    Thank you very much for your reply.

    Arduino board is yet to come, this will be my first project with AVR and I was not sure whether it is enough just to change:

    Set ADMUX.REFS1 On 'For 1.1 V reference source
    

    or

    Set ADMUX.REFS1 Off 'For 5 V reference source
    

    In this manner written my program (previously was written for the PIC 16F88 and operational amplifier for magnifying sensor voltage) I tested on demo version of Oshon AVR Simulator IDE. It worked everything, except change the ADC voltage source. So I did not know whether that limit / defect in the simulator or I did something wrong in my program (needs more parameters, a different form of set command, etc). Because of this I also put this question.

    Limit 30 starts of the demo version of simulator, I'm exceeded yesterday, so now I have to wait for the real Arduino hardware and determine how the program will work on it.
    .

    Thanks again.

     

    Last edit: Friderik Back 2015-07-12
  • kent_twt4

    kent_twt4 - 2015-07-12

    @Frederik
    Please note that the ADMUX register initial, or reset value, is b'00000000', so still need to set the REFS0 bit.

     
  • Hugh Considine

    Hugh Considine - 2015-07-12

    GCBASIC will overwrite anything that you put into te ADMUX register - but there is an option that lets you change the ADC reference source on AVR. You can set the AD_REF_SOURCE constant to whatever you want to use. It defaults to the VCC pin, but you can tell it to use the 1.1V reference with this:

    #define AD_REF_SOURCE AD_REF_256
    

    (256 refers to the 2.56V reference on some older AVRs, but the same code will select the 1.1V reference on an ATmega328p.)

    You can also use some tricks to switch this dynamically. Here's an example of some code that will does this:

    #define AD_REF_SOURCE ADRefSource
    #define AD_VREF_DELAY 5 ms
    
    AdRefSource = AD_REF_AVCC
    HSerPrint ReadAD10(AN1)
    HSerPrint ", "
    AdRefSource = AD_REF_256
    HSerPrint ReadAD10(AN1)
    

    Here, we set the AD_REF_SOURCE to a variable, and then change the value of the variable to select the source. With this approach, we also need to allow time to charge the reference capacitor to the correct voltage. To use this second bit of code, you'll need to get an updated copy of the a-d.h file from https://svn.code.sf.net/p/gcbasic/code/GCBASIC/trunk/include/lowlevel/a-d.h

    Hope this helps!

     
  • Friderik Back

    Friderik Back - 2015-07-13

    Thank you, I really did not expect so many responses.

    @Evan

    Yes, I know this problem. The consequence of stupid saver. Look schematic around Q1:

    Keypad version 1.0:
    http://www.dfrobot.com/image/data/DFR0009/LCDKeypad%20Shield%20V1.0%20SCH.pdf

    Keypad version 1.1:
    http://www.dfrobot.com/wiki/images/a/a7/LCDKeypad_Shield_SCH.png

    source:
    http://www.dfrobot.com/wiki/index.php?title=Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)

    Yes, the manufacturer has spared an single ultra low-cost resistor. Unfortunately, in this way, many programs, that use LCD backlight, become inappropriate. Because overloading  the mcu and its power source (and Q1).

    My solution of this problem is a simple software trick (PWM does not work!):

    #include <duemilanove.h>
    
    'LCD backlight ON
    DIR Digital_10 IN 'pin is in high impedance mode -  default mode on AVR
    
    ....
    
    'LCD backlight Off
    DIR Digital_10 OUT
    Digital_10 SET OFF 'Pin in low state and Q1 will be turn off
    

    By the way, diode for the diode mod it should be an Schottky diode (BAT85 or similar). With silicon diodes can not be guaranteed that the backlight is really completely turned off.

    @Hugh

    Your solution is much better and I have already joined in the my program. Thank you.

    Now I just wait to get a Arduino board and shield. Then I can test my program and give you the results. I would like to made a digital differential thermostat.

    I hope you understand my bad English.

    Best regards

     

    Last edit: Friderik Back 2015-07-13

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.