📄 calender.c
字号:
//@@***********************************************************
//-------------------------------------------------------------
//
// 文件名 :Calender.c
// 描述 :日历时钟模块,用于不带时钟芯片的系统
// 语言 :C
// 作者 :Jean J. Labrosse
// 修改 :李艳平
// 日期 :2002-05-17
// 说明 :原来的程序用于UCII_RTOS中,现移植C51中
//@@***********************************************************
#define SYS_CLOCK_EN 1
#if SYS_CLOCK_EN
#include "typedef.h"
#include "reg52.h"
#include "string.h"
#include "stdlib.h"
#include "Stdio.h"
#define FOSC 22118400L
#define MACHINE_NUM 12 /*根据需要调整,如:W777E58=4*/
#define TICKS_PER_SEC 10 /* # of clock ticks to obtain 1 second */
#define CLK_DATE_EN 1 /* Enable DATE (when 1) */
#define ENTER_CRITICAL() EA=1
#define EXIT_CRITICAL() EA=0
#if CLK_DATE_EN
typedef struct clk_month
{
UCHAR MonthDays; /* Number of days in each month */
char *MonthName; /* Name of the month */
UCHAR MonthVal; /* Value used to compute day of the week */
} CLK_MONTH;
#endif
static UCHAR ClkTickCtr; /* Counter used to keep track of system clock ticks */
static UCHAR ClkHr;
static UCHAR ClkMin;
static UCHAR ClkSec; /* Counters for local TIME
*/
#if CLK_DATE_EN
static UCHAR ClkDay; /* Counters for local DATE */
static UCHAR ClkDOW; /* Day of week (0 is Sunday) */
static UCHAR ClkMonth;
static UINT ClkYear;
#endif
/**********************************************************************************************************
* FUNCTION PROTOTYPES
**********************************************************************************************************/
static BOOL ClkUpdateTime(void);
#if CLK_DATE_EN
static BOOL ClkIsLeapYear(UINT year);
static void ClkUpdateDate(void);
static void ClkUpdateDOW(void);
#endif
void Init_sys_timer(void);
void Sys_clk_init(void);
void Sys_clk_task(void);
void Clk_format_time(UCHAR mode, char *s);
void Clk_set_time(UCHAR hr, UCHAR min, UCHAR sec);
int Cmp_now_time(UCHAR hr, UCHAR min, UCHAR sec);
#if CLK_DATE_EN
void Clk_format_date(UCHAR mode, char *s);
void Clk_set_date(UCHAR month, UCHAR day, UINT year);
void Clk_set_date_time(UCHAR month, UCHAR day, UINT year, UCHAR hr, UCHAR min, UCHAR sec);
int Cmp_now_date_time(UCHAR month, UCHAR day, UINT year, UCHAR hr, UCHAR min, UCHAR sec);
#endif
/*********************************************************************************************************/
#if CLK_DATE_EN
static char code *ClkDOWTbl[] = { /* NAME FOR EACH DAY OF THE WEEK */
"Sunday ",
"Monday ",
"Tuesday ",
"Wednesday ",
"Thursday ",
"Friday ",
"Saturday "
};
static CLK_MONTH code ClkMonthTbl[] = { /* MONTHS TABLE */
{0, "", 0}, /* Invalid month */
{31, "January ", 6}, /* January */
{28, "February ", 2}, /* February (note leap years are handled by code) */
{31, "March ", 2}, /* March */
{30, "April ", 5}, /* April */
{31, "May ", 0}, /* May */
{30, "June ", 3}, /* June */
{31, "July ", 5}, /* July */
{31, "August ", 1}, /* August */
{30, "September ", 4}, /* September */
{31, "October ", 6}, /* October */
{30, "November ", 2}, /* November */
{31, "December ", 4} /* December */
};
#endif
/*
*********************************************************************************************************
* TIME-OF-DAY CLOCK TASK
*
* Description : This task is created by Sys_clk_init() and is responsible for updating the time and date.
* Sys_clk_task() executes every second.
* Arguments : None.
* Returns : None.
* Notes : CLK_DLY_TICKS must be set to produce 1 second delays.
*********************************************************************************************************
*/
void Sys_clk_task (void)
{
if(++ClkTickCtr==TICKS_PER_SEC)
{
ClkTickCtr=0;
if (ClkUpdateTime() == TRUE)
{
#if CLK_DATE_EN
ClkUpdateDate(); /* And date if a new day (i.e. MM-DD-YY) */
#endif
}
}
}
//@@***********************************************************
//
// 功能: 系统时钟中断服务函数
// 函数: void Sys_tick_isr(void)
// 语言: C
// 输入: NONE
// 输出: NONE
// 作者: 李艳平
// 日期: 2002-05-17
//
//@@***********************************************************
//-------------------------------------------------------------
void Sys_tick_isr(void) interrupt 5 using 3
{
TF2=0;
Sys_clk_task();
}
//@@***********************************************************
//
// 功能: 系统定时器初始化
// 函数: void Vtimer_init(void)
// 语言: C
// 输入: NONE
// 输出: NONE
// 作者: 李艳平
// 日期: 2002-05-17
//
//@@***********************************************************
//-------------------------------------------------------------
void Init_sys_timer(void)
{
BYTE_WORD Init_value;
Sys_clk_init();
T2CON=0x00;
Init_value.word=FOSC/(TICKS_PER_SEC*MACHINE_NUM);
TH2=RCAP2H=-Init_value.bytes.high;
TL2=RCAP2L=-Init_value.bytes.low;
ET2=1;
TR2=1;
//TMOD&=0xF0; //使用定时器0,但在中断中需要重新赋初值,因而我更倾向用Timer2
//TMOD|=0x01;
// Init_value.word=FOSC/(TICKS_PER_SEC*MACHINE_NUM);
//TH0=-Init_value.bytes.high;
//TL0=-Init_value.bytes.low;
//ET0=1;
//TR0=1;
}
/*
*********************************************************************************************************
* TIME MODULE INITIALIZATION
* TIME-OF-DAY CLOCK INITIALIZATION
*
* Description : This function initializes the time module. The time of day clock task will be created
* by this function.
* Arguments : None
* Returns : None.
*********************************************************************************************************
*/
void Sys_clk_init (void)
{
ClkTickCtr = 0;
Clk_set_time(11,32,0);
#if CLK_DATE_EN
Clk_set_date(5,28,2002);
#endif
}
/*
*********************************************************************************************************
* FORMAT CURRENT DATE INTO STRING
*
* Description : Formats the current date into an ASCII string.
* Arguments : mode is the format type:
* 1 will format the time as "MM-DD-YY" (needs at least 9 characters)
* 2 will format the time as "Day Month DD, YYYY" (needs at least 30 characters)
* 3 will format the time as "YYYY-MM-DD" (needs at least 11 characters)
* s is a pointer to the destination string. The destination string must be large
* enough to hold the formatted date.
* contain
* Returns : None.
* Notes : - A 'switch' statement has been used to allow you to add your own date formats. For
* example, you could display the date in French, Spanish, German etc. by assigning
* numbers for those types of conversions.
* - This function assumes that strcpy(), strcat() and itoa() are reentrant.
*********************************************************************************************************
*/
#if CLK_DATE_EN
void Clk_format_date (UCHAR mode, char *s)
{
UINT year;
char str[5];
switch (mode)
{
case 1:
strcpy(s, "MM-DD-YY"); /* Create the template for the selected format */
s[0] = ClkMonth / 10 + '0'; /* Convert DATE to ASCII */
s[1] = ClkMonth % 10 + '0';
s[3] = ClkDay / 10 + '0';
s[4] = ClkDay % 10 + '0';
year = ClkYear % 100;
s[6] = year / 10 + '0';
s[7] = year % 10 + '0';
break;
case 2:
strcpy(s, ClkDOWTbl[ClkDOW]); /* Get the day of the week */
strcat(s, ClkMonthTbl[ClkMonth].MonthName); /* Get name of month */
if (ClkDay < 10)
{
str[0] = ClkDay + '0';
str[1] = 0;
}
else
{
str[0] = ClkDay / 10 + '0';
str[1] = ClkDay % 10 + '0';
str[2] = 0;
}
strcat(s, str);
strcat(s, ", ");
sprintf(str,"%d",ClkYear);
strcat(s, str);
break;
case 3:
strcpy(s, "YYYY-MM-DD"); /* Create the template for the selected format */
s[0] = year / 1000 + '0';
year = year % 1000;
s[1] = year / 100 + '0';
year = year % 100;
s[2] = year / 10 + '0';
s[3] = year % 10 + '0';
s[5] = ClkMonth / 10 + '0'; /* Convert DATE to ASCII */
s[6] = ClkMonth % 10 + '0';
s[8] = ClkDay / 10 + '0';
s[9] = ClkDay % 10 + '0';
break;
default:
strcpy(s, "?");
break;
}
}
#endif
/*
*********************************************************************************************************
* FORMAT CURRENT TIME INTO STRING
*
* Description : Formats the current time into an ASCII string.
* Arguments : mode is the format type:
* 1 will format the time as "HH:MM:SS" (24 Hour format)
* (needs at least 9 characters)
* 2 will format the time as "HH:MM:SS AM" (With AM/PM indication)
* (needs at least 13 characters)
* s is a pointer to the destination string. The destination string must be large
* enough to hold the formatted time.
* contain
* Returns : None.
* Notes : - A 'switch' statement has been used to allow you to add your own time formats.
* - This function assumes that strcpy() is reentrant.
*********************************************************************************************************
*/
void Clk_format_time (UCHAR mode, char *s)
{
UCHAR hr;
switch (mode)
{
case 1:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -