如何用查询方式读取ADC的数据?
小弟想不用中断方式来读取ADC的值,请问可否提供一个用查询的方式读取ADC的程序例子? 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;
} ADCSRA|=_BV(ADSC); //启动ad
while(!(ADCSRA&_BV(ADIF)));
ret=ADCL;
ret|=(uint)(ADCH<<8);
ad=ret; //ad数组放8次ad转换的结果
ADCSRA|=1<<ADIF;//清零标志 真是很感激以上两位啊!先试试了 请教上面的CLI();和SEI();指的是什么;
请兄弟指点;谢谢! 回复【4楼】rainlin
-----------------------------------------------------------------------
cli()关全局中断...
sei ()开全局... mark一下,最近在学这个 mark 。正在研究ADC ,谢谢哦 路过,学习一下 必须要mark一下~~
页:
[1]