STM32F3实时时钟RTC-闹钟和唤醒中…

  • STM32RTC使用步骤:
  1. 打开PWR时钟和Backup区数据访问
  2. 若使用外部低速时钟(LSE),打开LSE并等待起振
  3. 选择和打开RTC时钟,等待时钟同步
  4. 配置时间格式,分频系数等
  5. 根据需要配置时钟,日期,闹钟,唤醒,输出,时间戳,备份寄存器等模块
  6. 根据需要配置和打开中断,其中
 RTC Alarm ——EXTI line 17 
 RTC tamper and Timestamps——EXTI line 19 
 RTC wakeup——EXTI line 20 
下面的代码配置日期,时间,当前时间设置为 15年05月31日,星期日(7),15:50:40,打开闹钟A和唤醒中断,每一秒钟来一次中断,15:50:45秒产生闹钟中断,用串口打印相应的信息。
代码:
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);
}
结果:
STM32F3实时时钟RTC-闹钟和唤醒中断

风子
2015,05,31




你可能感兴趣的:(STM32,STM32学习笔记)