Max ADC value from atmega32U4

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

The maximum ADC value that I can get from the atmega32u4 on a Yun is 1.25V. The voltage reference is AVCC and I am setting 1 << REFS0 in ADMUX. Thus I would expect the maximum to be 5V. Why is this discrepancy happening? Thanks.

 

#include <avr/io.h>
#include <stdint.h>
#include <util/delay.h>

void init_adc()
{
    ADMUX = 1 << REFS0;
    ADCSRA = 1 << ADEN | 1 << ADPS2 | 1 << ADPS1 | 1 << ADPS0;
    ADCSRB = 0;
    ADCSRA |= 1 << ADSC;
    while( ADCSRA & 1 << ADSC)
    {
    }
}

uint8_t read_adc()
{
    ADCSRA |= 1 << ADSC;
    while( ADCSRA & 1 << ADSC)
    {
    }
    return (255*ADCW/1023);
}

int main(void)
{
    init_adc();
    DDRD = 1 << DDD0;
    TCCR0A = 1 << COM0B1 | 1 << WGM00;
    TCCR0B = 1 << CS02 | 1 << CS00;
    while( 1)
    {
        OCR0B = read_adc();
        _delay_ms(1000);
    }
    return 0;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LogLog wrote:
The maximum ADC value that I can get from the atmega32u4 on a Yun is 1.25V.

Start over -- the ADC value (on any ADC?) is never volts, but rather counts.

 

Report your counts until you get what you expect; THEN start worrying about units conversion.

 

And then you will find that your calculation as written overflows, giving you your symptom.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
return (255*ADCW/1023);

If ADCW is 1023 (the max value it may hold) then what do you think the result of this is?

 

(hint: "int" for AVR is 16 bit so has a numeric range of -32768..+32767).

 

See what happens if you add "UL" to the end of that 255.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Every app's needs and requirements are different.  That said, the shown test program uses the ADC results rather coarsely, as phase-correct PWM duty.  (Why phase-correct rather than fast?  Also, you might want to consider inverted PWM if normal gives you a one-count spike when the OC register is zero.)

 

Anyway, how far off are you going to be if you make your transfer function a simple /4?  Less than 0.1%...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks both. That was very silly of me.