RTC_SLEEP.c 2.4 KB

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