Function: The MCU samples at a certain sampling frequency (such as 2Khz). For example, every minute (60s), only the first 30s are sampled, and the last 30s are dormant (timed dormancy wake-up). It automatically wakes up again in the next minute, and repeats this cycle for 30 minutes. After 30 minutes, the MCU enters the dormant stop mode. The dormant MCU is awakened by serial port interruption.
Implementation:
1. Timing sleep wake-up code
HAL_Delay(2000); //Sampling time
HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 2, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); //Call library function to enter STOP mode
SystemClockConfig_STOP(); //Wake up after stop mode and restore clock source
count1++;
while(count1==1)//Number of loops
{
Rx_Init();
count1=0;
flag3=0;
__HAL_RTC_ALARM_DISABLE_IT(&RTCHandle, RTC_IT_ALRA);
__HAL_RTC_ALARM_CLEAR_FLAG(&RTCHandle, RTC_IT_ALRA);
HAL_TIM_Base_Stop(&TimHandle); //Timer stop
HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); //Call library function to enter STOP mode
}
Calculation method: For example, sleep for 4 seconds
/* RTC Wakeup Interrupt Generation:
Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI))
Wakeup Time = Wakeup Time Base * WakeUpCounter
= (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter
==> WakeUpCounter = Wakeup Time / Wakeup Time BaseTo configure the wake up timer to 4s the WakeUpCounter is set to 0x1FFF:
RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
Wakeup Time Base = 16 /(~39.000KHz) = ~0,410 ms
Wakeup Time = ~4s = 0,410ms * WakeUpCounter
==> WakeUpCounter = ~4s/0,410ms = 9750 = 0x2616 */
HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x2616, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
Limitation: Using internal clock LSI, the fixed size leads to the limitation of Wakeup Time Base. Wakeup Time (maximum) = 0.41ms*65535 (0xffff) = 26.869s, which may not meet the sleep time requirement.
Another method: change HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 2, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
Here RTC_WAKEUPCLOCK_CK_SPRE_16BITS sets the lower three bits in the CR register, the wake-up clock selection, here select 1hz, 2 means entering the wake-up interrupt after working for 2S, this number can be changed at will, and the amount of time it is changed to is the amount of sleep time.
2. Serial port interrupt wakeup
Wake-up mechanism: After the MCU enters the STOP state, it cannot be directly awakened by UART and other peripherals. Before the MCU enters the STOP state, set the RX pin to EXTI mode and enable the corresponding interrupt. After waking up, reinitialize the serial port and configure the clock. (Here are two codes, one is the code of Worms that I referred to, and the other is my own)
by: Worms
void Rx_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
// Configure the Rx pin of UART1 to EXIT mode
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; // Pay special attention to the interrupt mode here
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin==USARTx_RX_PIN)
{
// HAL_NVIC_EnableIRQ(SysTick_IRQn);
SystemClockConfig_STOP(); //Wake up after stop mode and restore clock source
HAL_Init();
SystemClock_Config();
SystemPower_Config();
HAL_SPI_DeInit(&hspi1);
HAL_GPIO_DeInit(GPIOA,GPIO_PIN_10); //When the serial port is reinitialized, the HAL_UART_Init() function will judge if(huart->State == HAL_UART_STATE_RESET), then HAL_UART_MspInit(huart) will be executed to configure the serial IO port. This sentence must be included!
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
SPI_GPIO_Init();
UartHandle.Instance = USARTx;
UartHandle.Init.BaudRate = uart_baud;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
/* Enable USART2 clock */
USARTx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH ;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);
__HAL_GPIO_EXTI_CLEAR_IT(USARTx_RX_PIN);
HAL_NVIC_SetPriority(USARTx_IRQn, 4, 0);
HAL_NVIC_EnableIRQ(USARTx_IRQn);
}
}
Finish!
Previous article:Read and write data between STM32 and Flash AT45DB321D
Next article:Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep (STOP) mode
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- The board for showing goods + collecting dust
- Principle of single chip microcomputer
- Practical tutorial | How to use Wangyou DFM software for cold plate analysis
- KiCad batch adds footprints to components
- RC voltage reduction principle
- Thank you + my friends
- Detailed explanation of the grounding problem of 50HZ interference oscilloscope
- SigmaTel Launches STDC2150 SoC Solution
- 【GD32307E-START】Receiving goods + Preliminary impression + Data download + Power on and run DEMO
- I want to ask the master to solve the detection circuit for POE power supply PD end