qzc0927 发表于 2024-3-5 16:53

ST NUCLEO-C031C6开发板学习笔记07(RTC实时时钟)

<div class='showpostmsg'>## 10:RTC实时时钟

### 10.1:硬件设计

查看芯片数据手册:

!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305164030046.png)

原理图设计:

!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305164123838.png)

### 10.2:软件设计

1:CubeMX设置

!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305164300057.png)

!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305164336164.png)

2:代码编写

RTC初始化:

```C
RTC_HandleTypeDef hrtc;

/* RTC init function */
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;
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 = 0x16;
sTime.Minutes = 0x18;
sTime.Seconds = 0x55;
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_TUESDAY;
sDate.Month = RTC_MONTH_MARCH;
sDate.Date = 0x5;
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实时时钟显示函数:

```C
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, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
    /* Display date Format : mm-dd-yyyy */
    sprintf((char *)showdate, "%2d-%2d-%2d", sdatestructureget.Month, sdatestructureget.Date, 2024 + sdatestructureget.Year);
       
        DEBUG_LOG("%s %s",showdate,showtime);

}
```

### 10.3:RTC单元测试

打开串口调试助手:

!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305165136508.png)

!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305165216353.png)</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                               
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: ST NUCLEO-C031C6开发板学习笔记07(RTC实时时钟)