📄 clk.c
字号:
*
* Description : This function converts a time-stamp to an ASCII string.
* Arguments : n is the desired format number:
* 1 : "MM-DD-YY HH:MM:SS" (needs at least 18 characters)
* 2 : "YYYY-MM-DD HH:MM:SS" (needs at least 20 characters)
* ts is the time-stamp value to format
* s is the destination ASCII string
* Returns : none
* Notes : - The time stamp is a 32 bit unsigned integer as follows:
*
* Field: -------Year------ ---Month--- ------Day----- ----Hours----- ---Minutes--- --Seconds--
* Bit# : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
*
* - The year is based from CLK_TS_BASE_YEAR. That is, if bits 31..26 contain 0 it really
* means that the year is really CLK_TS_BASE_YEAR. If bits 31..26 contain 13, the year
* is CLK_TS_BASE_YEAR + 13.
*********************************************************************************************************
*/
#if CLK_TS_EN && CLK_DATE_EN
void ClkFormatTS (INT8U n, TS ts, char *s)
{
INT16U yr;
INT8U month;
INT8U day;
INT8U hr;
INT8U min;
INT8U sec;
yr = CLK_TS_BASE_YEAR + (ts >> 26); /* Unpack time-stamp */
month = (ts >> 22) & 0x0F;
day = (ts >> 17) & 0x1F;
hr = (ts >> 12) & 0x1F;
min = (ts >> 6) & 0x3F;
sec = (ts & 0x3F);
switch (n) {
case 1:
strcpy(s, "MM-DD-YY HH:MM:SS"); /* Create the template for the selected format */
yr = yr % 100;
s[ 0] = month / 10 + '0'; /* Convert DATE to ASCII */
s[ 1] = month % 10 + '0';
s[ 3] = day / 10 + '0';
s[ 4] = day % 10 + '0';
s[ 6] = yr / 10 + '0';
s[ 7] = yr % 10 + '0';
s[ 9] = hr / 10 + '0'; /* Convert TIME to ASCII */
s[10] = hr % 10 + '0';
s[12] = min / 10 + '0';
s[13] = min % 10 + '0';
s[15] = sec / 10 + '0';
s[16] = sec % 10 + '0';
break;
case 2:
strcpy(s, "YYYY-MM-DD HH:MM:SS"); /* Create the template for the selected format */
s[ 0] = yr / 1000 + '0'; /* Convert DATE to ASCII */
yr = yr % 1000;
s[ 1] = yr / 100 + '0';
yr = yr % 100;
s[ 2] = yr / 10 + '0';
s[ 3] = yr % 10 + '0';
s[ 5] = month / 10 + '0';
s[ 6] = month % 10 + '0';
s[ 8] = day / 10 + '0';
s[ 9] = day % 10 + '0';
s[11] = hr / 10 + '0'; /* Convert TIME to ASCII */
s[12] = hr % 10 + '0';
s[14] = min / 10 + '0';
s[15] = min % 10 + '0';
s[17] = sec / 10 + '0';
s[18] = sec % 10 + '0';
break;
default:
strcpy(s, "?");
break;
}
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* GET TIME-STAMP
*
* Description : This function is used to return a time-stamp to your application. The format of the
* time-stamp is shown below:
*
* Field: -------Year------ ---Month--- ------Day----- ----Hours----- ---Minutes--- --Seconds--
* Bit# : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
*
* Arguments : None.
* Returns : None.
* Notes : The year is based from CLK_TS_BASE_YEAR. That is, if bits 31..26 contain 0 it really
* means that the year is CLK_TS_BASE_YEAR. If bits 31..26 contain 13, the year is
* CLK_TS_BASE_YEAR + 13.
*********************************************************************************************************
*/
#if CLK_TS_EN && CLK_DATE_EN
TS ClkGetTS (void)
{
TS ts;
OS_ENTER_CRITICAL();
ts = ClkTS;
OS_EXIT_CRITICAL();
return (ts);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* 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 ClkInit (void)
{
ClkSem = OSSemCreate(1); /* Create time of day clock semaphore */
ClkSemSec = OSSemCreate(0); /* Create counting semaphore to signal the occurrence of 1 sec. */
ClkTickCtr = 0;
ClkSec = 0;
ClkMin = 0;
ClkHr = 0;
#if CLK_DATE_EN
ClkDay = 1;
ClkMonth = 1;
ClkYear = 1999;
#endif
#if CLK_TS_EN && CLK_DATE_EN
ClkTS = ClkMakeTS(ClkMonth, ClkDay, ClkYear, ClkHr, ClkMin, ClkSec);
#endif
OSTaskCreate(ClkTask, (void *)0, &ClkTaskStk[CLK_TASK_STK_SIZE], CLK_TASK_PRIO);
}
/*$PAGE*/
/*
*********************************************************************************************************
* DETERMINE IF WE HAVE A LEAP YEAR
*
* Description : This function determines whether the 'year' passed as an argument is a leap year.
* Arguments : year is the year to check for leap year.
* Returns : TRUE if 'year' is a leap year.
* FALSE if 'year' is NOT a leap year.
*********************************************************************************************************
*/
#if CLK_DATE_EN
static BOOLEAN ClkIsLeapYear(INT16U year)
{
if (!(year % 4) && (year % 100) || !(year % 400)) {
return TRUE;
} else {
return (FALSE);
}
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* MAKE TIME-STAMP
*
* Description : This function maps a user specified date and time into a 32 bit variable called a
* time-stamp.
* Arguments : month is the desired month (1..12)
* day is the desired day (1..31)
* year is the desired year (CLK_TS_BASE_YEAR .. CLK_TS_BASE_YEAR+63)
* hr is the desired hour (0..23)
* min is the desired minutes (0..59)
* sec is the desired seconds (0..59)
* Returns : A time-stamp based on the arguments passed to the function.
* Notes : - The time stamp is formatted as follows using a 32 bit unsigned integer:
*
* Field: -------Year------ ---Month--- ------Day----- ----Hours----- ---Minutes--- --Seconds--
* Bit# : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
*
* - The year is based from CLK_TS_BASE_YEAR. That is, if bits 31..26 contain 0 it really
* means that the year is really CLK_TS_BASE_YEAR. If bits 31..26 contain 13, the year is
* CLK_TS_BASE_YEAR + 13.
*********************************************************************************************************
*/
#if CLK_TS_EN && CLK_DATE_EN
TS ClkMakeTS (INT8U month, INT8U day, INT16U yr, INT8U hr, INT8U min, INT8U sec)
{
TS ts;
yr -= CLK_TS_BASE_YEAR;
ts = ((INT32U)yr << 26) | ((INT32U)month << 22) | ((INT32U)day << 17);
ts |= ((INT32U)hr << 12) | ((INT32U)min << 6) | (INT32U)sec;
return (ts);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* SET DATE ONLY
*
* Description : Set the date 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 (CLK_TS_BASE_YEAR .. CLK_TS_BASE_YEAR+63)
* Returns : None.
* Notes : It is assumed that you are specifying a correct date (i.e. there is no range checking
* done by this function).
*********************************************************************************************************
*/
#if CLK_DATE_EN
void ClkSetDate (INT8U month, INT8U day, INT16U year)
{
INT8U err;
OSSemPend(ClkSem, 0, &err); /* Gain exclusive access to time-of-day clock */
ClkMonth = month;
ClkDay = day;
ClkYear = year;
ClkUpdateDOW(); /* Compute the day of the week (i.e. Sunday ...) */
OSSemPost(ClkSem); /* Release access to time-of-day clock */
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* SET DATE AND TIME
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -