忠实原著,不考虑优化
#include
#define uchar unsigned char
#define uint unsigned int
sbit STRT=P3^5;
sbit STP=P3^6;
sbit CLRR=P3^7;
sbit DS1=P2^7;
sbit DS0=P2^6;
uchar dtab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uchar ts,tc;
//无参数输入的延时子程序,误差会比汇编大
void delay()
{ uint i;
for(i=500;i>0;i--);
}
void disp()
{
uchar i,j;
i=tc/10;
j=tc%10;
P0=dtab[i];
DS1=0;
delay();
DS1=1;
P0=dtab[j];
DS0=0;
delay();
DS0=1;
}
void main()
{
SP=0x60;
TMOD =0X01;
EA=1;
ET0=1;
TH0=0X3C;
TL0=0XB0;
while(1)
{
disp(); //显示
//键扫
if(STRT)
{
disp(); //通过调用显示子程序消抖
if(STRT)TR0=1;
}
if(STP)
{
disp();
if(STP){TR0=0;}
}
if(CLRR)
{
disp();
if(CLRR){tc=0;}
}
}
}
void T0INT() interrupt 1
{
TH0=0X3C;
TL0=0XB0;
ts++;
if(ts>=20){
tc++;
if(tc>=100){tc=0;TR0=0;}
}
}