#include <reg52.h>
sbit DGT0=P2^3;
sbit DGT1=P2^2;
sbit DGT2=P2^1;
sbit DGT3=P2^0;
unsigned char code LedChar[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x90};
unsigned char LedBuff[4]={0x90,0x90,0x90,0x90};
unsigned char flagls=0;
unsigned int cnt=0;
unsigned char i=0;
void main()
{
unsigned int sec=9999;
TMOD=0x01;//MOV TMOD,#01H
TH0=0xFC;
TL0=0x18;
TR0=1;
EA=1;
ET0=1;
while(1)
{
if(flagls==1)
{
flagls=0;
sec--;
LedBuff[0]=LedChar[sec%10];
LedBuff[1]=LedChar[sec/10%10];
LedBuff[2]=LedChar[sec/100%10];
LedBuff[3]=LedChar[sec/1000%10];
if(sec==0)
sec=9999;
}
}
}
void InterruptTime0() interrupt 1
{
TH0=0xFC;
TL0=0x18;
cnt++;
if (cnt>=1000)
{
cnt=0;
flagls=1;
}
P0=0xFF;
switch (i)
{
case 0:DGT3=0;DGT2=0;DGT1=0;DGT0=1;i++;P0=LedBuff[0];break;
case 1:DGT3=0;DGT2=0;DGT1=1;DGT0=0;i++;P0=LedBuff[1];break;
case 2:DGT3=0;DGT2=1;DGT1=0;DGT0=0;i++;P0=LedBuff[2];break;
case 3:DGT3=1;DGT2=0;DGT1=0;DGT0=0;i=0;P0=LedBuff[3];break;
}
}
;;以上的代码可以试一下,亲测有效 |