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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : TimeDate.C
**     Project   : rtc
**     Processor : MC9S08JM60CLHE
**     Component : TimeDate
**     Version   : Component 02.106, Driver 01.20, CPU db: 3.00.046
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 2010-1-14, 14:08
**     Abstract  :
**         This bean "TimeDate" implements real time and date.
**         The bean requires a periodic interrupt generator: timer
**         compare or reload register or timer-overflow interrupt
**         (of free running counter). User can select precision of
**         selected timer.
**         The bean supports also alarm with event OnAlarm.
**     Settings  :
**         Timer name                  : RTC (8-bit)
**
**         Counter                     : RTCCNT    [$006D]
**         Mode register               : RTCSC     [$006C]
**         Run register                : RTCSC     [$006C]
**         Prescaler                   : RTCSC     [$006C]
**         Compare register            : RTCMOD    [$006E]
**
**         Interrupt name              : Vrtc
**         Interrupt enable reg.       : RTCSC     [$006C]
**         Priority                    : 
**         User handling procedure     : TimeDate_OnAlarm
**         This event is called whenever the current time is equal
**         to alarm time
**
**         High speed mode
**             Prescaler               : divide-by-1
**             Clock                   : 244 Hz
**           Resolution of timer
**             Xtal ticks              : 31232
**             microseconds            : 999424
**             milliseconds            : 999
**             seconds                 : 1
**             seconds (real)          : 0.999424
**             Hz                      : 1
**
**         Initialization:
**              Timer                  : Enabled
**
**              Time                   : 0:0:0
**              Date                   : 1/1/2010
**     Contents  :
**         SetTime  - byte TimeDate_SetTime(byte Hour, byte Min, byte Sec, byte Sec100);
**         GetTime  - byte TimeDate_GetTime(TIMEREC *Time);
**         SetDate  - byte TimeDate_SetDate(word Year, byte Month, byte Day);
**         GetDate  - byte TimeDate_GetDate(DATEREC *Date);
**         GetDay   - byte TimeDate_GetDay(byte *Day);
**         SetAlarm - byte TimeDate_SetAlarm(byte Hour, byte Min, byte Sec, byte Sec100);
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/

/* MODULE TimeDate. */

#include "Events.h"
#include "TimeDate.h"


#pragma MESSAGE DISABLE C2705          /* WARNING C2705: Possible loss of data */
#pragma MESSAGE DISABLE C5919          /* WARNING C5919: Conversion of floating to unsigned integral */


static bool EnUser;                    /* Enable/Disable device by user */
static byte CntDay;                    /* Day counter */
static byte CntMonth;                  /* Month counter */
static word CntYear;                   /* Year Counter */
static byte CntDOW;                    /* Sun - Sat counter */
static dword TotalHthL;
static dword TotalHthH;                /* Software tick counter (1 tick = 10ms) */
static dword AlarmHth;                 /* Alarm time (compared with software tick counter) */
static bool AlarmFlg;                  /* Alarm flag, it is set if alarm was called in the current day */

/* Table of month length (in days) */
static const  byte Month_Table[13] = {0,0,3,3,6,1,4,6,2,5,0,3,5}; /* Months table */
static const  byte ULY[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; /* Un-leap-year */
static const  byte  LY[12] = {31,29,31,30,31,30,31,31,30,31,30,31}; /* Leap-year */

/*** Internal macros and method prototypes ***/

/*
** ===================================================================
**     Method      :  SetCV (component TimeDate)
**
**     Description :
**         The method computes and sets compare eventually modulo value 
**         for time measuring.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define TimeDate_SetCV(_Val) \
  (RTCMOD = (byte)(_Val))

/*
** ===================================================================
**     Method      :  HWEnDi (component TimeDate)
**
**     Description :
**         Enables or disables the peripheral(s) associated with the bean.
**         The method is called automatically as a part of the Enable and 
**         Disable methods and several internal methods.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void HWEnDi(void);

/*** End of Internal methods declarations ***/

/*
** ===================================================================
**     Method      :  TimeDate_HWEnDi (component TimeDate)
**
**     Description :
**         Enables or disables the peripheral(s) associated with the bean.
**         The method is called automatically as a part of the Enable and 
**         Disable methods and several internal methods.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void HWEnDi(void)
{
  if (EnUser) {
    /* RTCSC: RTIF=1,RTCLKS=2,RTIE=1,RTCPS=4 */
    setReg8(RTCSC, 0xD4);              /* Run RTC - select clock source; set frequency and enable interrupt */ 
  } else {
    /* RTCSC: RTCPS=0 */
    clrReg8Bits(RTCSC, 0x0F);          /* Stop counter (RTCPS[0:3] = 000) */ 
  }
}

/*
** ===================================================================
**     Method      :  TimeDate_SetTime (component TimeDate)
**
**     Description :
**         This method sets a new actual time.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Hour            - Hours (0 - 23)
**         Min             - Minutes (0 - 59)
**         Sec             - Seconds (0 - 59)
**         Sec100          - Hundredths of seconds (0 - 99)
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte TimeDate_SetTime(byte Hour,byte Min,byte Sec,byte Sec100)
{
  bool TmrRun;                         /* Temporary flag enable/disable */

  if ((Sec100 > 99) || (Sec > 59) || (Min > 59) || (Hour > 23)) { /* Test correctnes of given time */
    return ERR_RANGE;                  /* If not correct then error */
  }
  TmrRun = EnUser;                     /* Store actual device state */
  if (EnUser) {                        /* Is the device enabled by user? */
    EnUser = FALSE;                    /* If yes then set the flag "device disabled" */
    HWEnDi();                          /* Enable/disable device according to status flags */
  }
  TotalHthH = (360000 * (dword)Hour) + (6000 * (dword)Min) + (100 * (dword)Sec) + (dword)Sec100; /* Load given time re-calculated to 10ms ticks into software tick counter */
  TotalHthL = 0;
  AlarmFlg = (bool)!(TotalHthH < AlarmHth); /* Set up alarm flag */
  if (TmrRun) {                        /* Was the device disabled? */
    EnUser = TRUE;                     /* If yes set flag "device enabled" */
    HWEnDi();                          /* Enable/disable device according to status flags */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  TimeDate_GetTime (component TimeDate)
**
**     Description :
**         Return current time.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Time            - Pointer to the structure TIMEREC. It
**                           contains actual number of hours, minutes,
**                           seconds and hundreths of seconds.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte TimeDate_GetTime(TIMEREC *Time)
{
  dword Var1;                          /* Working temporary copy of software tick counter */
  word Var2;                           /* Working temporary variable */

  EnterCritical();                     /* Save the PS register */
  Var1 = TotalHthH;                    /* Loading actual number of tens of ms */
  ExitCritical();                      /* Restore the PS register */
  Time->Sec100 = (byte)(Var1 % 100);   /* Modulo 100 gives appropriate number of hundreds of seconds */
  Var1 = (dword)(Var1 / 100);          /* Quotient gives total number of seconds then */
  Time->Sec = (byte)(Var1 % 60);       /* Modulo 60 gives appropriate number of seconds */
  Var2 = (word)(Var1 / 60);            /* Quotient gives total number of minutes then */
  Time->Min = (byte)(Var2 % 60);       /* Modulo 60 gives appropriate number of minutes */
  Time->Hour = (byte)(Var2 / 60);      /* Quotient gives total number of hours then */
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  TimeDate_SetDate (component TimeDate)
**
**     Description :
**         This method sets a new actual date. See limitations at the
**         page <General Info>.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Year            - Years (16-bit unsigned integer)
**         Month           - Months (8-bit unsigned integer)
**         Day             - Days (8-bit unsigned integer)
**     Returns     :

⌨️ 快捷键说明

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