stm32f4 discovery RTC Alarm
扫描二维码
随时随地手机看文章
手册中注意几个地方:
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);//打开配置寄存器权限
RCC_HSEConfig(RCC_HSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY)==RESET);//等待时钟稳定
RCC_RTCCLKConfig((RCC_RTCCLKSource_HSE_Div31));//时钟分频
RCC_RTCCLKCmd(ENABLE);//使能RTC时钟
if(RTC_ReadBackupRegister(RTC_BKP_DR0)!=0x9527)//一个标志,看是否已经配置过RTC
{
RTC_WriteProtectionCmd(DISABLE);
RTC_EnterInitMode();
RTC_InitStructure.RTC_HourFormat=RTC_HourFormat_24;
RTC_InitStructure.RTC_AsynchPrediv=0x7D-1;//有两次分频。小于0x7F
RTC_InitStructure.RTC_SynchPrediv=0x811-1;//小于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);//设置初始时间
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); //初始化完成,设置标志
RTC_WriteProtectionCmd(ENABLE);
}
PWR_BackupAccessCmd(DISABLE);
RTC_WaitForSynchro();//等待RTC Time and Date 寄存器和RTC APB clock同步完成
PWR_BackupAccessCmd(ENABLE);//打开配置寄存器权限
RTC_AlarmCmd(RTC_Alarm_A,DISABLE);
//设置闹钟为 每到第5s则触发一次
RTC_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;//这就会是每秒都触发一次闹钟了
RTC_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);
//中断优先级设定见nvic.c
PWR_BackupAccessCmd(DISABLE);
//Alarm必须配置EXTI_Line17中断
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//上升沿
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_Init(&EXTI_InitStructure);
}
void RTC_Alarm_IRQHandler(void)
{
if(RTC_GetFlagStatus(RTC_FLAG_ALRAF)==SET)
{
printf("alarm come rn");
PWR_BackupAccessCmd(ENABLE);//必须有,否则RTC_ClearITPendingBit 不会成功
RTC_ClearITPendingBit(RTC_IT_ALRA);
PWR_BackupAccessCmd(DISABLE);
EXTI_ClearITPendingBit(EXTI_Line17);//注意必须有此句,否则中断一直被触发,主循环不被执行了。
}
}
主循环:
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:%drn",rtc_time.RTC_Hours,rtc_time.RTC_Minutes,
rtc_time.RTC_Seconds);
Delay_ms(1000);
}
运行效果:每到第5s触发闹钟: