|
#include<reg51.h>
#include<intrins.h>
#define GPIO_KEY P1
#define SEG P0
#define LED P2
/*138譯碼器位選數(shù)碼管*/
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit Beep=P1^5;
typedef unsigned int uint;
typedef unsigned char uchar;
uchar code segduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar shi=23,fen=59,miao=50;
uchar KeyValue;
bit flag;
uchar jishu=0;
void delay(uint us)
{
while(us--);
}
void KeyDown()
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//讀取按鍵是否按下
{
delay(100);//延時10ms進行消抖
if(GPIO_KEY!=0x0f)//再次檢測鍵盤是否按下
{
//測試列
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//測試行
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
{
delay(1000);
a++;
}
}
}
}
void weixuan(uchar se,uint ne)
{
switch(se)
{
/*1-8位數(shù)碼管位選*/
case 0:LSC=0;LSB=0;LSA=0;break;
case 1:LSC=0;LSB=0;LSA=1;break;
case 2:LSC=0;LSB=1;LSA=0;break;
case 3:LSC=0;LSB=1;LSA=1;break;
case 4:LSC=1;LSB=0;LSA=0;break;
case 5:LSC=1;LSB=0;LSA=1;break;
case 6:LSC=1;LSB=1;LSA=0;break;
case 7:LSC=1;LSB=1;LSA=1;break;
}
SEG = segduan[ne];
delay(5);
SEG = 0x00;
}
void dpDisplay()
{
LSC=0;LSB=1;LSA=0;//case2:
SEG = 0x40;
delay(5);
SEG = 0x00;
LSC=1;LSB=0;LSA=1;//case5:
SEG = 0x40;
delay(5);
SEG = 0x00;
}
void miaoDisplay(uchar num)
{
uchar n;
n=num%10;
weixuan(0,n);
n=num/10;
weixuan(1,n);
}
void fenDisplay(uchar num)
{
uchar n;
n=num%10;
weixuan(3,n);
n=num/10;
weixuan(4,n);
}
void shiDisplay(uchar num)
{
uchar n;
n=num%10;
weixuan(6,n);
n=num/10;
weixuan(7,n);
}
void Timer0_set()
{
TMOD = 0x01;
TH0=0x4b;
TL0=0xfc;
TR0=1;
}
void main()
{
void seg1();
void seg01();
void seg02();
void fx4();
LED = 0xff;
Timer0_set();
while(1)
{
KeyValue = 0;
KeyDown();
switch(KeyValue)
{
case 2:flag=0;break;
case 6:flag=1;break;
case 10:shi=23;fen=59;miao=50;break;
case 14:shi=0;fen=0;miao=10;break;
default:break;
}
seg1();
};
}
void seg1()
{
if(flag==0)
seg01();
if(flag==1)
seg02();
miaoDisplay(miao);
dpDisplay();
fenDisplay(fen);
shiDisplay(shi);
fx4();
}
void seg01()
{
if(TF0==1)
{ TH0=0x4b;
TL0=0xfc;
TF0=0;
jishu++;
};
if(jishu==20)
{
jishu=0;
miao++;
};
if(miao==60)
{
miao=0;
if(fen!=59)
fen++;
if(fen==59)
{
fen=0;
if(shi!=23)
shi++;
if(shi==23)
shi=0;
}
};
}
void seg02()
{
if(TF0==1)
{ TH0=0x4b;
TL0=0xfc;
TF0=0;
jishu++;
};
if(jishu==20)
{
jishu=0;
miao--;
};
if(miao==0)
{
miao=59;
if(fen !=0)
fen--;
if(fen==0)
{
fen = 59;
if(shi !=0)
shi--;
if(shi==0)
shi=23;
}
};
}
void fx4()
{
if(shi==0 && fen==0)
{
if(miao<=3)
{
LED = 0x03;
Beep=~Beep;
delay(5);
}
else
LED = 0xff;
}
}
|
|