Note a few points in the manual:
RTC_SetAlarm() :The Alarm register can only be written when the corresponding Alarm is disabled (Use the RTC_AlarmCmd(DISABLE)).
All RTC interrupts are connected to the EXTI controller.
Configure and enable the EXTI Line 17 in interrupt mode and select the rising edge sensitivity using the EXTI_Init() function.
=========
void RTC_Config(void)
{
RTC_InitTypeDef RTC_InitStructure;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
RTC_AlarmTypeDef RTC_AlarmStructure;
EXTI_InitTypeDef EXTI_InitStructure;
PWR_BackupAccessCmd(ENABLE);//Open configuration register permissionsRCC_HSEConfig
(RCC_HSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY)==RESET);//Wait for the clock to stabilizeRCC_RTCCLKConfig
((RCC_RTCCLKSource_HSE_Div31));//Clock divisionRCC_RTCCLKCmd
(ENABLE);//Enable RTC clock
if(RTC_ReadBackupRegister(RTC_BKP_DR0)!=0x9527)//A flag to see if the RTC has been configured
{
RTC_WriteProtectionCmd(DISABLE);
RTC_EnterInitMode();
RTC_InitStructure.RTC_HourFormat=RTC_HourFormat_24;
RTC_InitStructure.RTC_AsynchPrediv=0x7D-1;//There are two frequency divisions. Less than 0x7F
RTC_InitStructure.RTC_SynchPrediv=0x811-1;//Less than 0x7FFF
RTC_Init(&RTC_InitStructure);
RTC_TimeStructure.RTC_Seconds = 0x00;
RTC_TimeStructure.RTC_Minutes = 0x01;
RTC_TimeStructure.RTC_Hours = 0x01;
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
RTC_SetTime(RTC_Format_BCD,&RTC_TimeStructure); //Set the initial time
RTC_DateStructure.RTC_Date = 15;
RTC_DateStructure.RTC_Month = 11;
RTC_DateStructure.RTC_WeekDay= RTC_Weekday_Thursday;
RTC_DateStructure.RTC_Year = 13;
RTC_SetDate(RTC_Format_BCD,&RTC_DateStructure);//设置初始日期
RTC_ExitInitMode();
RTC_WriteBackupRegister(RTC_BKP_DR0,0X9527); //Initialization completed, set flag
RTC_WriteProtectionCmd(ENABLE);
}
PWR_BackupAccessCmd(DISABLE);
RTC_WaitForSynchro();//Wait for RTC Time and Date register and RTC APB clock to be synchronized
PWR_BackupAccessCmd(ENABLE); //Open configuration register permissions
RTC_AlarmCmd(RTC_Alarm_A,DISABLE);
//Set the alarm to trigger every 5 secondsRTC_AlarmStructure.RTC_AlarmTime.RTC_Hours
=1;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes=1;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds=5;
RTC_AlarmStructure.RTC_AlarmTime.RTC_H12=RTC_H12_AM;
//RTC_AlarmStructure.RTC_AlarmMask=RTC_AlarmMask_All; //This will trigger the alarm once every
secondRTC_AlarmStructure.RTC_AlarmMask=RTC_AlarmMask_DateWeekDay|
RTC_AlarmMask_Hours|RTC_AlarmMask_Minutes;
RTC_SetAlarm(RTC_Format_BIN,RTC_Alarm_A,&RTC_AlarmStructure);
RTC_AlarmCmd(RTC_Alarm_A,ENABLE);
RTC_ITConfig(RTC_IT_ALRA,ENABLE);
//Interrupt priority setting see nvic.c
PWR_BackupAccessCmd(DISABLE);
//Alarm must configure
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //Rising edge
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_Init(&EXTI_InitS structure);
}
void RTC_Alarm_IRQHandler(void)
{
if(RTC_GetFlagStatus (RTC_FLAG_ALRAF)==SET)
{
printf("alarm come \r\n");
PWR_BackupAccessCmd(ENABLE);//Must exist, otherwise RTC_ClearITPendingBit will not succeed
RTC_ClearITPendingBit(RTC_IT_ALRA);
PWR_BackupAccessCmd(DISABLE);
EXTI_ClearITPendingBit(EXTI_Line17);//Note that this sentence is required, otherwise the interrupt will always be triggered and the main loop will not be executed.
}
}
Main loop:
while (1)
{
PD12_Toggle;
RTC_GetTime(RTC_Format_BIN,&rtc_time);
RTC_GetDate(RTC_Format_BIN,&rtc_date);
printf("date:20%d-%d-%d ",rtc_date.RTC_Year,rtc_date.RTC_Month,
rtc_date.RTC_Date);
printf("time:%d:%d:%d\r\n",rtc_time.RTC_Hours,rtc_time.RTC_Minutes,
rtc_time.RTC_Seconds);
Delay_ms(1000);
}
Running effect: The alarm is triggered every 5 seconds:
Previous article:STM32 Global Unique ID Reading Method
Next article:stm32f4 RAM read protection settings for running programs
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- 【TI mmWave Radar Review】XWR14XX Data Path
- Disconnection detection
- 【CH579M-R1】+ Ultrasonic distance detection in serial communication mode
- [NUCLEO-WL55JC2 Review]——by Lan Yuye
- WI-FI 6E
- How to achieve the same positive and negative display effects on the same code screen
- Recruiting embedded system development engineers Annual salary: 200,000-500,000 | Experience: 5 years or more | Work location: Beijing
- ADC question
- Hetai MCU Problem
- [Chuanglong TL570x-EVM] Customized SD card and artificial intelligence framework introduction