RTC_SLEEP.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // Created by EDZ on 25-9-16.
  3. //
  4. #include "../Inc/RTC_SLEEP.h"
  5. RTC_TimeTypeDef SOS_key_struct = {0};
  6. RTC_TimeTypeDef JUGE_key_struct = {0};
  7. RTC_TimeTypeDef Online_struct = {0};
  8. RTC_TimeTypeDef SOS_struct = {0};
  9. RTC_TimeTypeDef IDLE_struct = {0};
  10. // 单独的RTC时间和日期设置函数
  11. void RTC_SetDateTime(RTC_HandleTypeDef *hrtc, uint8_t hours, uint8_t minutes, uint8_t seconds,
  12. uint8_t weekday, uint8_t month, uint8_t date, uint8_t year) {
  13. RTC_TimeTypeDef sTime = {0};
  14. RTC_DateTypeDef sDate = {0};
  15. // 设置时间
  16. sTime.Hours = hours;
  17. sTime.Minutes = minutes;
  18. sTime.Seconds = seconds;
  19. HAL_RTC_SetTime(hrtc, &sTime, RTC_FORMAT_BCD);
  20. // 设置日期
  21. sDate.WeekDay = weekday;
  22. sDate.Month = month;
  23. sDate.Date = date;
  24. sDate.Year = year;
  25. HAL_RTC_SetDate(hrtc, &sDate, RTC_FORMAT_BCD);
  26. }
  27. // 读取RTC当前时间和日期
  28. void RTC_GetDateTime(RTC_HandleTypeDef *hrtc, RTC_DateTimeTypeDef *datetime) {
  29. RTC_TimeTypeDef sTime = {0};
  30. RTC_DateTypeDef sDate = {0};
  31. // 读取当前时间
  32. HAL_RTC_GetTime(hrtc, &sTime, RTC_FORMAT_BCD);
  33. // 读取当前日期
  34. HAL_RTC_GetDate(hrtc, &sDate, RTC_FORMAT_BCD);
  35. // 存储到自定义结构体中
  36. datetime->hours = sTime.Hours;
  37. datetime->minutes = sTime.Minutes;
  38. datetime->seconds = sTime.Seconds;
  39. datetime->weekday = sDate.WeekDay;
  40. datetime->month = sDate.Month;
  41. datetime->date = sDate.Date;
  42. datetime->year = sDate.Year;
  43. }
  44. void RTC_Sleep_Enter_Fun() {
  45. /* 调用独立的函数设置初始时间 */
  46. RTC_SetDateTime(&hrtc, 0x12, 0x00, 0x00,
  47. RTC_WEEKDAY_MONDAY, RTC_MONTH_SEPTEMBER, 0x16, 0x25);
  48. /* 获取当前时间用于设置闹钟 */
  49. RTC_TimeTypeDef currentTime = {0};
  50. HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BCD);
  51. /* 设置闹钟:50秒后触发 */
  52. RTC_AlarmTypeDef sAlarm = {0};
  53. sAlarm.AlarmTime.Hours = currentTime.Hours;
  54. sAlarm.AlarmTime.Minutes = currentTime.Minutes;
  55. sAlarm.AlarmTime.Seconds = (currentTime.Seconds + 50) % 60;
  56. sAlarm.Alarm = RTC_ALARM_A;
  57. HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD);
  58. /* 进入 STANDBY 模式 */
  59. __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); // 清除唤醒标志
  60. HAL_PWR_EnterSTANDBYMode(); // MCU 进入 STANDBY
  61. // MCU 唤醒后会从 main() 重新开始运行
  62. }