////////////////////////////////////////////////
/*接线方法:
P0.0-P0.7 ----> 独立按键
P1.0-P1.7 ----> 共阴数码管
P3.0-P3.7 ----> 共阳数码管
P2.3 ----> A位码
P2.2 ----> B段码*/
///////////////////////////////////////////////
#include
#define wait 300
sbit LATCH_bit = P2^3;
sbit LATCH_seg = P2^2;
sbit KEY1=P0^0; //定义按键输入端口
sbit KEY2=P0^1;
sbit KEY3=P0^2;
sbit KEY4=P0^3;
sbit KEY5=P0^4;
sbit KEY6=P0^5;
sbit KEY7=P0^6;
sbit KEY8=P0^7;
unsigned int LED[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay(int n);
void time();
void DelayMs(unsigned char t);
void DelayUs2x(unsigned char t);
void change();
void revise_hour();
void revise_min();
void revise_sec();
unsigned char KeyScan(void);
char sec;
char min;
char h;
unsigned char local = 0;
void main()
{
time();
}
void delay(int n)
{
while(n--);
}
void time()
{
TMOD = 0x01;
TH0 = 0x3c;
TL0 = 0xb0;
EA = 1;
ET0 = 1;
TR0 = 1;
sec = 49;
min = 57;
h = 23;
while(1)
{
P3 = LED[local];
change();
//时
P1 = 0xfe;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = ~LED[h/10];
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
P1 = 0xfd;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = ~LED[h%10];
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
//-
P1 = 0xfb;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = 0x40;
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
//分
P1 = 0xf7;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = ~LED[min/10];
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
P1 = 0xef;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = ~LED[min%10];
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
//-
P1 = 0xdf;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = 0x40;
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
//秒
P1 = 0xbf;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = ~LED[sec/10];
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
P1 = 0x7f;
LATCH_bit = 1;
LATCH_bit = 0;
P1 = ~LED[sec%10];
LATCH_seg = 1;
LATCH_seg = 0;
delay(wait);
if(sec == 60)
{
sec = 0;
min++;
if(min == 60)
{
min = 0;
h++;
if(h == 24)
h = 0;
}
}
}
}
void TSR_T0(void) interrupt 1
{
unsigned char i;
TH0 = 0x3c;
TL0 = 0xb0;
i++;
if(i == 20)
{
sec++;
i = 0;
}
}
void change()
{
if(KeyScan() == 3)
{
local++;
if( local == 7)
local = 0;
}
if(KeyScan() == 4)
{
local--;
if( local == -1)
local = 6;
}
if( local == 1 && KeyScan() == 1)
{
sec++;
revise_sec();
}
if( local == 2 && KeyScan() == 1)
{
sec += 10;
revise_sec();
}
if( local == 3 && KeyScan() == 1)
{
min++;
revise_min();
}
if( local == 4 && KeyScan() == 1)
{
min += 10;
revise_min();
}
if( local == 5 && KeyScan() == 1)
{
h++;
revise_hour();
}
if( local == 6 && KeyScan() == 1)
{
h += 10;
revise_hour();
}
if( local == 1 && KeyScan() == 2)
{
sec--;
revise_sec();
}
if( local == 2 && KeyScan() == 2)
{
sec -= 10;
revise_sec();
}
if( local == 3 && KeyScan() == 2)
{
min--;
revise_min();
}
if( local == 4 && KeyScan() == 2)
{
min -= 10;
revise_min();
}
if( local == 5 && KeyScan() == 2)
{
h--;
revise_hour();
}
if( local == 6 && KeyScan() == 2)
{
h -= 10;
revise_hour();
}
}
void revise_hour()
{
if(h >= 24)
h %= 24;
if(h < 0)
h += 24;
}
void revise_min()
{
if( min >= 60)
{
h++;
min %= 60;
revise_hour();
}
if( min < 0)
{
h--;
min += 60;
revise_hour();
}
}
void revise_sec()
{
if( sec >= 60)
{
sec %= 60;
min++;
revise_min();
}
if( sec < 0)
{
sec += 60;
min--;
revise_min();
}
}
unsigned char KeyScan(void)
{
/********************************************************/
if(!KEY1) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(!KEY1) //再次确认按键是否按下,没有按下则退出
{
while(!KEY1);//如果确认按下按键等待按键释放,没有则退出
{
return 1;
}
}
}
/********************************************************/
else if(!KEY2) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(!KEY2) //再次确认按键是否按下,没有按下则退出
{
while(!KEY2);//如果确认按下按键等待按键释放,没有则退出
{
return 2;
}
}
}
/********************************************************/
else if(!KEY3) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
//delay(30000);
if(!KEY3) //再次确认按键是否按下,没有按下则退出
{
while(!KEY3);//如果确认按下按键等待按键释放,没有则退出
{
return 3;
}
}
}
/********************************************************/
else if(!KEY4) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(!KEY4) //再次确认按键是否按下,没有按下则退出
{
while(!KEY4);//如果确认按下按键等待按键释放,没有则退出
{
return 4;
}
}
}
/********************************************************/
else if(!KEY5) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(!KEY5) //再次确认按键是否按下,没有按下则退出
{
while(!KEY5);//如果确认按下按键等待按键释放,没有则退出
{
return 5;
}
}
}
/********************************************************/
else if(!KEY6) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(!KEY6) //再次确认按键是否按下,没有按下则退出
{
while(!KEY6);//如果确认按下按键等待按键释放,没有则退出
{
return 6;
}
}
}
/********************************************************/
else if(!KEY7) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(!KEY7) //再次确认按键是否按下,没有按下则退出
{
while(!KEY7);//如果确认按下按键等待按键释放,没有则退出
{
return 7;
}
}
}
/********************************************************/
else if(!KEY8) //如果检测到低电平,说明按键按下
{
DelayMs(10); //延时去抖,一般10-20ms
if(!KEY8) //再次确认按键是否按下,没有按下则退出
{
while(!KEY8);//如果确认按下按键等待按键释放,没有则退出
{
return 8;
}
}
}
/********************************************************/
/*else
return 0;*/
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void DelayUs2x(unsigned char t)
{
while(--t);
}