/*****************************************ADC0804,AT89S51連接:數(shù)碼管:P0數(shù)據(jù),段選P2^7,位選P2^6.ADC0804:數(shù)據(jù)輸出P1WR=P3^6RD=P3^7INTR懸空CS接GND 現(xiàn)象:擰動滑動變阻器 數(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ù)****************************************/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;i3;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;//啟動AD轉(zhuǎn)換_nop_();AD_WR=1;for(a=10;a>0;a--) //啟動轉(zhuǎn)換后要多留點時間用來轉(zhuǎn)換{ display();}P1=0xff; //讀取P1口之前先給其寫全1 AD_RD=1; _nop_(); AD_RD=0;//AD讀使能_nop_();adval=P1; //AD數(shù)據(jù)讀取賦給advalAD_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();}}
評論