rtc_interrupt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /***
  15. * Demo: RTC
  16. * Board: STC8H8K64U
  17. *
  18. * Note: Cx51 compiler supports interrupt 0~31 only, for interrupts greater
  19. * than 31, you need to
  20. 1. Define the handler in EXTI_VectUser(vector:006BH)
  21. 2. add an asm code to redirect the interrupt from EXTI_VectRTC(
  22. vector:0123H) to EXTI_VectUser(vector:006BH)
  23. ASM code example:
  24. CSEG AT 0123H ; vector address of EXTI_VectRTC
  25. LJMP 006BH ; jump to vector address of EXTI_VectUser
  26. END
  27. *
  28. *
  29. */
  30. #include "fw_hal.h"
  31. uint8_t year, month, day, hour, minute, second;
  32. #if defined __CX51__ || defined __C51__
  33. INTERRUPT(RTC_Routine, EXTI_VectUser)
  34. #else
  35. INTERRUPT(RTC_Routine, EXTI_VectRTC)
  36. #endif
  37. {
  38. SFRX_ON();
  39. if (RTC_IsSecondInterrupt())
  40. {
  41. RTC_ClearSecondInterrupt();
  42. year = YEAR;
  43. month = MONTH;
  44. day = DAY;
  45. hour = HOUR;
  46. minute = MIN;
  47. second = SEC;
  48. }
  49. SFRX_OFF();
  50. }
  51. void RTC_Init(void)
  52. {
  53. // Enable internal 32kHz OSC
  54. SYS_EnableOscillatorLSI();
  55. // Set RTC clock source
  56. RTC_SetClockSource(RTC_ClockSource_Internal);
  57. // Set clock initial value
  58. RTC_ConfigClock(22, 1, 9, 18, 59, 03, 0);
  59. // Init clock
  60. RTC_ConfigClockApply();
  61. // Enable second interrupt
  62. EXTI_RTC_SetSecondIntState(HAL_State_ON);
  63. // Enable global interrupt
  64. EXTI_Global_SetIntState(HAL_State_ON);
  65. // Clear interrupts
  66. RTC_ClearAllInterrupts();
  67. // Enable RTC
  68. RTC_SetRunState(HAL_State_ON);
  69. }
  70. int main(void)
  71. {
  72. SYS_SetClock();
  73. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  74. RTC_Init();
  75. while(1)
  76. {
  77. SFRX_ON();
  78. UART1_TxString("RTCCR:");
  79. UART1_TxHex(RTCCR);
  80. UART1_TxString(" RTCCFG:");
  81. UART1_TxHex(RTCCFG);
  82. UART1_TxString(" RTCIEN:");
  83. UART1_TxHex(RTCIEN);
  84. UART1_TxString(" RTCIF:");
  85. UART1_TxHex(RTCIF);
  86. SFRX_OFF();
  87. UART1_TxString(" Year:");
  88. UART1_TxHex(year);
  89. UART1_TxString(" Month:");
  90. UART1_TxHex(month);
  91. UART1_TxString(" Day:");
  92. UART1_TxHex(day);
  93. UART1_TxString(" Hour:");
  94. UART1_TxHex(hour);
  95. UART1_TxString(" Minute:");
  96. UART1_TxHex(minute);
  97. UART1_TxString(" Second:");
  98. UART1_TxHex(second);
  99. UART1_TxString("\r\n");
  100. SYS_Delay(900);
  101. }
  102. }