#include
#include
#define uchar unsigned char
#define uint unsigned int
Uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0~F的共阴极字段码表
uchar chocode[]={0xfe,0xfd,0xfb,0xf7}; //位选码表
uchar N,M;
sbit P3_5=P3^5; //位定义
sbit P3_6=P3^6;
sbit P3_7=P3^7;
sbit P4_4=P4^4;
sbit P3_3=P3^3;
sbit P3_4=P3^4;
sbit P0_7=P0^7;
//===================================延时1毫秒====================== void Delay1ms()
{
unsigned char i, j;
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
//==================================延时300毫秒===================== void Delay300ms()
{
unsigned char i, j, k;
i = 13;
j = 156;
k = 83;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
//=====================================数码管显示================== void display(uchar N,M)
{
P1M0=0xff;
P1M1=0x00;
P0=chocode[3];
P1=table[M];
Delay1ms();
P0=chocode[2];
P1=table[N];
Delay1ms();
P0=chocode[1];
P1=table[0];
Delay1ms();
P0=chocode[0];
P1=table[0];
Delay1ms();
}
//========================扫描键盘,设置倒计时初值=============== void Scan_Key(void)
{
uchar m;
bit FLAG0=0;
P3_6=0;
P3_7=1;
P0=0xff;
m=P0;
switch(m&0xf0)
{
case 0xd0: N++;
FLAG0=1;
break;
case 0xb0: M++;
FLAG0=1;
break;
default: break;
}
if(FLAG0)
{
while((P0|0x0f)!=0xff)
{
display(N,M);
}
}
else
{
display(N,M);
}
P3_6=1;
P3_7=0;
P0=0xff;
m=P0;
switch(m&0xf0)
{
case 0xd0: N--;
FLAG0=1;
break;
case 0xb0: M--;
FLAG0=1;
break;
default: break;
}
if(FLAG0)
{
while((P0|0x0f)!=0xff)
{
display(N,M);
} //松手检测//松手检测
}
else
{
display(N,M);
}
P3_7=1;
P3_6=0;
}
//==========================主函数============================== main()
{
P4SW|=0x10; //将P4_4口定义为普通I/O口
P1=0x0;
while(1)
{
uchar i;
Scan_Key(); //扫描键盘,确定倒计时初值
P0=0xff;
if(P0_7==0) //检测是否开始倒计时
{
while((P0|0x0f)!=0xff) //松手检测
{
display(N,M);
}
while(1) //定时开始
{
for(i=0;i
TMOD=0x01;
TH0=0xd8;
TL0=0xf0;
TR0=1;
display(N,M);
do{display(N,M);} while(TF0==0);//检测是否完成一次定时 }
if((M|N)==0) //定时到,所有灯闪烁
{
while(1)
{
P0=0xff;
} } } P2=0x00; P4_4=0; P3_3=0; P3_4=0; P3_5=0; Delay300ms(); P2=0xff; P4_4=1; P3_3=1; P3_4=1; P3_5=1; Delay300ms(); } } if(M==0) { M=9; N--; } else M--;
#include
#include
#define uchar unsigned char
#define uint unsigned int
Uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0~F的共阴极字段码表
uchar chocode[]={0xfe,0xfd,0xfb,0xf7}; //位选码表
uchar N,M;
sbit P3_5=P3^5; //位定义
sbit P3_6=P3^6;
sbit P3_7=P3^7;
sbit P4_4=P4^4;
sbit P3_3=P3^3;
sbit P3_4=P3^4;
sbit P0_7=P0^7;
//===================================延时1毫秒====================== void Delay1ms()
{
unsigned char i, j;
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
//==================================延时300毫秒===================== void Delay300ms()
{
unsigned char i, j, k;
i = 13;
j = 156;
k = 83;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
//=====================================数码管显示================== void display(uchar N,M)
{
P1M0=0xff;
P1M1=0x00;
P0=chocode[3];
P1=table[M];
Delay1ms();
P0=chocode[2];
P1=table[N];
Delay1ms();
P0=chocode[1];
P1=table[0];
Delay1ms();
P0=chocode[0];
P1=table[0];
Delay1ms();
}
//========================扫描键盘,设置倒计时初值=============== void Scan_Key(void)
{
uchar m;
bit FLAG0=0;
P3_6=0;
P3_7=1;
P0=0xff;
m=P0;
switch(m&0xf0)
{
case 0xd0: N++;
FLAG0=1;
break;
case 0xb0: M++;
FLAG0=1;
break;
default: break;
}
if(FLAG0)
{
while((P0|0x0f)!=0xff)
{
display(N,M);
}
}
else
{
display(N,M);
}
P3_6=1;
P3_7=0;
P0=0xff;
m=P0;
switch(m&0xf0)
{
case 0xd0: N--;
FLAG0=1;
break;
case 0xb0: M--;
FLAG0=1;
break;
default: break;
}
if(FLAG0)
{
while((P0|0x0f)!=0xff)
{
display(N,M);
} //松手检测//松手检测
}
else
{
display(N,M);
}
P3_7=1;
P3_6=0;
}
//==========================主函数============================== main()
{
P4SW|=0x10; //将P4_4口定义为普通I/O口
P1=0x0;
while(1)
{
uchar i;
Scan_Key(); //扫描键盘,确定倒计时初值
P0=0xff;
if(P0_7==0) //检测是否开始倒计时
{
while((P0|0x0f)!=0xff) //松手检测
{
display(N,M);
}
while(1) //定时开始
{
for(i=0;i
TMOD=0x01;
TH0=0xd8;
TL0=0xf0;
TR0=1;
display(N,M);
do{display(N,M);} while(TF0==0);//检测是否完成一次定时 }
if((M|N)==0) //定时到,所有灯闪烁
{
while(1)
{
P0=0xff;
} } } P2=0x00; P4_4=0; P3_3=0; P3_4=0; P3_5=0; Delay300ms(); P2=0xff; P4_4=1; P3_3=1; P3_4=1; P3_5=1; Delay300ms(); } } if(M==0) { M=9; N--; } else M--;