⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clk.c

📁 《嵌入式系统构件》源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
*********************************************************************************************************
*                                             Clock/Calendar
*
*                            (c) Copyright 1999, Jean J. Labrosse, Weston, FL
*                                           All Rights Reserved
*
* Filename   : CLK.C
* Programmer : Jean J. Labrosse
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              INCLUDE FILES
*********************************************************************************************************
*/

#define  CLK_GLOBALS                   /* CLK.H is informed to allocate storage for globals            */
#include "includes.h"

/*
*********************************************************************************************************
*                                            LOCAL CONSTANTS
*********************************************************************************************************
*/

#define  CLK_TS_BASE_YEAR    2000      /* Time stamps start year                                       */

/*
*********************************************************************************************************
*                                            LOCAL VARIABLES
*********************************************************************************************************
*/

static  OS_EVENT   *ClkSem;            /* Semaphore used to access the time of day clock               */
static  OS_EVENT   *ClkSemSec;         /* Counting semaphore used to keep track of seconds             */

static  OS_STK      ClkTaskStk[CLK_TASK_STK_SIZE];

static  INT8U       ClkTickCtr;        /* Counter used to keep track of system clock ticks             */


/*$PAGE*/
/*
*********************************************************************************************************
*                                              LOCAL TABLES
*********************************************************************************************************
*/

#if CLK_DATE_EN
static  char *ClkDOWTbl[] = {          /* NAME FOR EACH DAY OF THE WEEK                                */
    "Sunday ",
    "Monday ",
    "Tuesday ",
    "Wednesday ",
    "Thursday ",
    "Friday ",
    "Saturday "
};


static  CLK_MONTH 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

/*
*********************************************************************************************************
*                                        LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

        void          ClkTask(void *data);
static  BOOLEAN       ClkUpdateTime(void);

#if     CLK_DATE_EN
static  BOOLEAN       ClkIsLeapYear(INT16U year);
static  void          ClkUpdateDate(void);
static  void          ClkUpdateDOW(void);
#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                    FORMAT CURRENT DATE INTO STRING
*
* Description : Formats the current date into an ASCII string.
* Arguments   : n      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  ClkFormatDate (INT8U n, char *s)
{
    INT8U   err;
    INT16U  year;
    char    str[5];


    OSSemPend(ClkSem, 0, &err);                  /* Gain exclusive access to time-of-day clock         */
    switch (n) {
        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, ", ");
              itoa(ClkYear, str, 10);
              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;
    }
    OSSemPost(ClkSem);                           /* Release access to clock                            */
}
#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                    FORMAT CURRENT TIME INTO STRING
*
* Description : Formats the current time into an ASCII string.
* Arguments   : n      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  ClkFormatTime (INT8U n, char *s)
{
    INT8U err;
    INT8U hr;


    OSSemPend(ClkSem, 0, &err);                       /* Gain exclusive access to time-of-day clock    */
    switch (n) {
        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;

        default:
              strcpy(s, "?");
              break;
    }
    OSSemPost(ClkSem);                                /* Release access to time-of-day clock           */
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                         FORMAT TIME-STAMP

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -