STM32RTC second interrupt - based on HAL library (one article to understand how to configure and use)


foreword

相关说明:

Development board: CT117E-M4 (STM32G431RB Blue Bridge Cup embedded competition board)
Development environment: CubeMX+Keil5


CubeMX配置:

1. CubeMX configuration (RTC related)

1. Enable RTC:

Three steps (corresponding to 2, 3, 4 in the figure):
a. Enable the RTC clock source .
b. Enable the calendar (the alarm clock can be used only when the calendar is enabled).
c. Enable the alarm clock .

insert image description here

2. Binary configuration:

There are two options, Hourformat24 corresponds to 24 base, Hourformat12 corresponds to 12 base.
insert image description here

3. Initial time configuration:

The DataFormat options are BIN (time in decimal) BCD (time in hexadecimal)
Hours is the hour
of the initial time Minutes is the minute
of the initial time Seconds is the second of the initial time The
insert image description here
difference in the time system is reflected in the code as follows , after selecting hexadecimal, you can also use decimal to configure the time or alarm clock, because you can re-select the time system during configuration .
insert image description hereinsert image description here

4. Date configuration:

To configure the second interrupt, you don't need to care about the configuration of the date, just the initial value.
insert image description here

5. Alarm clock configuration:

It is divided into two steps:
1. Initially, directly configure the alarm time to be the next second (23:55:56) of the initial time ( 23:55:55).
2. Enable the alarm trigger to ignore the date , ignore the hour , ignore the minute , but not the second . This way each interrupt occurs is only determined by the number of seconds.insert image description here

2. Code

There are three functions :
1.GET_Time()
2.SET_Alarm()
3.HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)

1. Get time

When getting the time, you should pay attention. If you only call HAL_RTC_GetTime(), there will be a problem that the alarm clock will not continue to go . Even without using it.

RTC_TimeTypeDef Now_Time;//定义时间结构体
RTC_DateTypeDef Now_Date;//定义日期结构体

void GET_Time()//获取当前时间
{
    
    
	HAL_RTC_GetTime(&hrtc,&Now_Time,RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc,&Now_Date,RTC_FORMAT_BIN);
}

2. Set an alarm

First, copy the alarm structure definition in the generated code, define it as a global variable , and then delete the generated sentence (when reconfiguring other things with MX, pay attention to delete the generated code again!!!), define it as a global variable The purpose is to use the alarm clock configured above (ignore the date, ignore the hour...).
insert image description here
Then in the function, assign the value of the second of the current time + 1 to the second of the alarm structure ! (the first line of code in the function); then judge whether it is 60, and reassign 0 if it is 60 ! ; Then call the function HAL_RTC_SetAlarm_IT() to reconfigure the alarm clock !

void SET_Alarm()
{
    
    
	sAlarm.AlarmTime.Seconds = Now_Time.Seconds+1;
	if(sAlarm.AlarmTime.Seconds==60)sAlarm.AlarmTime.Seconds=0;
    HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN);
}

3. Alarm clock interrupt function

After reaching the previously configured interrupt time (23:55:56), it will enter the alarm clock interrupt function, and do two things in the interrupt function:
1. Get the current time GET_Time().
2. Set the interrupt SET_Alarm() for the next second.
In this way, the alarm clock will be reconfigured every time an interruption occurs, and the interruption will occur again in the next second. Repeat this to achieve the effect of a second interruption , and then add code (LCD update time, serial port printing) to the interruption function according to your own needs. time).

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
    
    
	GET_Time();
	printf("%02d:%02d:%02d\n",Now_Time.Hours,Now_Time.Minutes,Now_Time.Seconds);//这句代码只是测试
	SET_Alarm();
}

Expansion (True Question of the 9th Provincial Competition of the Blue Bridge Cup): There are three steps
to configure the alarm clock according to the time stored in the EEPROM : 1. Read the time stored in the EEPROM and save it in the time structure . 2. Call the Set_Time function to set the current time . 3. Configure the alarm for the next second .


3. Experimental results

insert image description here


Summarize

The above is all the content, if there is any error, please criticize and correct.

Guess you like

Origin blog.csdn.net/Octopus1633/article/details/123792806