利用AVR單片機制作電壓表
下面是部分源程序:
uint mega16_ad()
{
uint addata;
DDRD|=BIT(4)|BIT(5)|BIT(6);
DDRA&=~BIT(PA3);
PORTA&=~BIT(PA3);
ADMUX=0x03;
ADCSR=0X80;//轉換使能
ADCSR|=BIT(ADSC);//開始轉換
while(!(ADCSR&(BIT(ADIF))));//等待轉換結束.
addata=ADC*4.8876; //轉換為四位5V電壓數(shù)
return addata;
}
void main()
{
uint ada;
while(1)
{
ada=mega16_ad();
if(ada>2500)
PORTD&=~BIT(2);//adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=cec722f12761e0de&k=%B5%E7%D1%B9&k0=%B5%E7%D1%B9&kdi0=0&luki=2&n=10&p=baidu&q=98059059_cpr&rb=0&rs=1&seller_id=1&sid=dee06127f122c7ce&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1831118&u=http%3A%2F%2Fwww%2E51hei%2Ecom%2Fmcu%2F2096%2Ehtml&urlid=0" id="0_nwl" mpid="0" target="_blank">電壓值大于2.5V時LED1亮
else
PORTD|=BIT(2); //否則LED1滅
show(4,ada%10);
delay(1);
ada=ada/10;
show(3,ada%10);
delay(1);
ada=ada/10;
show(2,ada%10);
delay(1);
ada=ada/10;
showdian(1,ada%10);
}
}
評論