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

AT89s51-ADC0804實(shí)驗(yàn)

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

我實(shí)在沒(méi)有興趣讀微積分了。就接著上次弄的繼續(xù)做。最近晚上天天失眠,所以就換種方式生活,宅男加夜貓。
 

/*****************************************
2011年5月23日
ADC0804,AT89S51
連接:
數(shù)碼管:
  P0數(shù)據(jù),段選P2^7,位選P2^6.
ADC0804:
  數(shù)據(jù)輸出P1
  WR=P3^6
  RD=P3^7
  INTR懸空
  CS接GND 
現(xiàn)象:擰動(dòng)滑動(dòng)變阻器 數(shù)碼管示數(shù)在0--255之間
*****************************************/

#include <reg51.h>   
#include <intrins.h>

sbit duanxuan=P2^7;
sbit weixuan=P2^6; 
sbit AD_WR=P3^6;  //定義ADC0804的WR端口
sbit AD_RD=P3^7;  //定義ADC0804的RD端口

unsigned char shu[8];
unsigned char code duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                            0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

unsigned char code weima[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

/****************************************
              延時(shí)函數(shù)
****************************************/
void delayms(unsigned int xms)    
{
   unsigned char i,j;
   for(i=xms;i>0;i--) 
     for(j=120;j>0;j--);
}

/****************************************
             顯示函數(shù)
****************************************/
void display()
{
 unsigned char i;
 for(i=0;i<3;i++)
 {
  P0=0;
  duanxuan=1;
  duanxuan=0;
  
  P0=weima[i];
  weixuan=1;
  weixuan=0;
  
  P0=shu[i];
  duanxuan=1;
  duanxuan=0;
  
  delayms(4);
 } 
}

/****************************************
                 MAIN
****************************************/
void main() 
{
  unsigned char adval;
  unsigned int a ;
    
  while(1)
 {
     AD_WR=1;
     _nop_();
     AD_WR=0;//啟動(dòng)AD轉(zhuǎn)換
     _nop_();
     AD_WR=1;

     for(a=10;a>0;a--)  //啟動(dòng)轉(zhuǎn)換后要多留點(diǎn)時(shí)間用來(lái)轉(zhuǎn)換
       { display();}
          
     P1=0xff;  //讀取P1口之前先給其寫全1 
 
     AD_RD=1;  
     _nop_(); 
     AD_RD=0;//AD讀使能
     _nop_();

     adval=P1;  //AD數(shù)據(jù)讀取賦給adval
     AD_RD=1;


     shu[0]=duanma[adval/100];
     shu[1]=duanma[adval%100/10];
     shu[2]=duanma[adval%10];
     display();

     for(a=100;a>0;a--)
     display();
    }
}


關(guān)閉窗口

相關(guān)文章