我實在沒有興趣讀微積分了。就接著上次弄的繼續做。最近晚上天天失眠,所以就換種方式生活,宅男加夜貓。
/***************************************** 2011年5月23日 ADC0804,AT89S51 連接: 數碼管: P0數據,段選P2^7,位選P2^6. ADC0804: 數據輸出P1 WR=P3^6 RD=P3^7 INTR懸空 CS接GND 現象:擰動滑動變阻器 數碼管示數在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}; /**************************************** 延時函數 ****************************************/ void delayms(unsigned int xms) { unsigned char i,j; for(i=xms;i>0;i--) for(j=120;j>0;j--); } /**************************************** 顯示函數 ****************************************/ 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;//啟動AD轉換 _nop_(); AD_WR=1; for(a=10;a>0;a--) //啟動轉換后要多留點時間用來轉換 { display();} P1=0xff; //讀取P1口之前先給其寫全1 AD_RD=1; _nop_(); AD_RD=0;//AD讀使能 _nop_(); adval=P1; //AD數據讀取賦給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(); } }