Realize STM32 calendar reading, setting and output through RTC

1. The principle of RTC

The real time clock is an independent timer. The RTC module has a set of continuous counting counters, which can provide the clock calendar function under the corresponding software configuration. Modifying the value of the counter can reset the current time and date of the system.
The RTC module and clock configuration system (RCC_BDCR register) are in the backup area, that is, the settings and time of the RTC remain unchanged after a system reset or wake-up from standby mode.

2. Create a project

1. New project

Open STM32CubeMX
insert image description here

2. Select chip

insert image description here

3. Set RCC

insert image description here

4. Set the serial port Usart1

insert image description here

5. Configure RTC

insert image description here

6. Configure the clock

insert image description here

7. Project management

insert image description here
insert image description here

3. Keil writes code

1. Output initial time

1. Define the structure of time and date in main.c to obtain time and date

RTC_DateTypeDef GetData;  //获取日期结构体
RTC_TimeTypeDef GetTime;   //获取时间结构体

insert image description here
2. Add the following code in the while loop of the main function

/* Get the RTC current Time */
	    HAL_RTC_GetTime(&hrtc, &GetTime, RTC_FORMAT_BIN);
      /* Get the RTC current Date */
      HAL_RTC_GetDate(&hrtc, &GetData, RTC_FORMAT_BIN);

      /* Display date Format : yy/mm/dd */
      printf("%02d/%02d/%02d\r\n",2000 + GetData.Year, GetData.Month, GetData.Date);
      /* Display time Format : hh:mm:ss */
      printf("%02d:%02d:%02d\r\n",GetTime.Hours, GetTime.Minutes, GetTime.Seconds);

      printf("\r\n");

      HAL_Delay(1000);

3. Rewrite the fputc function in the main.c file to complete the redirection of the printf function

//添加头文件#include "stdio.h"
int fputc(int ch,FILE *f){
    
    
 uint8_t temp[1]={
    
    ch};
 HAL_UART_Transmit(&huart1,temp,1,2);
 return ch;
}

insert image description here
Note that if you rewrite printf, you must check Use MiscroLiB before building.
insert image description here
Result: RTC initial time
insert image description here
because we add 2000 to the output year:

 printf("%02d/%02d/%02d\r\n",2000 + GetData.Year, GetData.Month, GetData.Date)

It can be known that the default initial time of the RTC is 0:00:00:00:00:00:00, January 1, 00.

2. Output the current time and add the day of the week

1. Modify it to the current time in rtc.c
insert image description here
2. Add the following code:

/* Display date Format : weekday */
		if(GetData.WeekDay==1){
    
    
			printf("星期一\r\n");
		}else if(GetData.WeekDay==2){
    
    
			printf("星期二\r\n");
		}else if(GetData.WeekDay==3){
    
    
			printf("星期三\r\n");
		}else if(GetData.WeekDay==4){
    
    
			printf("星期四\r\n");
		}else if(GetData.WeekDay==5){
    
    
			printf("星期五\r\n");
		}else if(GetData.WeekDay==6){
    
    
			printf("星期六\r\n");
		}else if(GetData.WeekDay==7){
    
    
			printf("星期日\r\n");
		}

insert image description here
Result: current time
insert image description here

Four. Summary

Through this experiment, I understand the principle of the real-time clock RTC. And know how to realize the calendar reading, setting and output of STM32. By calling the HAL library function, get the time and date, and output it with the printf function.

Guess you like

Origin blog.csdn.net/asdhnkhn/article/details/127671499