#include<reg52.h>
sbit cnt=P3^4;
sbit rC=P2^4;
sbit rB=P2^3;
sbit rA=P2^2;
unsigned char duan[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
unsigned int num=0;
void init()//¼ÆÊýÆ÷³õʼ»¯
{
TMOD=0x05;
TH0=0;
TL0=0;
TR0=1;
}
void delay()
{
unsigned char i,j;
for(i=0;i<4;++i)
{
for(j=0;j<110;++j);
}
}
void display(unsigned int shi,unsigned int ge)
{
rC=rB=rA=0;
P0=duan[shi];
delay();
rC=rB=0;
rA=1;
P0=duan[ge];
delay();
}
unsigned int read()//¶Á¼ÆÊýÆ÷µÄÖµ
{
unsigned int val;
unsigned char th,tl;
th=TH0;
tl=TL0;
val=th*256+tl;
return val;
}
void main()
{
unsigned int shi;
unsigned int ge;
init();
while(1)
{
num=read();
if(num>=100)
{
num=0;
TH0=TL0=0;
}
shi=num/10;
ge=num%10;
display(shi,ge);
}
}
就是用计数器T0,导线一端接地,另一端去触碰P3^4,并显示计数。运行的时候始终显示00. |