#include <REGX52.H>
sbit R1=P1^3;
sbit R2=P1^2;
sbit R3=P1^1;
sbit R4=P1^0;
sbit C1=P1^7;
sbit C2=P1^6;
sbit C3=P1^5;
sbit C4=P1^4;
sbit LSA=P2^4;
sbit LSB=P2^3;
sbit LSC=P2^2;
unsigned char i;
unsigned char smg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x37,0x7C,0x39,0x5E,0x79,0x71}; //数码管断码
unsigned char count1=0;
unsigned int count2=0;
unsigned int flag_a=0,flag_b=60;
unsigned char KeyNum=0;
void delay(unsigned int i)
{
while(--i!=0);
}
void init60s()
{
TMOD = 0X01;
TH0= (65536-10000)/256;
TL0= (65536-10000)%256;
TR0=1;
ET0=1;
EA=0;
}
unsigned int unx=0,coun1=0;
int unx1=60;
void init_60s() interrupt 1
{
TR0=0;
TH0= (65536-10000)/256;
TL0= (65536-10000)%256;
coun1++;
if(coun1==100)
{
if(KeyNum==9){unx++;if(unx==61)unx=0;}
if(KeyNum==10){unx1--;if(unx1<0)unx1=60;}
coun1=0;
}
TR0=1;
}
void ScanSMG(unsigned char n,unsigned char m) //数码管显示
{
switch(n)
{
case 0:P2_4=1;P2_3=1;P2_2=1;break;
case 1:P2_4=1;P2_3=1;P2_2=0;break;
case 2:P2_4=1;P2_3=0;P2_2=1;break;
case 3:P2_4=1;P2_3=0;P2_2=0;break;
case 4:P2_4=0;P2_3=1;P2_2=1;break;
case 5:P2_4=0;P2_3=1;P2_2=0;break;
case 6:P2_4=0;P2_3=0;P2_2=1;break;
case 7:P2_4=0;P2_3=0;P2_2=0;break;
}
P0=smg[m];
delay(5);
P0=0x00;
}
void key() //矩阵键盘
{
P1=0xf0; //IO口全部初始化为高电平
R1=0;R2=1;R3=1;R4=1;
if(C1==0){while(C1==0);KeyNum=1;}
if(C2==0){while(C2==0);KeyNum=5;}
if(C3==0){while(C3==0);KeyNum=9;}
if(C4==0){while(C4==0);KeyNum=13;}
P1=0xF0; //IO口全部初始化为高电平
R1=1;R2=0;R3=1;R4=1;
if(C1==0){while(C1==0);KeyNum=2;}
if(C2==0){while(C2==0);KeyNum=6;}
if(C3==0){while(C3==0);KeyNum=10;}
if(C4==0){while(C4==0);KeyNum=14;}
P1=0xF0; //IO口全部初始化为高电平
R1=1;R2=1;R3=0;R4=1;
if(C1==0){while(C1==0);KeyNum=3;}
if(C2==0){while(C2==0);KeyNum=7;}
if(C3==0){while(C3==0);KeyNum=11;}
if(C4==0){while(C4==0);KeyNum=15;}
P1=0xF0; //IO口全部初始化为高电平
R1=1;R2=1;R3=1;R4=0;
if(C1==0){while(C1==0);KeyNum=4;}
if(C2==0){while(C2==0);KeyNum=8;}
if(C3==0){while(C3==0);KeyNum=12;}
if(C4==0){while(C4==0);KeyNum=16;}
}
void SEM()
{
ScanSMG(1,KeyNum/10);
ScanSMG(2,KeyNum%10);
}
unsigned char max=0;
void panduan()
{
key();
switch(KeyNum)
{
case 1:for(i=0;i<8;i++){P2=~(0x01<<i);key();delay(20000);}break;
case 2:for(i=0;i<8;i++){P2=~(0x80>>i);key();delay(20000);}break;
case 3:for(i=0;i<4;i++){P2=(~(0x01<<i))&(~(0x80>>i));key();delay(20000);};break;
case 4:for(i=0;i<=4;i++){P2=(~(0x08<<i))&(~(0x10>>i));key();delay(20000);}break;
case 5:P2=0xaa;delay(20000);P2=~0xaa;delay(20000);key();break;
case 6:P2=0x0f;delay(20000);P2=~0x0f;delay(20000);key();break;
case 7:ScanSMG(1,max);key();break;
case 8:ScanSMG(0,1);ScanSMG(1,4);ScanSMG(2,4);ScanSMG(3,2);ScanSMG(4,0);ScanSMG(5,5);ScanSMG(6,0);ScanSMG(7,7);break;
case 9:unx1=60;EA=1;ScanSMG(0,unx/10);ScanSMG(1,unx%10);break;
case 10:unx=0;EA=1;ScanSMG(0,unx1/10);ScanSMG(1,unx1%10);break;
case 11: P1_0=1;delay(99);P1_0=0;delay(1);break;
case 12: P1_0=1;delay(50);P1_0=0;delay(50);break;
case 13: P1_0=1;delay(30);P1_0=0;delay(70);break;
}
}
unsigned int cout=0;
void main()
{
init60s();
while(1)
{
if(KeyNum==7){cout++;if(cout==1000){max++;cout=0;if(max==10)max=0;}}
EA=0;
panduan();
key();
}
}