📄 test.c
字号:
//
//
//
#include "reg52.h"
#include "lcd.h"
#include "common.h"
#include "gdi.h"
#include "key.h"
#include "timer.h"
#include "serial.h"
#include "i2c.h"
sbit P1_6 = P1^6;
sbit P1_7 = P1^7;
sbit P3_3 = P3^3;
bit AlarmFlag = 1;
#define ALARM_HOUR_ADDR 0
#define ALARM_MINUTE_ADDR 1
enum {RS_NULL, RS_DISTIME, RS_DATESETUP, RS_TIMESETUP, RS_SECCONUTER, RS_UART, RS_ARALMSETUP, RS_ARALM, RS_OTHER};
S_BYTE code WeekStr[7][10] = {" Monday ", " Tuesday ", "Wednesday", "Thursday ", " Friday ", "Saturday ", " Sunday "};
S_BYTE code RunState[] = {RS_NULL, RS_DISTIME, RS_DATESETUP, RS_TIMESETUP, RS_SECCONUTER, RS_UART, RS_ARALMSETUP, RS_ARALM, RS_OTHER};
extern S_CHAR code DaysOfMon[2][12];
void SystemInit(void)
{
P0 = 0xFF;
P1 = 0xFF;
P2 = 0xFF;
P3 = 0xFF;
P3_3 = 0;//speaker
InitDateTime();
LcdInit();
InitTimer0();
}
S_VOID Draw2Digit(S_BYTE x, S_BYTE y, S_BYTE num)
{
if (num < 10)
{
DrawText(x, y, "0");
DrawNum(x+6, y, num);
}
else
{
DrawNum(x, y, num);
}
}
S_VOID DrawFrame(S_VOID)
{
DrawRect(0, 0, LCDWIDTH-1, LCDHEIGHT-1, 1);
DrawRect(1, 1, LCDWIDTH-2, LCDHEIGHT-2, 1);
}
S_BYTE DisplayDateTime(S_VOID)
{
bit tm;
S_BYTE idata key;
S_BYTE idata sec=127, min=127, hour=127, day=127, mon=127, year=127;
S_BYTE idata AlarmHour, AlarmMin;
AlarmHour = I2C_ReadChar(ALARM_HOUR_ADDR);
AlarmMin = I2C_ReadChar(ALARM_MINUTE_ADDR);
if (AlarmHour > 23) AlarmHour = 23;
if (AlarmMin > 59) AlarmMin = 59;
ClearScrn();
DrawFrame();
DrawText(22, 4, "-- CALENDAR --");
DrawText(58, 19, "-");
DrawText(76, 19, "-");
DrawText(52, 47, ":");
DrawText(70, 47, ":");
tm = SetTextMode(TM_OPAQUE);
while (1)
{
key = GetKey();
if (key > KEY_BEGIN && key < KEY_UP)
break;
if (year != GetYear())
{
year = GetYear();
DrawNum(34, 19, year + BASEYEAR);
}
if (mon != GetMon())
{
mon = GetMon();
Draw2Digit(64, 19, mon);
}
if (day != GetDay())
{
S_WORD idata _year = year + BASEYEAR;
day = GetDay();
Draw2Digit(82, 19, day);
//week
if(mon == 1 || mon == 2)
{
mon += 12;
_year--;
}
day = (day + 2*mon + 3*(mon+1)/5 + _year + _year/4 - _year/100 + _year/400) % 7;
DrawText(36, 33, WeekStr[day]);
day = GetDay();
mon = GetMon();
}
if (hour != GetHour())
{
hour = GetHour();
Draw2Digit(40, 47, hour);
}
if (min != GetMinute())
{
min = GetMinute();
Draw2Digit(58, 47, min);
}
if (sec != GetSecond())
{
sec = GetSecond();
Draw2Digit(76, 47, sec);
if (hour == AlarmHour && min == AlarmMin && !sec && AlarmFlag)
{
key = KEY_UP;
break;
}
}
}
SetTextMode(tm);
return RunState[key+1];
}
S_VOID DateSetup(S_VOID)
{
bit leap=0, tm;
S_BYTE idata fc, bc;
S_BYTE idata index, key;
S_BYTE idata day, mon;
S_WORD idata year;
index = 0;
day = GetDay();
mon = GetMon();
year = GetYear() + BASEYEAR;
if (year % 4 == 0)
{
if ((year % 100 != 0) || (year % 400 == 0))
leap = 1;
}
ClearScrn();
DrawFrame();
DrawText(16, 4, "-- DATE SETUP --");
DrawText(58, 33, "-");
DrawText(76, 33, "-");
Draw2Digit(82, 33, day);
Draw2Digit(64, 33, mon);
tm = SetTextMode(TM_OPAQUE);
fc = SetTextColor(COLOR_WHITE);
bc = SetBackgroundColor(COLOR_BLACK);
DrawNum(34, 33, year);
while (1)
{
key = GetKey();
switch (key)
{
case KEY_UP:
if (index == 0)
{
if (year > BASEYEAR)
{
--year;
DrawNum(34, 33, year);
}
}
else if (index == 1)
{
if (mon > 1)
{
--mon;
Draw2Digit(64, 33, mon);
}
}
else
{
if (day > 1)
{
--day;
Draw2Digit(82, 33, day);
}
}
break;
case KEY_DOWN:
if (index == 0)
{
if (year < BASEYEAR+60)
{
++year;
DrawNum(34, 33, year);
}
}
else if (index == 1)
{
if (mon < 12)
{
++mon;
Draw2Digit(64, 33, mon);
}
}
else
{
if (day < DaysOfMon[leap][mon-1])
{
++day;
Draw2Digit(82, 33, day);
}
}
break;
case KEY_LEFT:
if (index == 1)
{
index = 0;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(64, 33, mon);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
DrawNum(34, 33, year);
}
else if (index == 2)
{
index = 1;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(82, 33, day);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(64, 33, mon);
}
break;
case KEY_RIGHT:
if (index == 0)
{
index = 1;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
DrawNum(34, 33, year);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(64, 33, mon);
}
else if (index == 1)
{
index = 2;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(64, 33, mon);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(82, 33, day);
}
break;
case KEY_OK:
SetDay(day);
SetMon(mon);
year -= BASEYEAR;
SetYear(year);
break;
default:
break;
}
if (key == KEY_OK || key == KEY_CANCEL)
break;
}
SetTextMode(tm);
SetTextColor(fc);
SetBackgroundColor(bc);
}
S_VOID TimeSetup(S_VOID)
{
bit tm;
S_BYTE idata fc, bc;
S_BYTE idata index, key;
S_BYTE idata hour, minute, second;
index = 0;
hour = GetHour();
minute = GetMinute();
second = GetSecond();
ClearScrn();
DrawFrame();
DrawText(16, 4, "-- TIME SETUP --");
DrawText(52, 33, ":");
DrawText(70, 33, ":");
Draw2Digit(76, 33, second);
Draw2Digit(58, 33, minute);
tm = SetTextMode(TM_OPAQUE);
fc = SetTextColor(COLOR_WHITE);
bc = SetBackgroundColor(COLOR_BLACK);
Draw2Digit(40, 33, hour);
while (1)
{
key = GetKey();
switch (key)
{
case KEY_UP:
if (index == 0)
{
if (hour > 0)
{
--hour;
Draw2Digit(40, 33, hour);
}
}
else if (index == 1)
{
if (minute > 0)
{
--minute;
Draw2Digit(58, 33, minute);
}
}
else
{
if (second > 0)
{
--second;
Draw2Digit(76, 33, second);
}
}
break;
case KEY_DOWN:
if (index == 0)
{
if (hour < 23)
{
++hour;
Draw2Digit(40, 33, hour);
}
}
else if (index == 1)
{
if (minute < 59)
{
++minute;
Draw2Digit(58, 33, minute);
}
}
else
{
if (second < 59)
{
++second;
Draw2Digit(76, 33, second);
}
}
break;
case KEY_LEFT:
if (index == 1)
{
index = 0;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(58, 33, minute);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(40, 33, hour);
}
else if (index == 2)
{
index = 1;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(76, 33, second);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(58, 33, minute);
}
break;
case KEY_RIGHT:
if (index == 0)
{
index = 1;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(40, 33, hour);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(58, 33, minute);
}
else if (index == 1)
{
index = 2;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(58, 33, minute);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(76, 33, second);
}
break;
case KEY_OK:
SetSecond(second);
SetMinute(minute);
SetHour(hour);
break;
default:
break;
}
if (key == KEY_OK || key == KEY_CANCEL)
break;
}
SetTextMode(tm);
SetTextColor(fc);
SetBackgroundColor(bc);
}
S_VOID SecondCounter(S_VOID)
{
#if 0
bit begin = 0;
bit tm;
S_BYTE idata key;
S_WORD idata counter=0;
S_BYTE idata minute=0, second=0, ms=0;
ClearScrn();
DrawFrame();
DrawText(18, 4, "-- SEC CONUT --");
DrawText(52, 33, ":");
DrawText(70, 33, ":");
Draw2Digit(76, 33, ms);
Draw2Digit(58, 33, second);
Draw2Digit(40, 33, minute);
tm = SetTextMode(TM_OPAQUE);
while (1)
{
key = GetKey();
switch (key)
{
case KEY_OK:
begin = !begin;
if (begin)
SetTimeTick(counter);
break;
case KEY_CANCEL:
if (!begin)
{
counter = 0;
ms = 0;
second = 0;
minute = 0;
Draw2Digit(76, 33, ms);
Draw2Digit(58, 33, second);
Draw2Digit(40, 33, minute);
}
break;
default:
break;
}
if (begin && counter != GetTimeTick())
{
counter = GetTimeTick();
ms = (S_BYTE)(counter%20);
Draw2Digit(76, 33, ms*5);
counter /= 20;
if (second != (S_BYTE)(counter%60))
{
second = (S_BYTE)(counter%60);
Draw2Digit(58, 33, second);
}
counter /= 60;
if ((S_BYTE)counter != minute)
{
minute = (S_BYTE)counter;
Draw2Digit(40, 33, minute);
}
}
if (key == KEY_UP && !begin)
break;
}
SetTextMode(tm);
#endif
}
S_VOID AlarmSetup(S_VOID)
{
bit tm;
S_BYTE idata fc, bc;
S_BYTE idata index, key;
S_BYTE idata hour, minute;
index = 0;
hour = I2C_ReadChar(ALARM_HOUR_ADDR);
minute = I2C_ReadChar(ALARM_MINUTE_ADDR);
hour = hour>23? 23:hour;
minute = minute>59? 59:minute;
ClearScrn();
DrawFrame();
DrawText(13, 4, "-- ALARM SETUP --");
DrawText(64, 33, ":");
Draw2Digit(70, 33, minute);
tm = SetTextMode(TM_OPAQUE);
fc = SetTextColor(COLOR_WHITE);
bc = SetBackgroundColor(COLOR_BLACK);
Draw2Digit(52, 33, hour);
while (1)
{
key = GetKey();
switch (key)
{
case KEY_UP:
if (index == 0)
{
if (hour > 0)
{
--hour;
Draw2Digit(52, 33, hour);
}
}
else
{
if (minute > 0)
{
--minute;
Draw2Digit(70, 33, minute);
}
}
break;
case KEY_DOWN:
if (index == 0)
{
if (hour < 23)
{
++hour;
Draw2Digit(52, 33, hour);
}
}
else
{
if (minute < 59)
{
++minute;
Draw2Digit(70, 33, minute);
}
}
break;
case KEY_LEFT:
if (index == 1)
{
index = 0;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(70, 33, minute);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(52, 33, hour);
}
break;
case KEY_RIGHT:
if (index == 0)
{
index = 1;
SetTextColor(COLOR_BLACK);
SetBackgroundColor(COLOR_WHITE);
Draw2Digit(52, 33, hour);
SetTextColor(COLOR_WHITE);
SetBackgroundColor(COLOR_BLACK);
Draw2Digit(70, 33, minute);
}
break;
case KEY_OK:
I2C_WriteChar(ALARM_HOUR_ADDR,hour);
I2C_WriteChar(ALARM_MINUTE_ADDR,minute);
AlarmFlag = 1;
break;
default:
break;
}
if (key == KEY_OK || key == KEY_CANCEL)
break;
}
SetTextMode(tm);
SetTextColor(fc);
SetBackgroundColor(bc);
}
S_VOID AlarmPrompt(S_VOID)
{
S_BYTE idata key;
S_WORD idata cnt = 0xFFF;
ClearScrn();
DrawFrame();
DrawText(10, 4, "-- ALARM PROMPT --");
P3_3 = 1;
while ((key = GetKey()) == KEY_NULL)
{
cnt--;
if (!cnt)
break;
}
P3_3 = 0;
AlarmFlag = 0;
}
S_VOID SerialComm(S_VOID)
{
S_BYTE idata key;
S_BYTE idata buf[6];
UART_Init();
ClearScrn();
DrawFrame();
DrawText(34, 4, "-- UART --");
UART_SendStr("calendar 1.0\n");
UART_SendStr("jinhailiao@163.com\n");
UART_SendStr("command:D, get date\n");
UART_SendStr("command:T, get time\n");
while (GetKey() == KEY_NULL)
{
if (RI)
{
key = UART_ReceiveChar();
DrawText(4, 30, "receive:");
}
if (key == 'D' || key == 'd')
{
DrawText(40, 30, "D");
Word2Str(buf, GetYear()+BASEYEAR);
UART_SendStr(buf);
buf[0] = '-';
Word2Str(&buf[1], GetMon());
UART_SendStr(buf);
Word2Str(&buf[1], GetDay());
UART_SendStr(buf);
UART_SendStr("\r\n");
}
if (key == 'T' || key == 't')
{
DrawText(40, 30, "T");
Word2Str(buf, GetHour());
UART_SendStr(buf);
buf[0] = ':';
Word2Str(&buf[1], GetMinute());
UART_SendStr(buf);
Word2Str(&buf[1], GetSecond());
UART_SendStr(buf);
UART_SendStr("\r\n");
}
}
UART_Uninit();
}
void main()
{
S_BYTE idata RunStatus = RS_DISTIME;
SystemInit();
while (1)
{
switch (RunStatus)
{
case RS_DISTIME:
RunStatus = DisplayDateTime();
break;
case RS_DATESETUP:
DateSetup();
RunStatus = RS_DISTIME;
break;
case RS_TIMESETUP:
TimeSetup();
RunStatus = RS_DISTIME;
break;
case RS_SECCONUTER:
SecondCounter();
RunStatus = RS_DISTIME;
break;
case RS_ARALMSETUP:
AlarmSetup();
RunStatus = RS_DISTIME;
break;
case RS_ARALM:
AlarmPrompt();
RunStatus = RS_DISTIME;
break;
case RS_UART:
SerialComm();
RunStatus = RS_DISTIME;
break;
default:
RunStatus = RS_DISTIME;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -