【ST NUCLEO-WB09KE测评】-5-RTC测试
本帖最后由 慕容雪花 于 2024-9-12 14:28 编辑<p>本文将体验WB09芯片的RTC特性,开发环境是STM32CubeIDE。</p>
<p>‌RTC是实时时钟。‌ RTC是一个独立的定时器,拥有一个连续计数的计数器,可以在相应的软件配置下提供时钟日历的功能。通过修改计数器的值,可以重新设置系统当前的时间和日期。‌12</p>
<p>RTC的功能包括计时和提供当前的时间和日期。它包含两个32位的寄存器,分别用于存储秒、分钟、小时(12或24小时制)、星期几、日期、月份和年份。RTC还可以自动补偿月份的天数,并提供二进制格式的亚秒值。RTC通过主板电池供电,即使在断电情况下也能独立运行。</p>
<p>RTC在不同领域有不同的应用。例如,在嵌入式系统中,RTC用于提供准确的时间;在计算机系统中,RTC与CMOS集成,通过主板电池供电,确保系统时间在关机后仍然准确;在通信领域,RTC即时通讯用于企业内部的信息交流。</p>
<p>下图所示为RTC模块框图:</p>
<p style="text-align: center;"> </p>
<p> </p>
<p>在CubeIDE中配置使能RTC模块,配置时钟:</p>
<div style="text-align: center;"></div>
<div style="text-align: center;"> </div>
<div style="text-align: center;">
<div style="text-align: center;"></div>
<p> </p>
</div>
<p>之后点击保存,自动生成代码:</p>
<pre>
<code>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.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
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 = 0x21;
sTime.Minutes = 0x0;
sTime.Seconds = 0x0;
sTime.SubSeconds = 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_WEDNESDAY;
sDate.Month = RTC_MONTH_SEPTEMBER;
sDate.Date = 0x11;
sDate.Year = 0x24;
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 */
}</code></pre>
<p>添加获取日期,时间函数:</p>
<pre>
<code>static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
{
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
/* Get the RTC current Time */
HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
/* Display time Format : hh:mm:ss */
sprintf((char *)showtime, "%02d:%02d:%02d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
/* Display date Format : mm-dd-yyyy */
sprintf((char *)showdate, "%02d-%02d-%02d", sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year);
}</code></pre>
<p>主程序:</p>
<pre>
<code>while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
static uint8_t i;
printf("\r\n idle main loop, current date: %s, current time: %s", aShowDate, aShowTime);
OLED_Clear();
OLED_ShowString(0,0,"OLED Driven by HwI2C",8,1);
OLED_ShowString(0,12,"STM32 WB09KE",8,1);
OLED_ShowString(24,24,"Time & Date",16,1);
OLED_ShowString(0,48,aShowDate,8,1);
OLED_ShowString(80,48,aShowTime,8,1);
OLED_Refresh();
RTC_CalendarShow(aShowTime, aShowDate);
HAL_Delay(100);
}</code></pre>
<p> </p>
<p>实物展示:</p>
<p>705bb930a68f3291b8ad03565f6e4b01</p>
<p> </p>
<p>增加了三个按键分别修改小时,分钟,秒:</p>
<pre>
<code>void HAL_GPIO_EXTI_Callback(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin) {
RTC_TimeTypeDef tempTime = {0};
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
if (HAL_RTC_GetTime(&hrtc, &tempTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
if (GPIO_Pin == B1_PIN) {
BSP_LED_Toggle(LD1);
printf("\r\n B1 Pressed. Hours++");
tempTime.Hours++;
}
if (GPIO_Pin == B2_PIN) {
BSP_LED_Toggle(LD2);
printf("\r\n B2 Pressed. Minutes++");
tempTime.Minutes++;
}
if (GPIO_Pin == B3_PIN) {
BSP_LED_Toggle(LD3);
printf("\r\n B3 Pressed. Seconds++");
tempTime.Seconds++;
}
if (HAL_RTC_SetTime(&hrtc, &tempTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
}</code></pre>
<p><br />
</p>
<p> </p>
页:
[1]