基于STC89C51单片机的智能时钟Keil工程
立即下载
资源介绍:
通过数码管显示年、月、日、时、分、秒;同时可以通过按键修改时间。其中包含最简单的按键消抖、简单C语言语句的运用、数码管的动态显示、74HC573数字芯片的运用、51单片机定时器中断的使用;适合新手观看,主要提供一点思路。
#include
#include
#define u8 unsigned char
#define u16 unsigned int
sbit DU = P2 ^ 6; //数码管段选
sbit WE = P2 ^ 7; //数码管位选
sbit key1 = P3 ^ 0; //切换日历和时间。
sbit key2 = P3 ^ 1; //切换位用来准备调值。
sbit key3 = P3 ^ 2; //加数器。
sbit key4 = P3 ^ 3; //切换到秒表。
u8 year = 22, month, day, h, m, s; //时间计数变量。
u8 ms, ss, us; //秒表计数变量。
u16 count, count1; //计数变量。
u8 keyvalue0, keyvalue, keyvalue1; //切换模式,调节时分秒各个位置,开关秒表。
void delay(int i)//5us延时函数。
{
while(i--)
_nop_();
}
void time0()//设置定时器0,8位自动重装模式,定时0.2ms
{
TMOD = 0x02;
TH0 = 0x37;
TL0 = 0x37;
EA = 1;
ET0 = 1;
TR0 = 1;
}
u8 code SMGduan[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; //固定数值数组。
void display(u8 a, u8 b, u8 c) //数码管显示函数。
{
if(keyvalue != 2 && keyvalue != 3)
{
DU = 0;
P0 = SMGduan[a / 10];
DU = 1;
DU = 0;
WE = 0;
P0 = 0xFE;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
DU = 0;
P0 = SMGduan[a % 10];
DU = 1;
DU = 0;
WE = 0;
P0 = 0xFD;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
}
DU = 0;
P0 = 0x40;
DU = 1;
DU = 0;
WE = 0;
P0 = 0xFB;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
if(keyvalue != 1 && keyvalue != 3)
{
DU = 0;
P0 = SMGduan[b / 10];
DU = 1;
DU = 0;
WE = 0;
P0 = 0xF7;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
DU = 0;
P0 = SMGduan[b % 10];
DU = 1;
DU = 0;
WE = 0;
P0 = 0xEF ;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
}
DU = 0;
P0 = 0x40 ;
DU = 1;
DU = 0;
WE = 0;
P0 = 0xDF;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
if(keyvalue != 1 && keyvalue != 2)
{
DU = 0;
P0 = SMGduan[c / 10];
DU = 1;
DU = 0;
WE = 0;
P0 = 0xBF;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
DU = 0;
P0 = SMGduan[c % 10];
DU = 1;
DU = 0;
WE = 0;
P0 = 0x7F ;
WE = 1;
delay(100);
P0 = 0xFF;
WE = 0;
}
}
void keyscan()//按键扫描。
{
if(key1 == 0)
{
delay(4000);
if(key1 == 0)
{
keyvalue0++;
if(keyvalue0 == 2)
keyvalue0 = 0;
}
while(!key1);
}
if(key2 == 0)
{
delay(4000);
if(key2 == 0)
{
keyvalue++;
if(keyvalue == 4)
keyvalue = 0;
}
while(!key2);
}
if(key4 == 0)
{
delay(4000);
if(key4 == 0)
{
keyvalue1++;
if(keyvalue1 == 3)
{
keyvalue1 = 0;
ms = 0;
ss = 0;
us = 0;
}
}
while(!key4);
}
if(keyvalue0 == 1)
{
if(keyvalue == 1)
{
if(key3 == 0)
{
delay(4000);
if(key3 == 0)
{
year++;
if(year == 51)
year = 22;
while(!key3);
}
}
}
if(keyvalue == 2)
{
if(key3 == 0)
{
delay(4000);
if(key3 == 0)
{
month++;
if(month == 13)
month = 1;
while(!key3);
}
}
}
if(keyvalue == 3)
{
if(key3 == 0)
{
delay(4000);
if(key3 == 0)
{
day++;
if(day == 31)
day = 1;
while(!key3);
}
}
}
}
else
{
if(keyvalue == 1)
{
if(key3 == 0)
{
delay(4000);
if(key3 == 0)
{
h++;
if(h == 24)
h = 0;
while(!key3);
}
}
}
if(keyvalue == 2)
{
if(key3 == 0)
{
delay(4000);
if(key3 == 0)
{
m++;
if(m == 60)
m = 0;
while(!key3);
}
}
}
if(keyvalue == 3)
{
if(key3 == 0)
{
delay(4000);
if(key3 == 0)
{
s++;
if(s == 60)
s = 0;
while(!key3);
}
}
}
}
}
void main()
{
time0();
while(1)
{
keyscan();
if(keyvalue1)
{
display(ms, ss, us);
}
else
{
if(keyvalue0)
display(year, month, day);
else
display(h, m, s);
}
}
}
void time0h() interrupt 1//定时器0中断函数。
{
count++;
if(count == 46)
{
count = 0;
count1++;
if(keyvalue1 == 1) //进入秒表。
{
us++;
if(us == 100)
{
us = 0;
ss++;
if(ss == 60)
{
ss = 0;
ms++;
}
}
}
if(count1 == 100) //正常时间累加。
{
count1 = 0;
s++;
if(s == 60)
{
s = 0;
m++;
if(m == 60)
{
m = 0;
h++;
if(h == 24)
{
h = 0;
day++;
if(day == 30)
{
day = 0;
month++;
if(month == 12)
{
month = 0;
year++;
}
}
}
}
}
}
}
}