• 【STM32】STM32学习笔记-修改主频 睡眠模式 停止模式 待机模式(45)


    00. 目录

    01. PWR简介

    • PWR(Power Control)电源控制

    • PWR负责管理STM32内部的电源供电部分,可以实现可编程电压监测器和低功耗模式的功能

    • 可编程电压监测器(PVD)可以监控VDD电源电压,当VDD下降到PVD阀值以下或上升到PVD阀值之上时,PVD会触发中断,用于执行紧急关闭任务

    • 低功耗模式包括睡眠模式(Sleep)、停机模式(Stop)和待机模式(Standby),可在系统空闲时,降低STM32的功耗,延长设备使用时间

    02. 修改主频接线图

    在这里插入图片描述

    03. 修改主频相关API

      /******************************************************************************
      * @file    system_stm32f10x.c
      * @author  MCD Application Team
      * @version V3.5.0
      * @date    11-March-2011
      * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
      * 
      * 1.  This file provides two functions and one global variable to be called from 
      *     user application:
      *      - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
      *                      factors, AHB/APBx prescalers and Flash settings). 
      *                      This function is called at startup just after reset and 
      *                      before branch to main program. This call is made inside
      *                      the "startup_stm32f10x_xx.s" file.
      *
      *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
      *                                  by the user application to setup the SysTick 
      *                                  timer or configure other parameters.
      *                                     
      *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
      *                                 be called whenever the core clock is changed
      *                                 during program execution.
      *
      * 2. After each device reset the HSI (8 MHz) is used as system clock source.
      *    Then SystemInit() function is called, in "startup_stm32f10x_xx.s" file, to
      *    configure the system clock before to branch to main program.
      *
      * 3. If the system clock source selected by user fails to startup, the SystemInit()
      *    function will do nothing and HSI still used as system clock source. User can 
      *    add some code to deal with this issue inside the SetSysClock() function.
      *
      * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depedning on
      *    the product used), refer to "HSE_VALUE" define in "stm32f10x.h" file. 
      *    When HSE is used as system clock source, directly or through PLL, and you
      *    are using different crystal you have to adapt the HSE value to your own
      *    configuration.
      *        
      ******************************************************************************/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    修改主频的方法

    system_stm32f10x.c 106行

    #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
    /* #define SYSCLK_FREQ_HSE    HSE_VALUE */
     #define SYSCLK_FREQ_24MHz  24000000
    #else
    /* #define SYSCLK_FREQ_HSE    HSE_VALUE */
    /* #define SYSCLK_FREQ_24MHz  24000000 */ 
       #define SYSCLK_FREQ_36MHz  36000000 
    /* #define SYSCLK_FREQ_48MHz  48000000 */
    /* #define SYSCLK_FREQ_56MHz  56000000 */
    //#define SYSCLK_FREQ_72MHz  72000000
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    04. 修改主频程序示例

    main.c

    #include "stm32f10x.h"
    
    #include "delay.h"
    #include "oled.h"
    
    
     int main(void)
     {	
    
    	 
    	 
    	 //初始化
    	 OLED_Init();
    #if 0
    	 //显示一个字符
    	 OLED_ShowChar(1, 1, 'A');
    	 //显示字符串
    	 OLED_ShowString(1, 3, "HelloWorld!");
    	 //显示十进制数字
    	 OLED_ShowNum(2, 1, 12345, 5);
    	 //显示有符号十进制数
    	 OLED_ShowSignedNum(2, 7, -66, 2);
    	 //显示十六进制
    	 OLED_ShowHexNum(3, 1, 0xAA55, 4);
    	 //显示二进制数字
    	 OLED_ShowBinNum(4, 1, 0xAA55, 16);
    #endif
    
    	 OLED_ShowString(1, 1, "SYSCLK:");
    	 OLED_ShowNum(1, 8, SystemCoreClock, 8);
    
    
    	 while(1)
    	 {
    		OLED_ShowString(2, 1, "Running");
    		delay_ms(500);
    		 
    		OLED_ShowString(2, 1, "       ");		 
    		delay_ms(500);	 	 
    	 }
    	 
    	 return 0;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    05. 睡眠模式接线图

    在这里插入图片描述

    06. 睡眠模式相关API

    #define __NOP                             __nop
    #define __WFI                             __wfi
    #define __WFE                             __wfe
    
    • 1
    • 2
    • 3

    07. 睡眠模式程序示例

    main.c

    #include "stm32f10x.h"
    #include 
    #include "delay.h"
    #include "oled.h"
    #include "uart.h"
    
    
     int main(void)
     {	
    	 uint16_t data = 0;
    	 
    	 OLED_Init();
    	 
    	 uart_init();
    	 
    	 //中断分组
    	 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    	 OLED_ShowChar(1, 1, 'A');
    
    
    	 while(1)
    	 {
    		if (1 == uart_getRxFlag())
    		{
    			data = uart_getRxData();
    			uart_send_byte(data);
    			OLED_ShowHexNum(1, 1, data, 2);
    		}
    		 
    		OLED_ShowString(2, 1, "Running");
    		delay_ms(100);
    		 
    		OLED_ShowString(2, 1, "       ");		 
    		delay_ms(100);	
    		
    		
    		//进入睡眠模式
    		__WFI();
    	 }
    	 
    	 return 0;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    08. 停止模式接线图

    在这里插入图片描述

    09. 停止模式相关API

    RCC_APB1PeriphClockCmd函数

    /**
      * @brief  Enables or disables the Low Speed APB (APB1) peripheral clock.
      * @param  RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.
      *   This parameter can be any combination of the following values:
      *     @arg RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,
      *          RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,
      *          RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,
      *          RCC_APB1Periph_USART2, RCC_APB1Periph_USART3, RCC_APB1Periph_USART4, 
      *          RCC_APB1Periph_USART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,
      *          RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,
      *          RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, RCC_APB1Periph_CEC,
      *          RCC_APB1Periph_TIM12, RCC_APB1Periph_TIM13, RCC_APB1Periph_TIM14
      * @param  NewState: new state of the specified peripheral clock.
      *   This parameter can be: ENABLE or DISABLE.
      * @retval None
      */
    void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
    功能:
    	使能或者失能 APB1 外设时钟
    参数:
    	RCC_APB1Periph: 门控 APB1 外设时钟
    	NewState:指定外设时钟的新状态,这个参数可以取:ENABLE 或者 DISABLE
    返回值:
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    PWR_EnterSTOPMode函数

    /**
      * @brief  Enters STOP mode.
      * @param  PWR_Regulator: specifies the regulator state in STOP mode.
      *   This parameter can be one of the following values:
      *     @arg PWR_Regulator_ON: STOP mode with regulator ON
      *     @arg PWR_Regulator_LowPower: STOP mode with regulator in low power mode
      * @param  PWR_STOPEntry: specifies if STOP mode in entered with WFI or WFE instruction.
      *   This parameter can be one of the following values:
      *     @arg PWR_STOPEntry_WFI: enter STOP mode with WFI instruction
      *     @arg PWR_STOPEntry_WFE: enter STOP mode with WFE instruction
      * @retval None
      */
    void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)
    功能:
    	进入停止(STOP)模式
    参数:
    	PWR_Regulator: 电压转换器在停止模式下的状态
    	PWR_STOPEntry: 选择使用指令 WFE 还是 WFI 来进入停止模式
    返回值:
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    10. 停止模式程序示例

    main.c

    #include "stm32f10x.h"
    
    #include "delay.h"
    #include "oled.h"
    #include "CountSensor.h"
    
     int main(void)
     {		 
    	//初始化
    	OLED_Init();
    	 
    	 //开启PWR时钟
    	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    
    	 OLED_ShowString(1, 1, "Count:");
    	 
    	 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    	 
    	count_sensor_init();
    
    	 
    
    	while(1)
    	{
    	 
    		OLED_ShowNum(1 , 7, CountSensor_Get(), 5);
    		
    		OLED_ShowString(2, 1, "Running");
    		delay_ms(100);
    		 
    		OLED_ShowString(2, 1, "       ");		 
    		delay_ms(100);	
    		
    		//进入停止模式
    		PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFI);
    		
    		//系统重新初始化
    		SystemInit();
    	}
    	 
    	 return 0;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    11. 待机模式接线图

    在这里插入图片描述

    12. 待机模式相关API

    PWR_EnterSTANDBYMode函数

    /**
      * @brief  Enters STANDBY mode.
      * @param  None
      * @retval None
      */
    void PWR_EnterSTANDBYMode(void)
    功能:
    	进入待机(STANDBY)模式
    参数:
    
    返回值:
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    PWR_WakeUpPinCmd函数

    /**
      * @brief  Enables or disables the WakeUp Pin functionality.
      * @param  NewState: new state of the WakeUp Pin functionality.
      *   This parameter can be: ENABLE or DISABLE.
      * @retval None
      */
    void PWR_WakeUpPinCmd(FunctionalState NewState)
    功能:
    	使能或者失能唤醒管脚功能
    参数:
    	NewState: 唤醒管脚功能的新状态,这个参数可以取:ENABLE 或者 DISABLE
    返回值:  
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    13. 待机模式程序示例

    main.c

    #include "stm32f10x.h"
    #include "delay.h"
    #include "oled.h"
    #include "key.h"
    #include "rtc.h"
    
     int main(void)
     {	
    	 uint32_t alarm = 0;
    	 
    	 //初始化
    	 OLED_Init();
    	 
    	 key_init();
    	 
    	 rtc_init();
     
    	 //开启PWR时钟
    	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    
    	 OLED_ShowString(1, 1, "CNT: ");
    	 OLED_ShowString(2, 1, "ALR: ");
    	 OLED_ShowString(3, 1, "ALRF: ");
    	 
    	 PWR_WakeUpPinCmd(ENABLE);
    	 
    	 alarm = RTC_GetCounter() + 10;
    	 RTC_SetAlarm(alarm);
    	 OLED_ShowNum(2, 6, alarm, 10);
    		 
    	 while(1)
    	 {	 			
    		OLED_ShowNum(1, 6, RTC_GetCounter(), 10);	//显示32位的秒计数器		
    		OLED_ShowNum(3, 6, RTC_GetFlagStatus(RTC_FLAG_ALR), 1);
    		 
    		OLED_ShowString(4, 1, "Running");
    		delay_ms(100);
    		 
    		OLED_ShowString(4, 1, "       ");		 
    		delay_ms(1000);	 
    		 
    		OLED_ShowString(4, 9, "STANDBY");
    		delay_ms(100);
    		 
    		OLED_ShowString(4, 9, "       ");		 
    		delay_ms(100);	 
    		 
    		 OLED_Clear();
    
    		PWR_EnterSTANDBYMode();
    	 }
    	 
    	 return 0;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    14. 示例程序下载

    34-修改主频.rar

    35-睡眠模式-UART发送和接收.rar

    36-停止模式-对射式红外传感器计次.rar

    37-待机模式-实时时钟.rar

    15. 附录

    参考: 【STM32】江科大STM32学习笔记汇总

  • 相关阅读:
    商标的跨类别保护
    深度解析linux内核模块编译makefile
    java计算机毕业设计ETC用户自驾游推荐系统MyBatis+系统+LW文档+源码+调试部署
    Apollo学习笔记(27)李群、李代数
    UNIX环境高级编程 第2章 UNIX标准化及实现
    【Docker项目实战】使用Docker部署HFish蜜罐系统
    Python 图片处理笔记
    centos离线安装fastdfs
    试用程序实现不使用缓存字节数组的方法复制C盘根目录下的a,jpg到E盘下的a.jpg
    uniapp-引入模块失效的问题
  • 原文地址:https://blog.csdn.net/u010249597/article/details/136354647