代码:
#include<reg51.h>
#define uchar unsigned char
sbit cjclk=P2^4;
sbit EOC=P3^2;
sbit key=P2^5;
uchar tab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar tabd[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uchar xsjs;
uchar num0,num1,num2,num3;
uchar cjsh;
uchar xdata *p;
int beichu;
void delay5ms(void)
{
uchar a,b;
for(b=19;b>0;b--)
for(a=130;a>0;a--);
}
void init_t()
{
TMOD=0x21;
TH0=0xf8;
TL0=0X30;
TH1=0xff;
TL1=0xff;
EA=1;
ET0=1;
ET1=1;
TR1=1;
TR0=1;
}
void int_t1()interrupt 3
{
bit a;
a=cjclk;
cjclk=~a;
}
void int_t0()interrupt 1
{
TH0=0xf8;
TL0=0x30;
switch(xsjs)
{
case 0:{P1=0xff;P2=0xfe;P1=tabd[num3];break;}
case 1:{P1=0xff;P2=0xfd;P1=tab[num2];break;}
case 2:{P1=0xff;P2=0xfb;P1=tab[num1];break;}
case 3:{P1=0xff;P2=0xf7;P1=tab[num0];break;}
}
xsjs++;
xsjs%=4;
}
void main()
{
beichu=0;
init_t();
xsjs=0;
p=0x7fff;
while(1)
{
key=1;
if(!key)
{
delay5ms();
if(!key)
{
*p=0xff;
while(EOC);
cjsh=*p;
beichu=(int)cjsh*5;
num3=beichu/ 0xff;
beichu=beichu%0xff * 10;
num2=beichu / 0xff;
beichu=beichu%0xff * 10;
num1=beichu /0xff;
beichu=beichu%0xff * 10;
num0=beichu/0xff;
}
}
}
}
效果:
点击按键开始测量电压,然后添加电压探针,通过ADC0808模块开始探测,数字大致一致就可以。
?
|