題目要求是 利用ADC0809設計一個簡易數字電壓表,要求可以測量0~5V之間8路輸入電壓 值,電壓值由4位LED數碼管顯示,并在數碼管上輪流顯示或單路選擇顯示; 2. 測量最小分辨率為0.019V,測量誤差約為±0.02V。
#include<reg52.h>
unsigned char code
dispbitcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char dispbuf[4];
unsighed ;char getdata;
unsigned int temp;
sbit ST=P3^0;
sbit OE=P3^1;
sbit EOC=P3^2;
sbit CLK=P3^3;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sbit P17=P1^7;
sbit bb=P3^7;
//unsigned char o;
void timeinitial();
void delay(unsigned int i);
void timeinitial()
{
TMOD=0x11;
TH1=(65536-200)/256;
TL1=(65536-200)%256;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
EA=1;
ET1=1;
ET0=1;
TR1=1;
}
void Delay(unsigned int i)
{
unsigned int j,k;
for(k=0;k<i;k++)
for(j=0;j<125;j++);
}
void Display()
{
P1=dispbitcode[dispbuf[3]];
P20=0;
P21=1;
P22=1;
P23=1;
Delay(10);
P1=0x00;
P1=dispbitcode[dispbuf[2]];
P17=1;
P20=1;
P21=0;
P22=1;
P23=1;
Delay(10);
P1=0x00;
P1=dispbitcode[dispbuf[1]];
P20=1;
P21=1;
P22=0;
P23=1;
Delay(10);
P1=0x00;
P1=dispbitcode[dispbuf[0]];
P20=1;
P21=1;
P22=1;
P23=0;
Delay(10);
P1=0x00;
}
void main()
{
timeinitial ();
{
ST=0;
OE=0;
ST=1;
ST=0;
while(EOC==0);
OE=1;
getdata=P0;
OE=0;
if(getdata>=255)
{
TR0=1;
}
else
TR0=0;
temp=getdata*1.0/255*500;
dispbuf[0]=temp%10;
dispbuf[1]=temp/10%10;
dispbuf[2]=temp/100;
dispbuf[3]=temp/1000;
Display();
}
}
void tl(void)interrupt 3 using 0
{
TH1=(65536-200)/256;
TL1=(65536-200)%256;
CLK=~CLK;
}
void t0(void)interrupt 1 using 0
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
bb=~bb;
}
|