📄 clk.c
字号:
* Description : Set the date and time of the time-of-day clock
* Arguments : month is the desired month (1..12)
* day is the desired day (1..31)
* year is the desired year (2xxx)
* 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 date and time (i.e. there is no range
* checking done by this function).
*********************************************************************************************************
*/
#if CLK_DATE_EN
void ClkSetDateTime (INT8U month, INT8U day, INT16U year, INT8U hr, INT8U min, INT8U sec)
{
INT8U err;
OSSemPend(ClkSem, 0, &err); /* Gain exclusive access to time-of-day clock */
ClkMonth = month;
ClkDay = day;
ClkYear = year;
ClkHr = hr;
ClkMin = min;
ClkSec = sec;
ClkUpdateDOW(); /* Compute the day of the week (i.e. Sunday ...) */
OSSemPost(ClkSem); /* Release access to time-of-day clock */
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* 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 ClkSetTime (INT8U hr, INT8U min, INT8U sec)
{
OS_ENTER_CRITICAL(); /* Gain exclusive access to time-of-day clock */
ClkHr = hr;
ClkMin = min;
ClkSec = sec;
OS_EXIT_CRITICAL(); /* Release access to time-of-day clock */
}
/*$PAGE*/
/*
*********************************************************************************************************
* SIGNAL CLOCK MODULE THAT A 'CLOCK TICK' HAS OCCURRED
*
* Description : This function is called by the 'clock tick' ISR on every tick. This function is thus
* responsible for counting the number of clock ticks per second. When a second elapses,
* this function will signal the time-of-day clock task.
* Arguments : None.
* Returns : None.
* Note(s) : CLK_DLY_TICKS must be set to the number of ticks to produce 1 second.
* This would typically correspond to OS_TICKS_PER_SEC if you use uC/OS-II.
*********************************************************************************************************
*/
void ClkSignalClk (void)
{
ClkTickCtr++; /* count the number of 'clock ticks' for one second */
if (ClkTickCtr >= CLK_DLY_TICKS) {
ClkTickCtr = 0;
OSSemPost(ClkSemSec); /* Signal that one second elapsed */
}
}
/*
*********************************************************************************************************
* TIME-OF-DAY CLOCK TASK
*
* Description : This task is created by ClkInit() and is responsible for updating the time and date.
* ClkTask() executes every second.
* Arguments : None.
* Returns : None.
* Notes : CLK_DLY_TICKS must be set to produce 1 second delays.
*********************************************************************************************************
*/
void ClkTask (void *data)
{
INT8U err;
data = data; /* Avoid compiler warning (uC/OS requirement) */
for (;;) {
#if CLK_USE_DLY
OSTimeDlyHMSM(0, 0, 1, 0); /* Delay for one second */
#else
OSSemPend(ClkSemSec, 0, &err); /* Wait for one second to elapse */
#endif
OSSemPend(ClkSem, 0, &err); /* Gain exclusive access to time-of-day clock */
if (ClkUpdateTime() == TRUE) { /* Update the TIME (i.e. HH:MM:SS) */
#if CLK_DATE_EN
ClkUpdateDate(); /* And date if a new day (i.e. MM-DD-YY) */
#endif
}
#if CLK_TS_EN && CLK_DATE_EN
ClkTS = ClkMakeTS(ClkMonth, ClkDay, ClkYear, ClkHr, ClkMin, ClkSec);
#endif
OSSemPost(ClkSem); /* Release access to time-of-day clock */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* UPDATE THE DATE
*
* Description : This function is called to update the date (i.e. month, day and year)
* Arguments : None.
* Returns : None.
* Notes : This function updates ClkDay, ClkMonth, ClkYear and ClkDOW.
*********************************************************************************************************
*/
#if CLK_DATE_EN
static void ClkUpdateDate (void)
{
BOOLEAN newmonth;
newmonth = TRUE;
if (ClkDay >= ClkMonthTbl[ClkMonth].MonthDays) { /* Last day of the month? */
if (ClkMonth == 2) { /* Is this February? */
if (ClkIsLeapYear(ClkYear) == TRUE) { /* Yes, Is this a leap year? */
if (ClkDay >= 29) { /* Yes, Last day in february? */
ClkDay = 1; /* Yes, Set to 1st day in March */
} else {
ClkDay++;
newmonth = FALSE;
}
} else {
ClkDay = 1;
}
} else {
ClkDay = 1;
}
} else {
ClkDay++;
newmonth = FALSE;
}
if (newmonth == TRUE) { /* See if we have completed a month */
if (ClkMonth >= 12) { /* Yes, Is this december ? */
ClkMonth = 1; /* Yes, set month to january... */
ClkYear++; /* ...we have a new year! */
} else {
ClkMonth++; /* No, increment the month */
}
}
ClkUpdateDOW(); /* Compute the day of the week (i.e. Sunday ...) */
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* COMPUTE DAY-OF-WEEK
*
* Description : This function computes the day of the week (0 == Sunday) based on the current month,
* day and year.
* Arguments : None.
* Returns : None.
* Notes : - This function updates ClkDOW.
* - This function is called by ClkUpdateDate().
*********************************************************************************************************
*/
#if CLK_DATE_EN
static void ClkUpdateDOW (void)
{
INT16U dow;
dow = ClkDay + ClkMonthTbl[ClkMonth].MonthVal;
if (ClkMonth < 3) {
if (ClkIsLeapYear(ClkYear)) {
dow--;
}
}
dow += ClkYear + (ClkYear / 4);
dow += (ClkYear / 400) - (ClkYear / 100);
dow %= 7;
ClkDOW = dow;
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* 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 BOOLEAN ClkUpdateTime (void)
{
BOOLEAN 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 */
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 */
}
} else {
ClkSec++; /* No, increment seconds */
}
return (newday);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -