ADC控制与状态寄存器A ADCSRA 程序中其取值为0xE6(11100110),其最高位ADEN置位启动ADC,ADSC置位开始转换,ADATE置位启动ADC自动触发功能。ADCSRA的低3位ADPS[2:0]设置为110,分频比设为64。
Proteus运行截图:
Studio6.2编译通过截图:
程序清单:
/*
* GccApplication11.c
*
* Created: 2014-11-6 18:47:24
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <stdint.h>
const uint8_t SEG_CODE[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF};
uint8_t Display_Buffer[] = {0,0,0,10,0,0,0,10};
void ADC_Convert(uint8_t CH)
{
int Result;
ADMUX = CH;
Result = (int)((ADCL+(ADCH<<8))*500.0/1023.0);
Display_Buffer[CH*4] = Result/100;
Display_Buffer[CH*4 +1] = Result/10 % 10;
Display_Buffer[CH*4 +2] = Result % 10;
}
int main(void)
{
uint8_t i;
DDRA = 0xFC;
DDRC = 0xFF;PORTC = 0x00;
DDRD = 0xFF;PORTD = 0x00;
ADCSRA = 0xE6;
_delay_ms(3000);
while(1)
{
ADC_Convert(0); ADC_Convert(1);
for(i=0;i<8;i++)
{
PORTC = 0xFF;
PORTD = _BV(i);
PORTC = SEG_CODE[Display_Buffer[i]];
if(i==0||i==4) PORTC &= 0x7F;
_delay_ms(4);
}
}
}
|