#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;
}
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 );
}
}
Previous article:STM32L series low power debugging "Practical"
Next article:Wireless street light project——STM32L low power consumption related
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- What are the future development directions of the new generation of Internet of Things?
- LIS2DW12 Accelerometer MakeCode Driver
- Problems with STM32F4 serial port 4 standard library program
- 【FAQ】Microchip Live|Wireless Power Consortium (WPC) compliant wireless charging authentication
- Recruiting part-time dsp training instructors
- Software platform installation for SILICON LABS PG22-DK2503A development board
- CCS CMD file invalidation causes memory allocation failure
- [ADI Practical Knowledge Sharing] The hidden costs of isolation system design cannot be ignored!
- EEWORLD University - Amplifier Design in Test and Measurement
- Design and implementation of FM function in handheld devices