rtc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. ******************************************************************************
  3. * @file rtc.c
  4. * @brief This file provides code for the configuration
  5. * of the RTC instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "rtc.h"
  21. /* USER CODE BEGIN 0 */
  22. RTC_TimeTypeDef GetTime;
  23. RTC_DateTypeDef GetDate;
  24. uint8_t RTC_READ_Flag;
  25. /* USER CODE END 0 */
  26. RTC_HandleTypeDef hrtc;
  27. extern uint16_t Now_date;
  28. /* RTC init function */
  29. void MX_RTC_Init(void)
  30. {
  31. uint8_t count = 0;
  32. /** Initialize RTC Only
  33. */
  34. hrtc.Instance = RTC;
  35. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  36. hrtc.Init.AsynchPrediv = 127;
  37. hrtc.Init.SynchPrediv = 255;
  38. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  39. hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  40. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  41. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  42. hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
  43. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  44. {
  45. rt_kprintf("HAL_RTC_Init failed !\r\n");
  46. }
  47. while (1)
  48. {
  49. HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
  50. HAL_RTC_GetDate(&hrtc, &GetDate, RTC_FORMAT_BIN);
  51. rt_kprintf("%02d/%02d/%02d\r\n", 2000 + GetDate.Year, GetDate.Month, GetDate.Date);
  52. rt_kprintf("%02d:%02d:%02d\r\n\r\n", GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
  53. //等待获取了当前时间再往下走
  54. if ((GetTime.Hours != 0) || (GetTime.Minutes != 0) || (GetTime.Seconds != 0))
  55. {
  56. Now_date = (GetDate.Year-21)*12+GetDate.Month;
  57. Now_date = Now_date << 8;
  58. Now_date |= GetDate.Date;
  59. break;
  60. }
  61. count++;
  62. //给出循环出口
  63. if (count > 3)
  64. {
  65. break;
  66. }
  67. }
  68. }
  69. void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
  70. {
  71. if(rtcHandle->Instance==RTC)
  72. {
  73. /* USER CODE BEGIN RTC_MspInit 0 */
  74. /* USER CODE END RTC_MspInit 0 */
  75. /* RTC clock enable */
  76. __HAL_RCC_RTC_ENABLE();
  77. __HAL_RCC_RTCAPB_CLK_ENABLE();
  78. /* USER CODE BEGIN RTC_MspInit 1 */
  79. /* USER CODE END RTC_MspInit 1 */
  80. }
  81. }
  82. void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
  83. {
  84. if(rtcHandle->Instance==RTC)
  85. {
  86. /* USER CODE BEGIN RTC_MspDeInit 0 */
  87. /* USER CODE END RTC_MspDeInit 0 */
  88. /* Peripheral clock disable */
  89. __HAL_RCC_RTC_DISABLE();
  90. __HAL_RCC_RTCAPB_CLK_DISABLE();
  91. /* USER CODE BEGIN RTC_MspDeInit 1 */
  92. /* USER CODE END RTC_MspDeInit 1 */
  93. }
  94. }
  95. /* USER CODE BEGIN 1 */
  96. void RTC_SetTime(uint8_t hour, uint8_t minute, uint8_t second)
  97. {
  98. /*##-2- Configure the Time #################################################*/
  99. GetTime.Hours = hour;
  100. GetTime.Minutes = minute;
  101. GetTime.Seconds = second;
  102. // GetTime.TimeFormat = RTC_HOURFORMAT12_AM;
  103. GetTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  104. GetTime.StoreOperation = RTC_STOREOPERATION_RESET;
  105. if(HAL_RTC_SetTime(&hrtc,&GetTime,RTC_FORMAT_BIN) != HAL_OK)
  106. {
  107. }
  108. }
  109. /* USER CODE END 1 */
  110. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/