简易电子钟
#include <reg51.h>
#include <display.h>
void T0_ISR_Init(void);//初始化计数器1
void T1_ISR_Init(void);//初始化计数器2
void Timering();
unsigned char Key_Press_1(void);
unsigned char Key_Press_2(void);
void delayms(unsigned char n);
unsigned char Timer[6] = {0};
unsigned char flag,t_50ms;
char Index = 0;
sbit Key_1 = P2 ^ 2;
sbit Key_2 = P2 ^ 3;
void main()
{
T0_ISR_Init();
T1_ISR_Init();
while(1)
{
TempData[0] = Seg_Code[Timer[5]];
TempData[1] = Seg_Code[Timer[4]];
TempData[2] = Seg_Code[10];
TempData[3] = Seg_Code[Timer[3]];
TempData[4] = Seg_Code[Timer[2]];
TempData[5] = Seg_Code[10];
TempData[6] = Seg_Code[Timer[1]];
TempData[7] = Seg_Code[Timer[0]];
flag = Key_Press_2();
if(1 == flag)
{
Index ++;
if(Index >= 6)
Index = 0;
}
else if(2 == flag)
{
Index = 0;
}
if(1 == Key_Press_1())
{
Timer[Index] ++;
Timering();
//Index = 5;
t_50ms = 0;
T0_ISR_Init();
}
if(20 == t_50ms)
{
t_50ms = 0;
Timer[0] ++;
Timering();
}
//display(0,8);
}
}
void T1_ISR_Init(void)
{
TMOD |= 0x10;
TH1 = (65536 - 200) / 256;
TL1 = (65536 - 200) % 256;
EA = 1;
ET1 = 1;
PT1 = 0;
TR1 = 1;
}
void T1_ISR(void) interrupt 3
{
TH1 = (65536 - 200) / 256;
TL1 = (65536 - 200) % 256;
display(0,8);
}
void T0_ISR_Init(void)
{
TMOD |= 0x01;
TH0 = 0x3c;//(65536 - 50000) / 256
TL0 = 0xb0;//(65536 - 50000) % 256
EA = 1;
ET0 = 1;
PT0 = 1;
TR0 = 1;
}
void T0_ISR(void) interrupt 1
{
EA = 0;
TH0 = 0x3c;
TL0 = 0xb0;
t_50ms ++;
EA = 1;
}
unsigned char Key_Press_1(void)
{
unsigned char flag = 0;
if(!Key_1)//按键按下,去抖
{
delayms(10);
if(!Key_1)
{
flag = 1;
while(!Key_1);
}
}
return flag;
}
unsigned char Key_Press_2(void)
{
unsigned char flag = 0;
unsigned char num = 0;
if(!Key_2)
{
delayms(10);
if(!Key_2)
{
flag = 1;
while(!Key_2)//判断是否长按
{
delayms(200);
num ++;
if(num > 20)
{
ET1 = 0;
flag = 2;
}
}
ET1 = 1;
}
}
return flag;
}
void Timering()
{
if(10 <= Timer[0])
{
Timer[0] = 0;
Timer[1] ++;
}
if(6 <= Timer[1])
{
Timer[1] = 0;
Timer[2] ++;
}
if(10 <= Timer[2])
{
Timer[2] = 0;
Timer[3] ++;
}
if(6 <= Timer[3])
{
Timer[3] = 0;
Timer[4] ++;
}
if(10 <= Timer[4])
{
Timer[4] = 0;
Timer[5] ++;
}
if(Timer[5] >= 2)
{
Timer[5] = 2;
}
if(4 <= Timer[4] && 2 <= Timer[5])
{
Timer[0] = Timer[1] = Timer[2] = Timer[3] = Timer[4] = Timer[5] = 0;
}
}
void delayms(unsigned char n)
{
while(--n);
}
#include "display.h"
sbit Seg_Latch = P2 ^ 0; //段锁存
sbit Bit_Latch = P2 ^ 1; //位锁存
unsigned char code Seg_Code[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
//断码显示
unsigned char code Bit_Code[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char TempData[8];
void display(unsigned char firstbit,unsigned char num)
{
unsigned char i;
for(i = 0;i < num;i ++)
{
DataPort=0;
Seg_Latch = 1;
Seg_Latch = 0;
DataPort=Bit_Code[i+firstbit];
Bit_Latch = 1;
Bit_Latch = 0;
DataPort =TempData[i];
Seg_Latch = 0;
Seg_Latch = 1;
}
}