733|2

166

帖子

1

TA的资源

一粒金砂(高级)

楼主
 

【NUCLEO-U083RC】3.RTC定时器输出精确时间 [复制链接]

本帖最后由 电子烂人 于 2024-5-5 19:35 编辑

这篇帖子试着来探讨RTC的功能。

1.RTC相关电路和手册:

众所周知,STM32的RTC定时器是嵌软工程师最头疼的地方之一,

ST的手册这部分写的比较精简,手册里对于RTC的说明:(来源:DS14463)

另外,在电源和低功耗部分,手册也给出了RTC在不同模式下的工作情况:(来源:DS14463)
 

2.cubeide配置:

如图,配置好DEBUG和RCC时钟之后,如图配置RTC定时器:

其他部分如串口沿用上一篇帖子的配置。

3.编辑代码

在MX_RTC_Init()的定义可以看到

static void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 0 */

  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};

  /* USER CODE BEGIN RTC_Init 1 */

  /* USER CODE END RTC_Init 1 */

  /** Initialize RTC Only
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
  hrtc.Init.BinMode = RTC_BINARY_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date
  */
  sTime.Hours = 0x20;
  sTime.Minutes = 0x10;
  sTime.Seconds = 0x0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_APRIL;
  sDate.Date = 0x29;
  sDate.Year = 0x0;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

rtc的参数全部被封装在stm32u0xx_hal_rtc.h文件的数个结构体中,还写有每个参数的注释,可以直接从中调用。

typedef struct
{
  uint8_t Hours;            /*!< Specifies the RTC Time Hour.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 12
                                 if the RTC_HourFormat_12 is selected.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 23
                                 if the RTC_HourFormat_24 is selected */

  uint8_t Minutes;          /*!< Specifies the RTC Time Minutes.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 59 */

  uint8_t Seconds;          /*!< Specifies the RTC Time Seconds.
                                 This parameter must be a number between Min_Data = 0 and Max_Data = 59 */

  uint8_t TimeFormat;       /*!< Specifies the RTC AM/PM Time.
                                 This parameter can be a value of @ref  RTC_AM_PM_Definitions */

  uint32_t SubSeconds;      /*!< Specifies the RTC_SSR RTC Sub Second register content.
                                 This field is not used by HAL_RTC_SetTime.
                                 If the free running 32 bit counter is not activated (mode binary none)
                                    - This parameter corresponds to a time unit range between [0-1] Second
                                    with [1 Sec / SecondFraction +1] granularity
                                 else
                                    - This parameter corresponds to the free running 32 bit counter. */

  uint32_t SecondFraction;  /*!< Specifies the range or granularity of Sub Second register content
                                 corresponding to Synchronous pre-scaler factor value (PREDIV_S)
                                 This parameter corresponds to a time unit range between [0-1] Second
                                 with [1 Sec / SecondFraction +1] granularity.
                                 This field will be used only by HAL_RTC_GetTime function */

  uint32_t DayLightSaving;  /*!< This interface is deprecated. To manage Daylight
                                 Saving Time, please use HAL_RTC_DST_xxx functions */

  uint32_t StoreOperation;  /*!< This interface is deprecated. To manage Daylight
                                 Saving Time, please use HAL_RTC_DST_xxx functions */
} RTC_TimeTypeDef;

/**
  * @brief   RTC Date structure definition
  */
typedef struct
{
  uint8_t WeekDay;  /*!< Specifies the RTC Date WeekDay.
                         This parameter can be a value of @ref RTC_WeekDay_Definitions */

  uint8_t Month;    /*!< Specifies the RTC Date Month (in BCD format).
                         This parameter can be a value of @ref RTC_Month_Date_Definitions */

  uint8_t Date;     /*!< Specifies the RTC Date.
                         This parameter must be a number between Min_Data = 1 and Max_Data = 31 */

  uint8_t Year;     /*!< Specifies the RTC Date Year.
                         This parameter must be a number between Min_Data = 0 and Max_Data = 99 */

} RTC_DateTypeDef;

/**
  * @brief  RTC Alarm structure definition
  */
typedef struct
{
  RTC_TimeTypeDef AlarmTime;     /*!< Specifies the RTC Alarm Time members */

  uint32_t AlarmMask;            /*!< Specifies the RTC Alarm Masks.
                                      This parameter can be a value of @ref RTC_AlarmMask_Definitions */

  uint32_t AlarmSubSecondMask;   /*!< Specifies the RTC Alarm SubSeconds Masks.
                                      if Binary mode is RTC_BINARY_ONLY or is RTC_BINARY_MIX
                                        This parameter can be a value of
                                        @ref RTCEx_Alarm_Sub_Seconds_binary_Masks_Definitions
                                      else if Binary mode is RTC_BINARY_NONE
                                        This parameter can be a value of @ref RTC_Alarm_Sub_Seconds_Masks_Definitions */

  uint32_t BinaryAutoClr;        /*!< Clear synchronously counter (RTC_SSR) on binary alarm.
                                      RTC_ALARMSUBSECONDBIN_AUTOCLR_YES must only be used if Binary mode is
                                      RTC_BINARY_ONLY
                                      This parameter can be a value of
                                      @ref RTCEx_Alarm_Sub_Seconds_binary_Clear_Definitions */

  uint32_t AlarmDateWeekDaySel;  /*!< Specifies the RTC Alarm is on Date or WeekDay.
                                     This parameter can be a value of @ref RTC_AlarmDateWeekDay_Definitions */

  uint8_t AlarmDateWeekDay;      /*!< Specifies the RTC Alarm Date/WeekDay.
                                      If the Alarm Date is selected, this parameter must be set to
                                      a value in the 1-31 range.
                                      If the Alarm WeekDay is selected, this parameter can be a value of
                                      @ref RTC_WeekDay_Definitions */

  uint32_t FlagAutoClr;          /*!< Specifies the alarm trigger generation. This feature is meaningful to avoid any
                                      RTC software execution after configuration.
                                      When FlagAutoClr is set in interrupt mode, EXTI is configured as EVENT instead of
                                      interrupt to avoid useless IRQ handler execution.
                                      This parameter can be a value of @ref RTC_ALARM_Flag_AutoClear_Definitions */

  uint32_t Alarm;                /*!< Specifies the alarm .
                                      This parameter can be a value of @ref RTC_Alarms_Definitions */
} RTC_AlarmTypeDef;

新建两个参数以调用出RTC的数值

/* USER CODE BEGIN PM */
RTC_DateTypeDef Get_Data;
RTC_TimeTypeDef Get_Time;
/* USER CODE END PM */

在主函数中调用

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LPUART1_UART_Init();
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
   while (1)
    {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  if (HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN) == HAL_OK)
	        {
	            char buffer[20];
	            sprintf(buffer, "Time: %02d:%02d:%02d\n", Get_Time.Hours, Get_Time.Minutes, Get_Time.Seconds);
	            HAL_UART_Transmit(&hlpuart1, (uint8_t *)buffer, strlen(buffer), 10);
	        }

  	  //HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN);
  	  //HAL_UART_Transmit(&hlpuart1,&Get_Time.Seconds,5,2);
  	  HAL_Delay(1000);
    }
  /* USER CODE END 3 */
}

按理说这个代码是可以运行的,但是在这里遇到了一个严重的BUG,RTC的时间不会工作

串口会一直发回一个固定设置好的时间。。。。
翻了好久的手册都没有消息,最后在一篇帖子里发现,STM32的RTC需要读取TIME后读取DATE,否则会一直卡住

之后稍微修改就可以得到准确的时间值了:

while (1)
    {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  if (HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN) == HAL_OK)

	        {
		  	if (HAL_RTC_GetDate(&hrtc, &Get_Data, RTC_FORMAT_BIN) == HAL_OK)
		        {
	            	char buffer[20];
	            	sprintf(buffer, "Time: %02d:%02d:%02d\n,Date: %02d/%02d/%02d WeekDay: %d\n", Get_Time.Hours, Get_Time.Minutes, Get_Time.Seconds, Get_Data.Date,Get_Data.Month,Get_Data.Year,Get_Data.WeekDay);
	            	HAL_UART_Transmit(&hlpuart1, (uint8_t *)buffer, strlen(buffer), 10);
		        }
	        }

  	  //HAL_RTC_GetTime(&hrtc, &Get_Time, RTC_FORMAT_BIN);
  	  //HAL_UART_Transmit(&hlpuart1,&Get_Time.Seconds,5,2);
  	  HAL_Delay(1000);
    }

 

之后就可以得到精准的RTC定时器输出的时间了:

这样看来,STM32U0的RTC在MSI时钟的加持下更加精准,还是比较好用的。

【NUCLEO-U083RC】3.RTC定时器输出精确时间.zip (8.91 MB, 下载次数: 1)

 

 

此帖出自stm32/stm8论坛

最新回复

多用用官方的例程,就不会卡了,有时候,官方例程比读数据手册更有用。   详情 回复 发表于 2024-5-6 08:34
点赞(1) 关注
个人签名

没用比没有强

 

回复
举报

7003

帖子

11

TA的资源

版主

沙发
 

多用用官方的例程,就不会卡了,有时候,官方例程比读数据手册更有用。

此帖出自stm32/stm8论坛

点评

学到了,多谢大佬  详情 回复 发表于 2024-5-6 11:49
 
 

回复

166

帖子

1

TA的资源

一粒金砂(高级)

板凳
 
lugl4313820 发表于 2024-5-6 08:34 多用用官方的例程,就不会卡了,有时候,官方例程比读数据手册更有用。

学到了,多谢大佬

此帖出自stm32/stm8论坛
 
个人签名

没用比没有强

 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表