#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;
}
上面的代码应从 PA0 获取模拟值(使用电位器)并在连接到 PORT C 的 8 个 LED 上显示数字值。当我在 Proteus 上模拟电路时,即使我更改电位器值,LED 也始终亮着,并且有一条消息说“[AVR AD 转换器]参考值 = 0”。
感谢您能帮助我了解问题所在。