STM32CubeMX series|RTC real-time clock

RTC real time clock

1. Introduction to RTC real-time clock

The real-time clock RTC of STM32 is an independent timer. The RTC module has a set of continuous counting counters. Under the corresponding software configuration, it can provide the clock calendar function. Modifying the counter value can reset the current time and date of the system. The
RTC module And the clock configuration system (RCC_BDCR register) is in the back area, that is, the RTC settings and events remain unchanged after the system resets or wakes up from standby mode. However, after the system is reset, access to the backup register and RTC is automatically prohibited to prevent accidental operations on the backup area (BKP). Therefore, before setting the time, cancel the write protection of the backup area. The simplified block diagram of RTC is as follows:

Insert picture description here
RTC consists of two main parts: the first part (APB1 interface) is used to connect to the APB1 bus, this unit also contains a set of 16-bit registers, which can be read and written through the APB1 bus; the other part (RTC core) consists of a The group programmable counter is composed of two main modules. The first module is the prescaler module of RTC, which can be programmed to generate 1 second RTC time reference TR_CLK. The second module is a 32-bit programmable counter (RTC_CNT), which can be initialized to the current system time. A 32-bit clock counter can record 4294967296 seconds per second, which is about 136 years. It is sufficient
RTC for general applications There is also an alarm clock register RTC_ALR, which is used to generate an alarm clock. The system time is accumulated according to the TR_CLK cycle and compared with the programmable time stored in the RTC_ALR register. If RTC_CNT = RTC_ALR, an alarm interrupt will be generated to realize the alarm function

2. Hardware design

In this experiment, the continuous output time is printed through the serial port 1 through the RTC second interrupt, and an alarm reminder is realized by setting the alarm interrupt, and the D1 indicator indicates that the system is running normally.

  • D1 indicator
  • USART1 serial port
  • RTC

3. Software design

3.1 STM32CubeMX settings
  • Enable the external high-speed crystal oscillator (HSE) and external low-speed crystal oscillator (LSE) in the RCC setting, and set the RTC frequency to 32.768KHz in the clock tree

Insert picture description here
Insert picture description here

  • PC0 is set to GPIO push-pull output mode, pull-up, high-speed, and the default output level is high
  • USART1 is selected as the asynchronous communication mode, the baud rate is set to 115200Bits/s, the transmission data length is 8Bit, no parity, 1 stop bit
  • Activate clock source, activate calendar, select No RTC Output, set initial date and time

Insert picture description here

  • Enable RTC global interrupt and alarm interrupt

Insert picture description here

  • Enter the project name, select the project path (no Chinese), select MDK-ARM V5; check Generated periphera initialization as a pair of'.c/.h' files per IP; click GENERATE CODE to generate the project code
3.2 MDK-ARM programming
  • Write the second interrupt processing callback function and the alarm interrupt processing function in the rtc.c file
extern RTC_DateTypeDef GetDate;
extern RTC_TimeTypeDef GetTime;

void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc){
    
    
	HAL_RTC_GetTime(hrtc,&GetTime,RTC_FORMAT_BIN);
	HAL_RTC_GetDate(hrtc,&GetDate,RTC_FORMAT_BIN);
	printf("Date:%02d-%02d-%02d\r\n",2000+GetDate.Year,GetDate.Month,GetDate.Date);
	printf("Time:%02d:%02d:%02d\r\n",GetTime.Hours,GetTime.Minutes,GetTime.Seconds);
	printf("\r\n");	
}

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc){
    
    
	printf("Alarm Active...!!!\n");
}
  • Write the alarm setting related code in the main.c file and turn on the related interrupt
/* USER CODE BEGIN PV */
RTC_DateTypeDef GetDate;
RTC_TimeTypeDef GetTime;
RTC_AlarmTypeDef sAlarm;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

int main(void){
    
    
  HAL_Init();
  SystemClock_Config();
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  __HAL_RTC_SECOND_ENABLE_IT(&hrtc,RTC_IT_SEC); //开启秒中断
  sAlarm.Alarm = RTC_ALARM_A;
  sAlarm.AlarmTime.Hours = 16;
  sAlarm.AlarmTime.Minutes = 20;
  sAlarm.AlarmTime.Seconds = 30;
  HAL_RTC_SetAlarm_IT(&hrtc,&sAlarm,RTC_FORMAT_BIN);  //设置闹钟并使能闹钟中断
  /* USER CODE END 2 */
  while (1){
    
    
    HAL_Delay(1000);
	HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_0);
  }
}

4. Download verification

After the compilation is correct, download it to the development board, you can see that the D1 indicator flashes once every 1s, the serial port assistant will print out the corresponding date and time every 1s, and the related statements in the alarm interrupt function will be printed out when the alarm set time is up

Insert picture description here

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108712743