本文主要是介绍【STM32】STM32学习笔记-修改主频 睡眠模式 停止模式 待机模式(45),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
00. 目录
文章目录
- 00. 目录
- 01. PWR简介
- 02. 修改主频接线图
- 03. 修改主频相关API
- 04. 修改主频程序示例
- 05. 睡眠模式接线图
- 06. 睡眠模式相关API
- 07. 睡眠模式程序示例
- 08. 停止模式接线图
- 09. 停止模式相关API
- 10. 停止模式程序示例
- 11. 待机模式接线图
- 12. 待机模式相关API
- 13. 待机模式程序示例
- 14. 示例程序下载
- 15. 附录
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.* ******************************************************************************/
修改主频的方法
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
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);
#endifOLED_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;}
05. 睡眠模式接线图
06. 睡眠模式相关API
#define __NOP __nop
#define __WFI __wfi
#define __WFE __wfe
07. 睡眠模式程序示例
main.c
#include "stm32f10x.h"
#include <stdio.h>
#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;}
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
返回值:无
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 来进入停止模式
返回值:无
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;}
11. 待机模式接线图
12. 待机模式相关API
PWR_EnterSTANDBYMode函数
/*** @brief Enters STANDBY mode.* @param None* @retval None*/
void PWR_EnterSTANDBYMode(void)
功能:进入待机(STANDBY)模式
参数:返回值:
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
返回值:
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;}
14. 示例程序下载
34-修改主频.rar
35-睡眠模式-UART发送和接收.rar
36-停止模式-对射式红外传感器计次.rar
37-待机模式-实时时钟.rar
15. 附录
参考: 【STM32】江科大STM32学习笔记汇总
这篇关于【STM32】STM32学习笔记-修改主频 睡眠模式 停止模式 待机模式(45)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!