|
发表于 2005-11-29 13:40:44
|
显示全部楼层
void adc_init(unsigned char adc_channel)
{
ADCSRA = 0x00;
ACSR = 0x80;
ADMUX = (0<<REFS1)|(1<<REFS0)|adc_channel;
ADCSRA = (1<<ADEN)|(0<<ADSC)|(0<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
unsigned int GetAdcValue(unsigned char adc_channel)
{
unsigned int adc_value=0,temp=0;
unsigned char i=0;
adc_init(adc_channel);
delay_nms(30);
for(i=0;i<2;i++)
{
ADCSRA|=(1<<ADSC);
CLI();
while((ADCSRA & 0x10)==0);
SEI();
ADCSRA |= (1<<ADIF);
temp = ADCL;
temp |= (int)ADCH<<8;
}
for(i=0;i<5;i++)
{
ADCSRA|=(1<<ADSC);
CLI();
while((ADCSRA & 0x10)==0);
SEI();
ADCSRA |= (1<<ADIF);
temp = ADCL;
temp |= (int)ADCH<<8;
adc_value = adc_value + temp;
}
adc_value = adc_value/5 ;
return adc_value;
} |
|