專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

利用AVR單片機(jī)制作電壓表

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2013年10月19日   【字體:

    通過研究和實(shí)驗(yàn),用mega16可以制作5V電壓。電壓值從LED顯示出來。

下面是部分源程序:
uint mega16_ad()
{
     uint addata;
  DDRD|=BIT(4)|BIT(5)|BIT(6); 
  DDRA&=~BIT(PA3);
  PORTA&=~BIT(PA3);
  ADMUX=0x03;
  ADCSR=0X80;//轉(zhuǎn)換使能
  ADCSR|=BIT(ADSC);//開始轉(zhuǎn)換
  while(!(ADCSR&(BIT(ADIF))));//等待轉(zhuǎn)換結(jié)束.
  addata=ADC*4.8876;    //轉(zhuǎn)換為四位5V電壓數(shù)
  return addata;
}  

    
void main()
{  
     uint ada; 
  while(1)
  {
      ada=mega16_ad();
       if(ada>2500)
   PORTD&=~BIT(2);//電壓值大于2.5V時(shí)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);  
  }

}

      2009年3月25日

關(guān)閉窗口

相關(guān)文章