3

I'm tinkering around with an ATMega328P right now and wanted to read an analogue value from a pin through the ADC and simply output the value to 4 LEDs. Really simple

#define F_CPU 20000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define BRIGHTNESS_PIN 2
#define ADC_SAMPLES 5

void init_adc()
{
    //set ADC VRef to AVCC
    ADMUX |= (1 << REFS0);
    //enable ADC and set pre-scaler to 128
    ADCSRA = (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2) | (1 << ADEN);
}

uint16_t read_adc(unsigned char channel)
{       
    //clear lower 4 bits of ADMUX and select ADC channel according to argument
    ADMUX &= (0xF0);
    ADMUX |= (channel & 0x0F); //set channel, limit channel selection to lower 4 bits

    //start ADC conversion
    ADCSRA |= (1 << ADSC);

    //wait for conversion to finish
    while(!(ADCSRA & (1 << ADIF)));

    ADCSRA |= (1 << ADIF); //reset as required      

    return ADC;
}

int main(void)
{
    uint32_t brightness_total;
    uint16_t brightness = 0;
    uint32_t i = 0;

    init_adc();
    sei();
    while (1) 
    {
        //reset LED pins
        PORTB &= ~(1 << PINB0);
        PORTD &= ~(1 << PIND7);
        PORTD &= ~(1 << PIND6);
        PORTD &= ~(1 << PIND5);

        PORTB |= (1 << PINB1); //just blink

        read_adc(BRIGHTNESS_PIN); //first throw-away read
        //read n sample values from the ADC and average them out
        brightness_total = 0;
        for(i = 0; i < ADC_SAMPLES; ++i)
        {
            brightness_total += read_adc(BRIGHTNESS_PIN);   
        }
        brightness = brightness_total / ADC_SAMPLES;

        //set pins for LEDs depending on read value.
        if(brightness > 768)
        {
            PORTB |= (1 << PINB0);
            PORTD |= (1 << PIND7);
            PORTD |= (1 << PIND6);
            PORTD |= (1 << PIND5);
        }
        else if (brightness <= 768 && brightness > 512)
        {
            PORTB |= (1 << PINB0);
            PORTD |= (1 << PIND7);
            PORTD |= (1 << PIND6);
        }
        else if (brightness <= 512 && brightness > 256)
        {
            PORTB |= (1 << PINB0);
            PORTD |= (1 << PIND7);
        }
        else if (brightness <= 256 && brightness >=64)
        {
            PORTB |= (1 << PINB0);
        }

        _delay_ms(500);
        PORTB &= ~(1 << PINB1); //just blink
        _delay_ms(500);
    }
}

This works kind of fine, except the channel selection. When I select a channel it works fine, but independently from the selected channel, channel 0 also always reads and converts. What I mean with that is that if I plug the cable into the selected channel pin, it reads the values correctly. When I plug it into any other channel pin it obviously doesn't, except for ADC0. No matter what channel I set, not only does that one read but also ADC0.

Why is that and how do I fix that?

I already checked my PCB for solder bridges, but there are none and I would also expect some slightly different behaviour with that.

Also ADC4 and ADC5 don't seem to properly convert either. Any idea why that is? The only clue I found in the datasheet is, that those two use digital power, while all the other ADCs use analogue. What's the difference, why does it matter and why does it not correctly convert my anlogue signal?

Both ARef and AVCC are connected according to the datasheet, with the exception that the inductor for ARef is missing.

2 Answers 2

4

I think what is happening is that

ADMUX &= (0xF0);

is setting the channel to 0, and

ADMUX |= (channel & 0x0F);

is then setting the channel to the one you want. You're then taking a reading and throwing the result away, which should mean that the initial channel being set to 0 doesn't matter.

Howevever, when you then try to take an actual reading, you are setting the channel again, by using read_adc to take the reading. So, you don't ever throw a reading away.

What I would do is replace your ADMUX setting commands with:

ADMUX = (0xF0) | (channel & 0x0F)

Then move this into a separate function called something like set_adc_channel(int channel). Include a throw away read in that function, then remove the ADMUX setting from your read_adc function. Just start a conversion and get the result.

Also note that since you only ever use one channel, you could move the channel setting part to init_adc(). I assume it's in a separate function so you could later read more than one channel.

I hope that's clear. Let me know if not.

2
  • ah, that worked! Thanks! Mino correction though, the line need to be this ADMUX |= (ADMUX & 0xF0) | (channel & 0x0F); otherwise the ANDing will prevent the proper bits to be set.
    – rfreytag
    Sep 4, 2016 at 21:01
  • 1
    Correct, you caught my typo. Glad to have helped.
    – js441
    Sep 4, 2016 at 21:02
0

EDIT: So as you stated, ADIF is really reset by writing logic 1. I've just tested your adc_read function and it is working for me (if you don't mind Arduino mixture)

uint16_t read_adc(unsigned char channel)
{       
    //clear lower 4 bits of ADMUX and select ADC channel according to argument
    ADMUX &= (0xF0);
    ADMUX |= (channel & 0x0F); //set channel, limit channel selection to lower 4 bits

    //start ADC conversion
    ADCSRA |= (1 << ADSC);

    //wait for conversion to finish
    while(!(ADCSRA & (1 << ADIF)));

    ADCSRA |= (1 << ADIF); //reset as required      

    return ADC;
}

void setup() {
  Serial.begin(57600);
  //set ADC VRef to AVCC
  ADMUX |= (1 << REFS0);
  //enable ADC and set pre-scaler to 128
  ADCSRA = (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2) | (1 << ADEN);

  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  pinMode(A3, INPUT_PULLUP);
}

void loop() {
  Serial.println(read_adc(0));
  Serial.println(read_adc(1)); 
  Serial.println(read_adc(2)); 
  Serial.println(read_adc(3)); 

  delay(1000);
}

I just connect one of these channels to 3.3V pin and it'll read 713 on it. Other channels are pulled up to levels about 1017.

1
  • From the datasheet: "Alternatively, ADIF is cleared by writing a logical one to the flag."
    – rfreytag
    Sep 4, 2016 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.