📄 main.c
字号:
#ifndef __AT89X51_H__
#include <at89x51.h>
#endif
#include <lcd.h>
#include <stdio.h>
sbit SET = P1^3;
sbit ON = P1^4;
sbit BEEP = P1^5;
int ms = 0;
unsigned char sec = 0;
char ptime[16];
int state = 0;
bit stopwatch = 0; //功能开关变量
bit alarm = 0;
bit count = 0;
bit time_up_key = 0; //提示开关变量
int hour = 12, minuter = 0, second = 0, msecond =0; //标准时间
int stopw_minuter = 0, stopw_second = 0, stopw_msec = 0; //秒表时间
int count_hour = 0, count_minuter=0, count_second = 0; //倒计时时间
int alarm_hour = 0, alarm_minuter = 0; //闹钟时间
void init()
{
TMOD = TMOD | 0x02;
TH0 = 56;
TL0 = 0;
IE = 0x9f;
IT1 = 1;
IT0 = 1;
TR0 = 1;
}
void time_up()
{
unsigned char i;
for (i=200; i>0; i--)
; //调用音乐程序!!
BEEP = !BEEP;
}
/*设置各种状态下时间小时位 或 清零时分秒位*/
void key_hour_add () interrupt 0
{
switch( state )
{
case 0 : hour = (++hour) % 24;
case 1 : alarm_hour = (++alarm_hour) % 24;
case 2 : {
if( !count )
{
count_hour = (++count_hour) % 24;
}
else
{
count_hour = 0;
count_minuter = 0;
count_second = 0;
}
}
case 3 : {
stopw_minuter = 0;
stopw_second = 0;
stopw_msec = 0;
} ;
}
}
/*设置各种状态下分钟位 或 清零时分秒位*/
void key_minuter_add () interrupt 2
{
switch (state)
{
case 0 : minuter = (++minuter) % 60;
case 1 : alarm_minuter = (++alarm_minuter) % 60;
case 2 : {
if ( !count )
{
count_minuter = (++count_minuter) % 60;
}
else
{
count_hour = 0;
count_minuter = 0;
count_second = 0;
}
}
case 3 : {
stopw_minuter = 0;
stopw_second = 0;
stopw_msec = 0;
}
}
}
void run_time()
{
if (23 == hour&&59==minuter&&59==second)
{
hour = 0;
minuter =0;
second = 0;
}
else if (59==minuter&&59==second)
{
hour++;
minuter = 0;
second = 0;
}
else if (59 == second)
{
minuter++;
second = 0;
}
else
{
second++;
}
}
void run_stopwatch()
{
if (59==stopw_minuter&&59 ==stopw_second&&9 ==stopw_msec)
{
stopw_minuter = 0;
stopw_second = 0;
stopw_msec = 0;
}
else if (59==stopw_second&&9==stopw_msec)
{
stopw_minuter++;
stopw_second = 0;
stopw_msec = 0;
}
else if (9==stopw_msec)
{
stopw_second++;
stopw_msec = 0;
}
else
{
stopw_msec++;
}
}
void run_count()
{
if (0==count_hour&&0==count_minuter&&0 ==count_second )
{
time_up_key = 1;
count = !count;
}
else if (0==count_minuter&&0==count_second )
{
count_hour--;
count_minuter = 59;
count_second = 59;
}
else if (0==count_second )
{
count_minuter--;
count_second = 59;
}
else
{
count_second--;
}
}
void run_alarm()
{
if (alarm_hour==hour&&alarm_minuter==minuter ) //判断闹钟
{
time_up_key = 1;
alarm = !alarm;
}
}
/*刷新屏幕时间*/
void flush_src()
{
switch (state)
{
case 0 :{
sprintf(ptime,"******time******");
lcd_write_str(ptime, 1, 0);
sprintf(ptime," %02d:%02d:%02d ", hour, minuter, second);
lcd_write_str(ptime, 2, 0);
break;
}
case 1 :{ if (alarm)
sprintf(ptime,"****alarm on****");
else
sprintf(ptime,"****alarm off***");
lcd_write_str(ptime, 1, 0);
sprintf(ptime," %02d:%02d ", alarm_hour, alarm_minuter);
lcd_write_str(ptime, 2, 0);
break;
}
case 2 :{
if (count)
sprintf(ptime,"****count on****");
else
sprintf(ptime,"****count off***");
lcd_write_str(ptime, 1, 0);
sprintf(ptime," %02d:%02d:%02d ", count_hour, count_minuter, count_second);
lcd_write_str(ptime, 2, 0);
break;
}
case 3 :{
if (stopwatch)
sprintf(ptime,"*stopwatch on***");
else
sprintf(ptime,"*stopwatch off*");
lcd_write_str(ptime, 1, 0);
sprintf(ptime, " %02d:%02d:%1d ", stopw_minuter, stopw_second, stopw_msec );
lcd_write_str(ptime, 2, 0);
break;
}
}
}
void time0 ()interrupt 1
{
ms++;
if (500 == ms) // 100ms到
{
if(1 == stopwatch)
run_stopwatch();
sec++;
if (10 == sec) //1秒到
{
run_time();
if ( 1 == count )
run_count();
if ( 1 == alarm )
run_alarm();
sec = 0;
}
flush_src();
ms = 0;
}
}
/* set 按钮功能函数 */
void set_state_key()
{
unsigned char i = 100, j = 100;
SET = 1;
if (0 == SET)
{
for (i=200; i>0; i--)
for (j=200; j>0; j--)
;
if (0 == SET)
{
state = (++state) % 4;
}
}
}
/* on/off 按钮功能函数 */
void set_on_key()
{
char i = 100, j = 100;
ON = 1;
if (0 == ON)
{
for (i=200; i>0; i--)
for (j=200; j>0; j--)
;
if (0 == ON)
{
if (1 == time_up_key)
{
time_up_key = 0;
return ;
}
switch (state)
{
case 0 : ;
case 1 : {
alarm = !alarm;
break;
}
case 2 : {
count = !count;
break;
}
case 3 : {
stopwatch = !stopwatch;
break;
}
}
}
}
}
void main()
{
lcd_init();
init();
while(1)
{
set_state_key();
set_on_key();
if (time_up_key)
{
time_up();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -