#include <AT89X52.H>
unsigned char code dispbitcode[]={0xfe,0xfd,0xfb,0xf7, 0xef,0xdf,0xbf,0x7f}; //放數碼管位選的值
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f,0x00};//放數碼管顯示的數字
unsigned char dispbuf[8]={10,10,10,10,10,0,0,0}; //存放ad采集過來的值(只有最后3位有用)
unsigned char dispcount;
unsigned char getdata;
unsigned int temp;
long int i;
unsigned int R1;
sbit ST=P3^0; //是否選中該芯片
sbit OE=P3^1; //所存轉換的值
sbit EOC=P3^2; //當EAC數據有效時,將轉換數據送出
sbit CLK=P3^3; //給adc0808提供時鐘
void main(void)
{
ST=0;
OE=0;
ET0=1; //啟用定時器0
ET1=1; // 啟用定時器1
EA=1; //開總中斷
TMOD=0x12;//設置定時器0和1的工作方式
TH0=216;
TL0=216; //設置定時器0的初值為216(為adc0808提供時鐘)
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;//設置定時器1的初值為5ms
TR1=1; //開定時器1
TR0=1; //開定時器0
ST=1;
ST=0;
while(1)
{
if(EOC==1) //判斷裝換是否結束
{
OE=1;
getdata=P1; //如果結束,給OE一個跳變,讀出數據存入getdata
OE=0;
i=getdata*196; //256/getdata=5V/xV >>>x=(getdata*5/256)V
dispbuf[5]=i/10000; //將數據存入數組中以待顯示。 (個位)
i=i%10000;
dispbuf[6]=i/1000; //小數點第一位
i=i%1000;
dispbuf[7]=i/100; //小數點第二位
ST=1;
ST=0;
}
}
}
void t0(void) interrupt 1 using 0 //定時器0 中斷服務
{
CLK=~CLK; //給adc0808提供時鐘
}
void t1(void) interrupt 3 using 0 //定時器1 中斷服務
{
TH1=(65536-5050)/256;
TL1=(65536-5050)%256;
P2=0xff;
P0=dispcode[dispbuf[dispcount]]; //段選
P2=dispbitcode[dispcount]; //位選
if(dispcount==5) // 顯示小數點
{
P0=P0 | 0x80;
}
dispcount++; //讓位選循環(huán)起來
if(dispcount==8)
{
dispcount=0;
}
}
|