STM32F3 real-time clock RTC - alarm and wake-up

Publisher:daasddlaLatest update time:2017-09-30 Source: eefocusKeywords:STM32F3 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  • Steps to use STM32RTC:

  1. Enable PWR clock and Backup area data access

  2. If using an external low-speed clock (LSE), turn on the LSE and wait for it to start oscillating.

  3. Select and turn on the RTC clock and wait for the clock to synchronize

  4. Configure time format, frequency division factor, etc.

  5. Configure clock, date, alarm, wake-up, output, timestamp, backup register and other modules as needed

  6. 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);

}

Keywords:STM32F3 Reference address:STM32F3 real-time clock RTC - alarm and wake-up

Previous article:STM32F3—CAN communication sends and receives automatically
Next article:STM32 Digital-to-Analog Converter - DAC_DMA

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号