STM32F3實(shí)時(shí)時(shí)鐘RTC-鬧鐘和喚醒中
掃描二維碼
隨時(shí)隨地手機(jī)看文章
STM32RTC使用步驟:
打開PWR時(shí)鐘和Backup區(qū)數(shù)據(jù)訪問
若使用外部低速時(shí)鐘(LSE),打開LSE并等待起振
選擇和打開RTC時(shí)鐘,等待時(shí)鐘同步
配置時(shí)間格式,分頻系數(shù)等
根據(jù)需要配置時(shí)鐘,日期,鬧鐘,喚醒,輸出,時(shí)間戳,備份寄存器等模塊
根據(jù)需要配置和打開中斷,其中
RTC Alarm ——EXTI line 17
RTC tamper and Timestamps——EXTI line 19
RTC wakeup——EXTI line 20
下面的代碼配置日期,時(shí)間,當(dāng)前時(shí)間設(shè)置為 15年05月31日,星期日(7),15:50:40,打開鬧鐘A和喚醒中斷,每一秒鐘來一次中斷,15:50:45秒產(chǎn)生鬧鐘中斷,用串口打印相應(yīng)的信息。
代碼:
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);
}