#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
code unsigned char LED[] = { // 八段數碼管字形碼 (共陰極)
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, //顯示字符0,1,2,3,4,5,6,7
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71,0x6e //顯示字符8,9,A,B,C,D,E,F
};
sbit key=P3^3;
int flag; //標志位
uint temp=999;//初始值
void delay( uint k )
{
uint m,n;
for(m=0;m<k;m++)
{
for(n=0;n<120;n++);
}
}
void Timer0() interrupt 1
{
static uchar Count_50;//
TH0=0xb1;
TL0=0xdf;//定時時間為20ms,每20ms中斷一次
Count_50--;
if(Count_50==50)//20ms*50=1秒
{
Count_50=0;
temp--;
}
}
void init()//初始化
{
TMOD=0x01;
TH0=0xb1;
TL0=0xdf;//定時時間為20ms,每20ms中斷一次
ET0=1; //開T1中斷
EA=1; //開總中斷
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;// TR1=1; //T1開始運行
delay(14);
delay(15);
}
void display()
{
if(key==0)
{ delay(15);
if(key==0)
{
flag++;
if(flag==4)
{
flag=1;
}
while(!key);
}
}
switch(flag)
{
case 1:
temp=999;//恢復初值
ET0=0; //關T0中斷
EA=0; //關總中斷
TR0=0; //T1不運行
P2=0xef; //百位
P0=LED[temp/100];
delay(4);
P2=0xef; //十位
P0=LED[temp%100];
delay(4);
P2=0xef;//個位
P0=LED[temp%10];
delay(4);
break;
case 2:
ET0=1; //開T1中斷
EA=1; //開總中斷
TR0=1; //T1開始運行
if(temp>0)
{
P2=0xef; //百位
P0=LED[temp/100];
delay(4);
P2=0xef; //十位
P0=LED[temp%100/10];
delay(4);
P2=0xff;//個位
P0=LED[temp%10];
delay(4);
}
else
{
P2=0xef; //百位
P0=LED[temp/100];
delay(4);
P2=0xef; //十位
P0=LED[temp%100/10];
delay(4);
P2=0xef;//個位
P0=LED[temp%10];
delay(4);
}
break;
case 3:
ET0=0; //關T1中斷
EA=0; //關總中斷
TR0=0; //T0不運行
delay(5);
P2=0x76;
P0=LED[3];
delay(5);
P2=0x76;
P0=LED[2];
delay(5);
P2=0x76;
P0=LED[1];
delay(5);
break;
}
}
void main()
{
init();
P2=0Xff; //開數碼管
while(1)
{
display();
}
}
求大神改一下,改成第二次中斷后顯示999自減 |