接上一篇USART数据发送,在STM32CubeMX添加RTC,如下图片 Calenda Time配置时间为12:45:6 Calenda Time配置日期为20/1/1
在stm32g431xx_hal_rtc.h参照时间结构体
typedef struct
{
uint8_t Hours;
uint8_t Minutes;
uint8_t Seconds;
uint8_t TimeFormat;
uint32_t SubSeconds;
uint32_t SecondFraction;
uint32_t DayLightSaving;
uint32_t StoreOperation;
} RTC_TimeTypeDef;
```c
在stm32g431xx_hal_rtc.h参照日期结构体
typedef struct
{
uint8_t WeekDay;
uint8_t Month;
uint8_t Date;
uint8_t Year;
} RTC_DateTypeDef;
uint16_t TX_ms=0;
char TX_buff[100];
char RX_buff[10];
uint16_t TX_t=0;
RTC_TimeTypeDef Time;
RTC_DateTypeDef Date;
while (1)
{
if(TX_ms==1000){
HAL_RTC_GetTime(&hrtc,&Time,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&Date,RTC_FORMAT_BIN);
sprintf(TX_buff," %02d/%02d/%02d %02d:%02d:%02d",2000+Date.Year,Date.Month,Date.Date,Time.Hours,Time.Minutes,Time.Seconds);
HAL_UART_Transmit(&huart1,(uint8_t *)TX_buff,(uint16_t)sizeof(TX_buff),20);
TX_t++;
TX_ms=0;
}
}
}
效果如下图
|