|
#include<reg51.h>
#define uChar unsigned char
#define uInt unsigned int
uChar a[10] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f };
uChar b[8] = { 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f };
uChar second = 50, minute = 59, hour = 12, year = 18, month = 07, day = 21, count;
sbit Key1 = P3 ^ 0; //計時停止
sbit Key2 = P3 ^ 1; //調(diào)位
sbit Key3 = P3 ^ 2; //加一
sbit Key4 = P3 ^ 3; //切換
sbit Buzzer = P1 ^ 1;
/*********************延遲函數(shù)********************/
void Delay(uInt t)
{
while (t)
{
t--;
}
}
/*********************時分秒顯示函數(shù)*******************/
void Dispaly1(uChar second, uChar minute, uChar hour)
{
/*********************第一位數(shù)碼管*********************/
P2 = b[0];
P0 = a[hour / 10];
Delay(10);
/*********************第二位數(shù)碼管*********************/
P2 = b[1];
P0 = a[hour % 10];
Delay(10);
/*********************第三位數(shù)碼管*********************/
P2 = b[2];
P0 = 0x40;
Delay(10);
/*********************第四位數(shù)碼管*********************/
P2 = b[3];
P0 = a[minute / 10];
Delay(10);
/*********************第五位數(shù)碼管*********************/
P2 = b[4];
P0 = a[minute % 10];
Delay(10);
/*********************第六位數(shù)碼管*********************/
P2 = b[5];
P0 = 0x40;
Delay(10);
/*********************第七位數(shù)碼管*********************/
P2 = b[6];
P0 = a[second / 10];
Delay(10);
/*********************第八位數(shù)碼管*********************/
P2 = b[7];;
P0 = a[second % 10];
Delay(10);
}
/*********************年月日顯示函數(shù)********************/
void Dispaly2(uChar day, uChar month, uChar year)
{
P2 = b[0];
P0 = a[day / 10];
Delay(10);
P2 = b[1];
P0 = a[day % 10];
Delay(10);
P2 = b[2];
P0 = 0x40;
Delay(10);
P2 = b[3];
P0 = a[month / 10];
Delay(10);
P2 = b[4];
P0 = a[month % 10];
Delay(10);
P2 = b[5];
P0 = 0x40;
Delay(10);
P2 = b[6];
P0 = a[year / 10];
Delay(10);
P2 = b[7];
P0 = a[year % 10];
Delay(10);
}
/*********************時鐘按鍵掃描函數(shù)*********************/
void Keyscan1()
{
static uChar i = 0, j = 0;
if (Key1 == 0)
{
Delay(10); //消抖
if (Key1 == 0)
while (!Key1); //等待按鍵彈
i++;
}
/*時鐘暫停功能*/
if (i % 2 == 1)
{
TR0 = 0;/*如果是奇數(shù)次按鍵自然關閉定時器0*/
}
if (i % 2 == 0)
{
TR0 = 1;/*如果是偶數(shù)次按鍵則打開定時器0*/
}
/*時鐘調(diào)位和數(shù)值加一功能*/
if (Key2 == 0)
{
Delay(10);
if (Key2 == 0)
while (!Key2);
j++;
}
if (j % 4 == 1)
{
if (Key3 == 0)
{
Delay(10);
if (Key3 == 0)
while (!Key3);
second++;
if (second == 60)
second = 0;
}
}
if (j % 4 == 2)
{
if (Key3 == 0)
{
Delay(10);
if (Key3 == 0)
while (!Key3);
minute++;
if (minute == 60)
minute = 0;
}
}
if (j % 4 == 3)
{
if (Key3 == 0)
{
Delay(10);
if (Key3 == 0)
while (!Key3);
hour++;
if (hour == 24)
hour = 0;
}
}
}
/*日期按鍵掃描函數(shù)*/
void Keyscan2()
{
static uChar m = 0, n = 0;
if (Key1 == 0)
{
Delay(10);
if (Key1 == 0)
while (!Key3);
m++;
}
if (m % 2 == 1)
{
TR0 = 0;/*奇數(shù)次按鍵則關閉定時器0*/
}
if (m % 2 == 0)
{
TR0 = 1;/*偶數(shù)次按鍵則打開定時器0*/
}
if (Key2 == 0)
{
Delay(10);
if (Key2 == 0)
while (!Key2);
n++;
}
/*日期調(diào)位和日期加一功能*/
if (n % 4 == 1)
{
if (Key3 == 0)
{
Delay(10);
if (Key3 == 0)
while (!Key3);
day++;
if (day == 30)
day = 0;
}
}
if (n % 4 == 2)
{
if (Key3 == 0)
{
Delay(10);
if (Key3 == 0)
while (!Key3);
month++;
if (month == 12)
month = 0;
}
}
if (n % 4 == 3)
{
if (Key3 == 0)
{
Delay(10);
if (Key3 == 0)
while (!Key3);
year++;
if (year == 99)
year = 0;
}
}
}
/************************************************/
/***************主函數(shù)***************************/
/************************************************/
void main()
{
TMOD = 0x01; /*定時器以方式一工作*/
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;/*10ms計時*/
EA = 1;
ET0 = 1;/*允許定時器0中斷*/
TR0 = 1;/*打開定時器0*/
while (1)
{
static uChar h = 0;
/*時鐘和日期切換功能*/
if (Key4 == 0)
{
Delay(10);
if (Key4 == 0)
while (!Key4);
h++;
}
if (h % 2 == 0)/*如果按鍵偶數(shù)次則顯示時鐘*/
{
Dispaly1(second, minute, hour);
Keyscan1();
}
if (h % 2 == 1)/*如果按鍵奇數(shù)次則顯示日期*/
{
Dispaly2(year, month, day);
Keyscan2();
}
}
}
/**********************中斷函數(shù)**************************/
void time0_int(void) interrupt 1
{
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
count++;
if (count == 100)/*10ms??ê±£???100′??ò??o?1s*/
{
count = 0;
second++;
if (second == 60)
{
second = 0;
minute++;
if (minute == 60)
{
minute = 0;
hour++;
if (hour == 24)
{
hour = 0;
day++;
if (day == 30)
{
day = 0;
month++;
if (month == 12)
{
month = 0;
year++;
if (year == 99)
{
year = 0;
}
}
}
}
}
}
}
/*判斷整點提醒*/
if (second == 00 && minute == 00)
Buzzer = 0;
else
Buzzer = 1;
}
|
|