Steps to use STM32RTC:
Enable PWR clock and Backup area data access
If using an external low-speed clock (LSE), turn on the LSE and wait for it to start oscillating.
Select and turn on the RTC clock and wait for the clock to synchronize
Configure time format, frequency division factor, etc.
Configure clock, date, alarm, wake-up, output, timestamp, backup register and other modules as needed
Configure and enable interrupts as needed, where
RTC Alarm ——EXTI line 17
RTC tamper and Timestamps——EXTI line 19
RTC wakeup——EXTI line 20
The following code configures the date and time. The current time is set to 15:50:40, Sunday (7), May 31, 2015. Alarm A and wake-up interrupt are turned on. An interrupt is generated every second. At 15:50:45 seconds, an alarm interrupt is generated and the corresponding information is printed using the serial port.
Code:
void RTC_Config(void)
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
RTC_InitTypeDef RTC_InitStructure;
RTC_AlarmTypeDef RTC_AlarmStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
PWR_BackupAccessCmd(ENABLE);
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_InitStructure.RTC_HourFormat=RTC_HourFormat_24;
RTC_InitStructure.RTC_AsynchPrediv=0x7F;
RTC_InitStructure.RTC_SynchPrediv=0xFF;
RTC_Init(&RTC_InitStructure);
RTC_DateStructure.RTC_Date=31; // 15,05,31
RTC_DateStructure.RTC_Month=RTC_Month_May;
RTC_DateStructure.RTC_Year=15;
RTC_DateStructure.RTC_WeekDay=RTC_Weekday_Sunday;
RTC_SetDate(RTC_Format_BIN,&RTC_DateStructure);
RTC_TimeStructure.RTC_H12=RTC_H12_AM; // 15:50:40
RTC_TimeStructure.RTC_Hours=15;
RTC_TimeStructure.RTC_Minutes=50;
RTC_TimeStructure.RTC_Seconds=40;
RTC_SetTime(RTC_Format_BIN,&RTC_TimeStructure);
RTC_AlarmStructure.RTC_AlarmDateWeekDaySel=RTC_AlarmDateWeekDaySel_WeekDay;
RTC_AlarmStructure.RTC_AlarmDateWeekDay=RTC_Weekday_Sunday;
RTC_AlarmStructure.RTC_AlarmMask=RTC_AlarmMask_None;
RTC_AlarmStructure.RTC_AlarmTime=RTC_TimeStructure;// 15:50:40
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds=45;// 15:50:45
RTC_SetAlarm(RTC_Format_BIN,RTC_Alarm_A,&RTC_AlarmStructure);
RTC_AlarmCmd(RTC_Alarm_A,ENABLE);
RTC_ITConfig(RTC_IT_ALRA,ENABLE);
RTC_ITConfig(RTC_IT_WUT,ENABLE);
RTC_WakeUpCmd(DISABLE);
RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
RTC_SetWakeUpCounter(0);
RTC_WakeUpCmd(ENABLE);
}
void RTC_NvicExti_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line17);
EXTI_InitStructure.EXTI_Line=EXTI_Line17;
EXTI_InitStructure.EXTI_LineCmd =ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line20);
EXTI_InitStructure.EXTI_Line=EXTI_Line20;
EXTI_InitStructure.EXTI_LineCmd =ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
}
Previous article:STM32F3—CAN communication sends and receives automatically
Next article:STM32 Digital-to-Analog Converter - DAC_DMA
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- CC3200-LAUNCHXL
- In a single-chip computer, writing 1 clears it to 0, and writing 0 clears it to 0. What is the difference?
- How to monitor battery voltage with Arduino
- Image Algorithm Transplantation to DSP and Its Optimization Steps
- To advance in technology to a certain level, you just need to read more datasheets!
- Prize-winning live broadcast: Keysight’s Metaverse Test Series Seminar: Challenges in VR/AR Digital Interface Testing in Progress!
- 51 STM32 reference study manuals, one-click download without points!
- Microwave and RF Applications
- With the advent of 5G, what should we pay attention to when designing mobile phone antennas?
- 220V motor control board, discharges very slowly after power failure, and can still shock people after a long time