stm32f4 discovery RTC Alarm

Publisher:ShimmeringMoonLatest update time:2017-01-08 Source: eefocusKeywords:stm32f4  discovery  RTC  Alarm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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: 
image


Keywords:stm32f4  discovery  RTC  Alarm Reference address:stm32f4 discovery RTC Alarm

Previous article:STM32 Global Unique ID Reading Method
Next article:stm32f4 RAM read protection settings for running programs

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号