#define F_CPU 1000000UL
#include <avr/io.h>
volatile uint8_t adcvalue;
int main(void)
{
DDRA =0x00;
DDRC = 0xff;
// enable adc
ADCSRA |= (1<<ADEN);
// using division factor 8
ADCSRA |= (1<<ADPS0) | (1<<ADPS1);
// enable 8 bit conversion
ADMUX |= (1<<ADLAR);
// take input from PA0
ADMUX |= (1<<MUX0);
while (1)
{
// Start conversion
ADCSRA |= (1<<ADSC);
// wait until conversion is done
while (ADCSRA & (1<<ADSC));
// save result to adcvalue
adcvalue = ADCH;
// show result on 8 leds connected to PORT C
PORTC = adcvalue;
}
return 0;
}
The above code shall take analog value from PA0 (using potentiometer) and show digital value on 8 leds connected to PORT C. When I simulate the circuit on Proteus the leds are always on even when I change the potentiometer value and there is a message saying "[AVR AD CONVERTER]Reference Value = 0".
Appreciate if you can help me know what is wrong.
Thats wrong, with that line you take PA1/ADC1 as input, not PA0/ADC0
Check out the Datasheet at Page 218: http://www.atmel.com/images/doc2466.pdf
MUX 4..0 00000 is PA0 and 00001 is PA1
You set the value of MUX 4..0 to 00001 with
and therefore using PA1 as Source.
Cheers