Atmega8a的采样(查询法)
2018-10-07 来源:eefocus
实现了ADC0和ADC1的切换采样,每个通道采样10次,累计满10次取一次平均,然后输出数据
========================================
在main.c中:
int main(){
//此处都是初始化的函数
//...........................
while (1)
{
ad_calc();
_delay_ms(50);
PORTD |=(1<<5);
_delay_ms(50);
PORTD &=~(1<<5);
}
}
========================================
void ad_calc()
{
switch(step)
{
case 1:
get;
step=2;
break;
case 2:
switch (ADC_sel(ch))//ch选择
{
case 0://ch=0
get;
result = ADCL;
result = result+(ADCH<<8);
if (adcnt
{
volta[adcnt++] = result;
if (adcnt==ADNUM)//采满
{
ADC_Average_Vol();
ch = ADC_sel(1);
adcnt=0;//采满的计数清零
}
}
break;//ch=0
case 1://ch=1
get;
result = ADCL;
result = result+(ADCH<<8);
if (adcnt
{
volta[adcnt++] = result;
if (adcnt==ADNUM)//采满
{
ADC_Average_Tmp();
ch = ADC_sel(0);
adcnt=0;//采满的计数清零
}
}
break; //ch=1
}
break; //step=2
}
}
========================================
uchar step=1,adcnt=0,ch=0;//AD相关
uint result=0;//存AD值
uint volta[20];//存电压
uint temper[10];//存温度
#define get while(!(ADCSRA & (1 << ADIF)))//若转化完成,ADIF=1
========================================
atmega8和16不同,16的采样可以由定时器触发