This is the third day of my participation in Gwen Challenge
Stm32 microcontroller low power mode of a total of three, three kinds of low power analysis test. First, read the manual about low power consumption:
Let’s start with sleep patterns:
Getting into sleep mode is relatively easy, with just one command. To wake up the sleep mode, we use the serial port interrupt to wake up by sending data to the serial port. To wake up the sleep mode, use the serial port RXD pin as follows:
Void sleep_mode_wfi(void) {__WFI(); Void sleep_mode_wfe(void) {__WFE(); void sleep_mode_wfe(); }Copy the code
By calling these two functions, you can enter sleep mode directly and send data to the serial port to exit sleep mode. After the sleep mode exits, the program continues on the next line of code that enters sleep mode. The program is suspended. Down to the stop mode:
Stop mode The stop mode can be entered by calling the library function PWR_EnterSTOPMode (). To wake up in stop mode, an external interrupt is required. The RXT pin of the serial port is set to an external interrupt and the rising edge is triggered. This allows you to wake up stop mode directly through the serial port. The relevant codes are as follows:
void EXIT_UART_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource10); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); EXTI_InitStructure.EXTI_Line = EXTI_Line10; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); } void EXTI15_10_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line10) ! = RESET) { EXTI_ClearITPendingBit(EXTI_Line10); __set_FAULTMASK(1); // Close all interrupts NVIC_SystemReset(); WKUP cannot be awakened void enter_stop_mode(void) {EXIT_UART_Init(); //RX pins are configured as external interrupts RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFI); // Enter shutdown mode}Copy the code
After you enter the stop mode, all I/O ports remain in the current state. When exiting stop mode, the HSI RC oscillator will be selected as the system clock, whereas the development version uses an external clock and will need to reset the system clock. In order to avoid the trouble of resetting the clock, execute the system software reset command directly in the interrupt program. Directly reset the system. If the external I/O port status needs to remain unchanged in a project, you need to reconfigure the system clock and other peripherals instead of using the system reset command. Let’s look at the machine model:
In standby mode, the system consumes the lowest power and external I/O ports are in high resistance state. After exiting the standby mode, the system reinitializes, which is equivalent to pressing the reset button. The relevant codes are as follows:
void Sys_Standby(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // ENABLE the PWR peripheral clock PWR_WakeUpPinCmd(ENABLE); WKUP PWR_EnterSTANDBYMode(); Void Sys_Enter_Standby(void) {void Sys_Enter_Standby(void) { RCC_APB2PeriphResetCmd(0X01FC, DISABLE); Sys_Standby(); // Reset all IO ports. }Copy the code
WKUP wake up was used for easy testing. After entering the low-power mode, press the WKUP button and the system will wake up. After waking up, the system will reset and the program will restart. The main function test code is as follows:
int main(void) { u8 i = 0, j = 0; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); delay_init(); // the delay function initializes LED_Init(); // Initialize the hardware interface connected to the LED KEY_Init(); uart_init(9600); LED = 1; delay_ms(500); printf("low power test! \r\n\r\n"); while(1) { i = KEY_Scan(1); switch(i) { case 0: break; Case 1: printf(" Enter shutdown mode \r\n\r\n"); enter_stop_mode(); Printf (" Exit shutdown mode \r\n\r\n"); // Cannot execute this break; Case 2: printf(" Enter standby mode \r\n\r\n"); Sys_Enter_Standby(); Printf (" Exit standby mode \r\n\r\n"); // Cannot execute this break; Case 3: printf(" Enter sleep mode interrupt wake up \r\n\r\n"); sleep_mode_wfi(); // Wake up followed by the next statement printf(" wake up \r\n\r\n"); // Wake up and execute the current statement break; Case 4: printf(" Wake up \r\n\r\n"); sleep_mode_wfe(); // Wake up followed by the next statement printf(" wake up \r\n\r\n"); // Wake up and execute the current statement break; } j++; if(j > 5) { j = 0; LED = ! LED; } delay_ms(10); }}Copy the code
The program execution status is indicated by LED flashing, and the LED light stops flashing after entering the low-power mode. After exiting the low-power mode, the LED continues to blink and output related information through the serial port for easy observation.