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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
📖 第 1 页 / 共 2 页
字号:
**         ---             - 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_SetDate(word Year,byte Month,byte Day)
{
  byte a,b,c,d,tDay,tW;                /* Working temporary variable */
  const byte * ptr;                    /* Pointer to ULY/LY table */

  if ((Year < 1998) || (Year > 2099) || (Month > 12) || (Month == 0) || (Day > 31) || (Day == 0)) { /* Test correctness of given parameters */
    return ERR_RANGE;                  /* If not correct then error */
  }

  ptr = ( Year & 0x03 ? ULY : LY );    /* Set pointer to leap-year or un-leap-year day table */

  if (ptr[Month-1] < Day) {            /* Does the obtained number of days exceed number of days in the appropriate month & year? */
    return ERR_RANGE;                  /* If yes (incorrect date inserted) then error */
  }
  a = (byte)( Year > 1999 ? 6 : 0 );   /* Algorithm to calculate the day of the week */
  b = (byte)(Year % 100);
  c = (byte)(b >> 2);
  tDay = Day;
  if (!(Year & 3) && (Month < 3)) {
    tDay--;
  }
  d = Month_Table[Month];
  tW = (byte)((a + b + c + d + tDay) % 7);

  EnterCritical();                     /* Save the PS register */
  CntDOW = tW;                         /* Set Sun - Sat counter to calculated value of day in a week */
  CntDay = Day;                        /* Set day counter to the given value */
  CntMonth = Month;                    /* Set month counter to the given value */
  CntYear = Year;                      /* Set year counter to the given value */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  TimeDate_GetDate (component TimeDate)
**
**     Description :
**         Return current date.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Date            - Pointer to the structure DATEREC. It
**                           contains actual year, month, and day
**                           description.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte TimeDate_GetDate(DATEREC *Date)
{
  EnterCritical();                     /* Save the PS register */
  Date->Year = CntYear;                /* Year */
  Date->Month = CntMonth;              /* Month */
  Date->Day = CntDay;                  /* Day */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  TimeDate_GetDay (component TimeDate)
**
**     Description :
**         This method returns current day of the week.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Day             - Pointer to returned day - 8-bit unsigned
**                           number  (Range 0..6, where 0 =Sunday, 1
**                           =Monday, 2 =Tuesday, ...). 
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte TimeDate_GetDay(byte *Day)
{
  *Day = CntDOW;                       /* Day in a week */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  TimeDate_SetAlarm (component TimeDate)
**
**     Description :
**         This method sets a new time of alarm. (only time, not date -
**         alarm event <OnAlarm> is called every 24 hours). Setting any
**         parameter out of its range disables alarm.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Hour            - Hours (0 - 23)
**         Min             - Minutes (0 - 59)
**         Sec             - Seconds (0 - 59)
**         Sec100          - Hundreths of seconds (0 - 99)
**     Returns     :
**         ---             - Error code, possible codes: 
**                           - ERR_OK - OK 
**                           - ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte TimeDate_SetAlarm(byte Hour,byte Min,byte Sec,byte Sec100)
{
  dword Hundredth;                     /* Temporary variable */

  if ((Sec100 > 99) || (Sec > 59) || (Min > 59) || (Hour > 23)) { /* Test correctness of the given time */
    AlarmHth = ULONG_MAX;              /* If no correct then it means switch off the alarm */
    return ERR_OK;                     /* OK */
  }
  Hundredth = (360000 * (dword)Hour) + (6000 * (dword)Min) + (100 * (dword)Sec) + (dword)Sec100; /* Load given time re-calculated to 10ms ticks into temporary variable */
  EnterCritical();                     /* Save the PS register */
  AlarmFlg = (bool)!(TotalHthH < Hundredth); /* Set up alarm flag */
  AlarmHth = Hundredth;                /* Copy content of temporary variable into Alarm time variable */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  TimeDate_InitTD (component TimeDate)
**
**     Description :
**         Initializes the associated peripheral(s) and the beans 
**         internal variables. The method is called automatically as a 
**         part of the application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void TimeDate_InitTD(void)
{
  /* RTCSC: RTIF=0,RTCLKS=0,RTIE=0,RTCPS=0 */
  setReg8(RTCSC, 0x00);                /* Stop HW */ 
  AlarmHth = ULONG_MAX;                /* Disable alarm */
  AlarmFlg = FALSE;                    /* Reset alarm flag */
  (void)TimeDate_SetDate((word)2010, (byte)1, (byte)1); /* Initial date */
  EnUser = FALSE;
  (void)TimeDate_SetTime((byte)0, (byte)0, (byte)0, (byte)0); /* Initialize time */
  EnUser = TRUE;
  TimeDate_SetCV(0xF3);                /* Inicialize appropriate value to the compare/modulo/reload register */
  RTCMOD = RTCMOD;                     /* Reset HW counter */
  HWEnDi();
}

/*
** ===================================================================
**     Method      :  TimeDate_Interrupt (component TimeDate)
**
**     Description :
**         The method services the interrupt of the selected peripheral(s)
**         and eventually invokes the beans event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
ISR(TimeDate_Interrupt)
{
  dword Val32;                         /* Temporary variable */
  const byte * ptr;                    /* Pointer to ULY/LY table */
  bool InvokeAlarmEvnt = FALSE;        /* Flag for OnAlarm event call */

  /* RTCSC: RTIF=1 */
  setReg8Bits(RTCSC, 0x80);            /* Reset real-time counter request flag */ 
  TotalHthH += 0x63LU;                 /* Software timer counter increment by timer period (10 ms) */
  Val32 = TotalHthL + 0xF141205CLU;    /* Add timer period to the lower part of software timer counter */
  if (Val32 < TotalHthL) {             /* Was there a carry from lower part into upper part of software timer counter? */
    TotalHthH++;                       /* If yes then increment upper part of the software timer counter */
  }
  TotalHthL = Val32;                   /* Write new value of the software timer counter */
  /* *** */
  if (!AlarmFlg) {                     /* Has the alarm already been on? */
    if (TotalHthH >= AlarmHth) {       /* Is the condition for alarm invocation satisfied? */
      InvokeAlarmEvnt = TRUE;          /* Set flag to invoke event at the end */
      AlarmFlg = TRUE;                 /* Set alarm flag - alarm has been invocated */
    }
  }
  /* *** */
  if (TotalHthH >= 0x0083D600LU) {     /* Does the counter reach 24 hours? */
    TotalHthH -= 0x0083D600LU;         /* If yes then reset it by subtracting exactly 24 hours */
    AlarmFlg = FALSE;                  /* Reset alarm flag - alarm has not occured during these 24 hours yet */
    CntDOW++;                          /* Increment Sun - Sat counter */
    if (CntDOW >= 7) {                 /* Sun - Sat counter overflow? */
      CntDOW = 0;                      /* Set Sun - Sat counter on Mon */
    }
    CntDay++;                          /* Increment day counter */
    if (CntYear & 0x03U) {             /* Is this year un-leap-one? */
      ptr = ULY;                       /* Set pointer to un-leap-year day table */
    } else {                           /* Is this year leap-one? */
      ptr = LY;                        /* Set pointer to leap-year day table */
    }
    ptr--;                             /* Decrement the pointer */
    if (CntDay > ptr[CntMonth]) {      /* Day counter overflow? */
      CntDay = 1;                      /* Set day counter on 1 */
      CntMonth++;                      /* Increment month counter */
      if (CntMonth > 0x0C) {           /* Month counter overflow? */
        CntMonth = 1;                  /* Set month counter on 1 */
        CntYear++;                     /* Increment year counter */
      }
    }
  }
  if (InvokeAlarmEvnt) {
    TimeDate_OnAlarm();                /* Invoke user event */
  }
}



/* END TimeDate. */

/*
** ###################################################################
**
**     This file was created by Processor Expert 3.07 [04.34]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

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