误差不算大,有误差修正参数就行了。
//@@*********************************************************** //------------------------------------------------------------- // // 文件名 :Calender.c // 描述 :日历时钟模块,用于不带时钟芯片的系统 // 语言 :C // 作者 :Jean J. Labrosse // 修改 :李艳平 // 日期 :2002-05-17 // 说明 :原来的程序用于UCII_RTOS中,现移植C51中 //@@***********************************************************
//备注: //盛唐电子公司移植到EPS112仿真编程器,用于显示测试与数码管试验。 //完成日期:07-04-12 //联系方式:Xg_2004_sy@126.com (025)51615878 13057504110
#define SYS_CLOCK_EN 1
#if SYS_CLOCK_EN
#include "Caltypedef.h" //#include "string.h" //#include "stdlib.h" //#include "Stdio.h" #include "..\inc\includes.h"
//#include "DriverComm.h" //#include "HardResource.h" //#define FOSC 22118400L //#define FOSC 11059200L #define FOSC 18432000L
#define MACHINE_NUM 24 /*根据需要调整,如:W777E58=4*/ #define TICKS_PER_SEC 80 /* # of clock ticks to obtain 1 second */ //#define TICKS_PER_SEC 10 //Only for Test
//#define CLK_DATE_EN 1 /* Enable DATE (when 1) */
#define ENTER_CRITICAL() EA=1 #define EXIT_CRITICAL() EA=0
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 */ /********************************************************************************************************** * FUNCTION PROTOTYPES **********************************************************************************************************/ static BOOL ClkUpdateTime(void);
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);
/*********************************************************************************************************/ void Set_LEDFlashDot_On(void); void Set_LEDFlashDot_Off(void);
/* ********************************************************************************************************* * 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. ********************************************************************************************************* */ extern uchar ucDispTemperatureCount; extern bool bDispTemperatureSwitch; #define c_DispTemperatureIntervalMinute 10 //显示温度的间隔时间(以分钏钟为单位) //功能:时钟中断任务 static void Sys_clk_task (void) { static bool bFlashFlag;
if(++ClkTickCtr==TICKS_PER_SEC) { ClkTickCtr=0; if (ClkUpdateTime() == TRUE) { }
if(bFlashFlag) { Set_LEDFlashDot_On(); } else { Set_LEDFlashDot_Off(); } bFlashFlag = !bFlashFlag;
if(ucDispTemperatureCount > 0) { ucDispTemperatureCount--; }
} }
uchar ucFlagEnableClock; //全局变量是否开启时钟的标志
//@@*********************************************************** // // 功能: 系统时钟中断服务函数 // 函数: void Sys_tick_isr(void) // 语言: C // 输入: NONE // 输出: NONE // 作者: 李艳平 // 日期: 2002-05-17 // //@@*********************************************************** //------------------------------------------------------------- static void Sys_tick_isr(void) interrupt 5 using 3 { TF2=0; // P2_0 = 0; /* if(ucFlagEnableClock == 0) { RunLedPin = ~RunLedPin; //没有闹钟时闪烁 } else { RunLedPin = 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; //定时器2使用机器周期作为计数脉冲(Fosc/12) //Init_value.word=FOSC/((ulong)TICKS_PER_SEC*MACHINE_NUM); Init_value.word=38400; TH2=RCAP2H=-Init_value.bytes.high; TL2=RCAP2L=-Init_value.bytes.low;
// ComSendTxt ("TH2Value", &Init_value, 2);
//启动定时器2工作 ET2=1; TR2=1; //开中断总开关 ENTER_CRITICAL(); //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. ********************************************************************************************************* */ //功能:系统时钟初始化 static void Sys_clk_init (void) { ClkTickCtr = 0; Clk_set_time(0,0,0); } /* ********************************************************************************************************* * 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. ********************************************************************************************************* */ /* ********************************************************************************************************* * 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: strcpy(s, "HH:MM:SS"); /* Create the template for the selected format */ s[0] = ClkHr / 10 + '0'; /* Convert TIME to ASCII */ s[1] = ClkHr % 10 + '0'; s[3] = ClkMin / 10 + '0'; s[4] = ClkMin % 10 + '0'; s[6] = ClkSec / 10 + '0'; s[7] = ClkSec % 10 + '0'; break;
case 2: strcpy(s, "HH:MM:SS AM"); /* Create the template for the selected format */ s[9] = (ClkHr >= 12) ? 'P' : 'A'; /* Set AM or PM indicator */ if (ClkHr > 12) { /* Adjust time to be displayed */ hr = ClkHr - 12; } else { hr = ClkHr; } s[0] = hr / 10 + '0'; /* Convert TIME to ASCII */ s[1] = hr % 10 + '0'; s[3] = ClkMin / 10 + '0'; s[4] = ClkMin % 10 + '0'; s[6] = ClkSec / 10 + '0'; s[7] = ClkSec % 10 + '0'; break; case 3: //xg Add 格式为“HHMMSS” strcpy(s, "000000"); /* Create the template for the selected format */ s[0] = ClkHr / 10 + '0'; /* Convert TIME to ASCII */ s[1] = ClkHr % 10 + '0'; s[2] = ClkMin / 10 + '0'; s[3] = ClkMin % 10 + '0'; s[4] = ClkSec / 10 + '0'; s[5] = ClkSec % 10 + '0'; break;
default: strcpy(s, "?"); break; } } /* ********************************************************************************************************* * UPDATE THE TIME * * Description : This function is called to update the time (i.e. hours, minutes and seconds) * Arguments : None. * Returns : TRUE if we have completed one day. * FALSE otherwise * Notes : This function updates ClkSec, ClkMin and ClkHr. ********************************************************************************************************* */ //功能:更新时间 static BOOL ClkUpdateTime (void) { BOOL newday; newday = FALSE; /* Assume that we haven't completed one whole day yet */ if (ClkSec >= 59) { /* See if we have completed one minute yet */ ClkSec = 0; /* Yes, clear seconds */
if (ClkMin >= 59) { /* See if we have completed one hour yet */ ClkMin = 0; /* Yes, clear minutes */ bDispTemperatureSwitch = true; //整点时显示温度
if (ClkHr >= 23) { /* See if we have completed one day yet */ ClkHr = 0; /* Yes, clear hours ... */ //时钟计时跨天 newday = TRUE; /* ... change flag to indicate we have a new day */ } else { ClkHr++; /* No, increment hours */ } } else { ClkMin++; /* No, increment minutes */ if((ClkMin % c_DispTemperatureIntervalMinute) == 0) { bDispTemperatureSwitch = true; } else { bDispTemperatureSwitch = false; } } } else { ClkSec++; /* No, increment seconds */ } return (newday); }
//时钟的小时加1操作 void CLK_IncHour(void) { if (ClkHr >= 23) { ClkHr = 0; } else { ClkHr++; } }
//时钟的小时加1操作 void CLK_IncMinute(void) { if (ClkMin >= 59) { ClkMin = 0; } else { ClkMin++; } }
/* ********************************************************************************************************* * SET TIME ONLY * * Description : Set the time-of-day clock * Arguments : hr is the desired hour (0..23) * min is the desired minutes (0..59) * sec is the desired seconds (0..59) * Returns : None. * Notes : It is assumed that you are specifying a correct time (i.e. there is no range checking * done by this function). ********************************************************************************************************* */ void Clk_set_time (UCHAR hr, UCHAR min, UCHAR sec) { EXIT_CRITICAL(); /* Release access to time-of-day clock */ ClkHr = hr; ClkMin = min; ClkSec = sec; ENTER_CRITICAL(); /* Gain exclusive access to time-of-day clock */
}
//@@*********************************************************** // // 功能: 给定的时间与当前时间比较 // 函数: int Cmp_now_time(UCHAR hr, UCHAR min, UCHAR sec) // 语言: C // 输入: // 输出: 0 相等 // >0 给定时间大于当前时间 // <0 给定时间小于当前时间 // 作者: 李艳平 // 日期: 2002-05-17 //@@*********************************************************** //------------------------------------------------------------- /* int Cmp_now_time(UCHAR hr, UCHAR min, UCHAR sec) { if(ClkHr!=hr) return(hr-ClkHr); else if(ClkMin!= min) return(min-ClkMin); else // return(sec-ClkSec); sec = sec; return(0); } */
|