Clock RTC implements calendar reading, setting and output of STM32

1. Conceptual Relevance

1.RTC

RTC, full name in English: Real-time clock, Chinese name: Real-time clock, refers to electronic equipment that can output real time like a clock. It is generally an integrated circuit, so it is also called a clock chip. Real-time clock chip is one of the most widely used consumer electronic products in daily life. It provides precise real-time time for people or precise time reference for electronic system. Currently, most real-time clock chips use high-precision crystal oscillator as clock source.

2.RTC Advantages

RTC can provide a stable clock signal for subsequent circuits. A system without a real-time clock can also calculate the actual time, but using a real-time clock has the following advantages:
1) Low power consumption (especially important when using auxiliary power)
2) Let the main system handle more time-sensitive work
3) Sometimes the output is more accurate than other methods

3.RTC characteristics

Programmable pre-dividing factor: The high dividing factor is 220.
A 32-bit programmable counter that can be used for measurements over a longer period of time.
Two separate clocks: PCLK1 and RTC clocks for APB1 interface (RTC clocks must be less than a quarter of PCLK1 clock frequency).
Three RTC clock sources can be selected:
HSE clock divided by 128;
LSE oscillator clock;
SI Oscillator Clock
Two independent reset types:
APB1 interface is reset by the system;
RTC core (pre-divider, alarm, counter and divider) can only be reset by backup domain
Three specialized shieldable interrupts:
1. Alarm interrupt, used to produce a software programmable alarm interrupt
2.second interrupt, used to produce a programmable periodic interrupt signal (up to 1 second in length).
3. Overflow interrupt, indicating that the internal programmable counter overflowed and reverted to zero.
RTC clock source:
Three different clock sources can be used to drive the system clock (SYSCLK):
HSI Oscillator Clock
HSE Oscillator Clock
PLL Clock
These devices have the following two secondary clock sources:
40kHz low speed internal RC can be used to drive independent watchdog and process sequence selection to drive RTC. RTC is used to automatically wake up the system from downtime/standby mode.
32.768kHz low-speed external crystals can also be used to programmatically select the drive RTC(RTCCLK).

4.RTC clock source

The RTC clock source is a stand-alone clock source.

Items and Codes

1. Project Establishment

1. SYS Configuration

Configure RCC (set high-speed external clock to enable external crystal oscillation LSE)

Configuring RTC (Activate Clock Source) and Calendar (Activate Calendar)
Here you can create the initial time

(4) Enabled serial port


Clock Tree Configuration

Create a project

2. Code modifications

Add the following functions to main.c to complete redirection of printf functions

//Add Header File #include "stdio.h"
int fputc(int ch,FILE *f){
 uint8_t temp[1]={ch};
 HAL_UART_Transmit(&huart1,temp,1,2);
 return ch;
}

Define the time and date structure

RTC_DateTypeDef GetData;  //Get Date Structure

RTC_TimeTypeDef GetTime;   //Get Time Structure

Add the following code to the while 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);
      /* Display date Format : weekday */
		if(GetData.WeekDay==1){
			printf("Monday\r\n");
		}else if(GetData.WeekDay==2){
			printf("Tuesday\r\n");
		}else if(GetData.WeekDay==3){
			printf("Wednesday\r\n");
		}else if(GetData.WeekDay==4){
			printf("Thursday\r\n");
		}else if(GetData.WeekDay==5){
			printf("Friday\r\n");
		}else if(GetData.WeekDay==6){
			printf("Saturday\r\n");
		}else if(GetData.WeekDay==7){
			printf("Sunday\r\n");
		}

3. Running results

3. Summary

Learned RTC principles, and set up the reading and output of the calendar in the code section.

Reference Links

What is RTC
STM32 Calendar Read, Set and Output

Keywords: Single-Chip Microcomputer stm32

Added by sunnypal on Wed, 08 Dec 2021 02:13:40 +0200