独立按键的综合应用,让其中一个数码管点亮,S2数字加一,S3数字减一,S4归零,S5从当前位置开始计时。
#include <reg52.h>
sbit KeyS2=P3^4;
sbit KeyS3=P3^5;
sbit KeyS4=P3^6;
sbit KeyS5=P3^7;
sbit d1=P1^0;
sbit dula=P2^6;
sbit wela=P2^7;
char flag=0;
static int cnt_time=0;
unsigned char code LedChar[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
unsigned char num=0;
void delay();
void main()
{
wela=1;
P0=0xdf;
wela=0;
dula=1;
P0=LedChar[num];
dula=0;
P3=0xff;
TMOD=0x11;
TH0=0xb8;
TL0=0x00;
TH1=0xdc;
TL1=0x00;
TR0=1;
EA=1;
ET0=1;
ET1=1;
while(1)
{
switch(flag)
{
case 1: flag=0;num++;if(num>=10) num=0;break;
case 2: flag=0;if(num>0) num--;if(num<=0) num=9;break;
case 3: flag=0;num=0;break;
case 4: TR1=1; if(cnt_time>=100){cnt_time=0; num++;if(num>=10) num=0;}break;
default:break;
}
dula=1;P0=LedChar[num];dula=0;
}
}
void Key_Test() interrupt 1
{
static unsigned char cnt;
TH0=0xb8;
TL0=0x00;
cnt++;
if(KeyS2==0)
{
if(cnt>=10)
{
cnt=0;
if(KeyS2==0)
{
flag=1;
}
}
}
if(KeyS3==0)
{
if(cnt>=10)
{
cnt=0;
if(KeyS3==0)
{
flag=2;
}
}
}
if(KeyS4==0)
{
if(cnt>=10)
{
cnt=0;
if(KeyS4==0)
{
flag=3;
}
}
}
if(KeyS5==0)
{
if(cnt>=10)
{
cnt=0;
if(KeyS5==0)
{
flag=4;
}
}
}
}
void T1_Timer() interrupt 3
{
TH1=0xdc;
TL1=0x00;
cnt_time++;
}