STM32L151C8 periodically wakes up from standby mode (RTC Wakeup Timer)

Publisher:zhihuaLatest update time:2018-09-10 Source: eefocusKeywords:STM32L151C8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "stm32l1xx.h"

#include "system_stm32l1xx.h"

#include "OLED.h"

#include "delay.h"


void RtcWakeUpConfig(void);

u8 RtcInit(void);

u8 RtcConfig(void);


int main()

{

    DelayInit(); //Delay initialization

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); //Enable PWR clock

    if(PWR_GetFlagStatus(PWR_FLAG_SB)) //Wake up from standby mode 

    {

        PWR_ClearFlag(PWR_FLAG_SB);

        PWR_ClearFlag(PWR_FLAG_WU);

    }

    OLED_Init();

    RtcInit();


    while(1)    

    {

        OLED_8x16StrP(0,0,"Runing..."); //LCD prompts that it is running

        DelayS(2); //Delay 2 seconds

        RtcWakeUpConfig(); //RTC wakeup configuration: automatic wakeup every 500ms

// PWR_WakeUpPinCmd(PWR_WakeUpPin_1,ENABLE); //Periodic auto-wakeup does not require an external wake-up pin, so this sentence is not needed

        PWR_EnterSTANDBYMode(); //Enter standby mode     

    }

}


void RtcWakeUpConfig(void)

{

    RTC_WakeUpCmd(DISABLE);

    RTC_ClearFlag(RTC_FLAG_WUTF);

    RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div8); //Select the clock as external 32.768KHz 8-division

    RTC_SetWakeUpCounter(2047); //500ms interval

    RTC_ClearITPendingBit(RTC_IT_WUT);

    RTC_ITConfig(RTC_IT_WUT,ENABLE); //Need to enable interrupt, no interrupt function is needed

    RTC_WakeUpCmd(ENABLE);

}



u8 RtcInit(void)

{

    // Check if it is the first time to configure the clock

    u8 flag = 0;

    if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) //Read data from the specified backup register: The data read does not match the specified data written.

    {

        /* RTC configuration  */

        flag = RtcConfig();

        if(flag == 0)

        {

            RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);

        }

        else

            return flag;

    }

    else

    {

        /* Enable the PWR clock */

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);


        /* Allow access to RTC */

        PWR_RTCAccessCmd(ENABLE);


        /* Wait for RTC APB registers synchronisation */

        RTC_WaitForSynchro();

    }

    return 0; //ok

}   


/**

  * @brief  Configure the RTC peripheral by selecting the clock source.

  * @param  None

  * @retval None

  */

u8 RtcConfig(void)

{

    /* Enable the PWR clock */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);


    /* Allow access to RTC */

    PWR_RTCAccessCmd(ENABLE);


    /* Enable the LSE OSC */

    RCC_LSEConfig(RCC_LSE_ON);


    /* Wait till LSE is ready */ 

    u32 temp = 0;

    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)

    {

        temp++;

        delay_ms(10);

        if(temp >= 250) return 1; //Failed to initialize the clock, the crystal oscillator has a problem, the 32768 crystal oscillator start-up limit time is 1-5S 

    }


    /* Select the RTC Clock Source */

    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);


    /* Enable the RTC Clock */

    RCC_RTCCLKCmd(ENABLE);


    /* Wait for RTC APB registers synchronisation */

    RTC_WaitForSynchro();


    /* Configure the RTC data register and RTC prescaler */

    RTC_InitTypeDef RTC_InitStructure;

    RTC_InitStructure.RTC_AsynchPrediv = 0x7F;

    RTC_InitStructure.RTC_SynchPrediv = 0xFF;

    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

    RTC_Init(&RTC_InitStructure);


    return 0;

}

Write the picture description here

Non-standby mode, RTC Wakeup timer interrupt configuration and interrupt processing

void RTC_IRQConfig(void)

{

    NVIC_InitTypeDef NVIC_InitStructure; 

    EXTI_InitTypeDef EXTI_InitStructure;

    /* EXIT configuration ************************************************** *********/

    EXTI_ClearITPendingBit(EXTI_Line20);

    EXTI_InitStructure.EXTI_Line = EXTI_Line20;

    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;

    EXTI_InitStructure.EXTI_LineCmd = ENABLE;

    EXTI_Init(&EXTI_InitStructure);


    /* Enable the RTC Wakeup Interrupt */

    NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);  

}


void RTC_WKUP_IRQHandler(void)

{

    if(RTC_GetITStatus(RTC_IT_WUT) != RESET) 

    {

        EXTI_ClearITPendingBit(EXTI_Line20); 

        PWR_RTCAccessCmd(ENABLE); //If it was disabled before, reopen it

        RTC_ClearITPendingBit(RTC_IT_WUT);

        PWR_RTCAccessCmd(DISABLE); //Can be turned off again

        GPIO_ToggleBits( GPIOB, GPIO_Pin_4 );

    }

}


Keywords:STM32L151C8 Reference address:STM32L151C8 periodically wakes up from standby mode (RTC Wakeup Timer)

Previous article:STM32L series low power debugging "Practical"
Next article:Wireless street light project——STM32L low power consumption related

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号