14.1 PWR寄存器结构.............................................................187 14.2 PWR库函数.................................................................188 No 函数名 描述 1 PWR_DeInit 将外设PWR寄存器重设为缺省值 2 PWR_BackupAccessCmd 使能或失能RTC和后备寄存器访问 3 PWR_PVDCmd 使能或者失能可编程电压探测器(PVD) 4 PWR_PVDLevelConfig 设置PVD的探测电压阈值 5 PWR_WakeUpPinCmd 使能或者失能唤醒管脚功能 6 PWR_EnterSTOPMode 进入停止(STOP)模式 7 PWR_EnterSTANDBYMode 进入待命(STANDBY)模式 8 PWR_GetFlagStatus 检查指定PWR标志位设置与否 9 PWR_ClearFlag 清除PWR的待处理标志位 14 功耗控制(PWR)
PWR有多种用途,包括功耗管理和低功耗模式选择。
Section 14.1 PWR寄存器结构描述了固件函数库所使用的数据结构,Section 14.2 固件库函数介绍了函数库 里的所有函数。
14.1 PWR寄存器结构
PWR寄存器结构,PWR_TypeDeff,在文件stm2f10x_map.h中定义如下:
typedef struct {
vu32 CR; vu32 CSR; }PWR_TypeDef;
Table 321.例举了PWR所有寄存器 Table 321. PWR寄存器 寄存器 描述 CR 功耗控制寄存器 CSR 功耗控制状态寄存器 PWR外设声明于文件sm32f10x_map.h:
#define PERIPH_BASE ((u32)0x40000000) #define APB1PERIPH_BASE PERIPH_BASE
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000) #define AHBPERIPH_BASE (PERIPH_BASE + 0x20000) #define PWR_BASE (APB1PERIPH_BASE + 0x7000) #ifndef DEBUG ...
#ifdef _PWR
#define PWR ((PWR_TypeDef *) PWR_BASE) #endif /*_PWR */ ...
#else /* DEBUG */ ...
#ifdef _PWR
EXT PWR_TypeDef *PWR; #endif /*_PWR */ ... #endif
使用Debug模式时,初始化指针PWR于文件:
#ifdef _PWR
PWR = (PWR_TypeDef *) PWR_BASE; #endif /*_PWR */
为了访问PWR寄存器,_PWR必须在文件stm2f10x_conf.h中定义如下:
#define _PWR
14.2 PWR库函数
Table 322. PWR库函数【见首页】
14.2.1 函数PWR_DeInit
Table 323. 函数PWR_DeInit 函数名 PWR_DeInit 函数原形 void PWR_DeInit(void) 功能描述 将外设I2Cx寄存器重设为缺省值(关闭外设) 输入参数 无 输出参数 无 返回值 无 先决条件 无 被调用函数 RCC_APB1PeriphClockCmd(). 例: /* Deinitialize the PWR registers */ PWR_DeInit(); 函数原型如下:
void PWR_DeInit(void) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);// ((u32)0x10000000),PWR外设开启 RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE); // PWR外设关闭 }
14.2.2 函数PWR_BackupAccessCmd
Table 324. 函数 PWR_BackupAccessCmd 函数名 PWR_BackupAccessCmd 函数原形 void PWR_BackupAccessCmd(FunctionalState NewState) 功能描述 使能或失能RTC和后备寄存器访问 输入参数 NewState: RTC和后备寄存器访问的新状态(ENABLE或DISABLE) 输出参数 无 返回值 无 先决条件 无 被调用函数 无 例:
/* Enable access to the RTC and backup registers */ PWR_BackupAccessCmd(ENABLE); 函数原型如下:
void PWR_BackupAccessCmd(FunctionalState NewState) {
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(vu32 *) CR_DBP_BB = (u32)NewState; }
CR.DBP的位操作如下:
#define CR_DBP_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (DBP_BitNumber * 4)) 【1】外设基址别名地址:
#define PERIPH_BB_BASE ((u32)0x42000000) 【2】PWR->CR的偏移地址:
#define PWR_OFFSET (PWR_BASE - PERIPH_BASE) #define PWR_BASE (APB1PERIPH_BASE + 0x7000) #define APB1PERIPH_BASE PERIPH_BASE
#define CR_OFFSET (PWR_OFFSET + 0x00) 【3】DBP位在CR内的位置:
#define DBP_BitNumber 0x08
[结果]&CR_DBP_BB = 0x420E 0020。意义:使能或失能RTC和后备寄存器访问。
14.2.3 函数PWR_PVDCmd
Table 325. 函数PWR_PVDCmd 函数名 PWR_PVDCmd 函数原形 void PWR_PVDCmd(FunctionalState NewState) 功能描述 使能或失能可编程电压探测器(PVD) 输入参数 NewState: PVD的新状态(ENABLE或DISABLE) 输出参数 无 返回值 无 先决条件 无 被调用函数 例: 无 /* Enable the Power Voltage Detector(PVD) */ PWR_PVDCmd(ENABLE); 函数原型如下:
void PWR_PVDCmd(FunctionalState NewState) {
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(vu32 *) CR_PVDE_BB = (u32)NewState; }
CR.PVDE的位操作如下:
#define CR_PVDE_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PVDE_BitNumber * 4)) 【1】外设基址别名地址:
#define PERIPH_BB_BASE ((u32)0x42000000) 【2】PWR->CR的偏移地址:
#define PWR_OFFSET (PWR_BASE - PERIPH_BASE) #define PWR_BASE (APB1PERIPH_BASE + 0x7000) #define APB1PERIPH_BASE PERIPH_BASE
#define CR_OFFSET (PWR_OFFSET + 0x00) 【3】DBP位在CR内的位置:
#define PVDE_BitNumber 0x04
[结果]&CR_PVDE_BB = 0x420E 0010。意义:电源电压检测器使能或失能。
14.2.4 函数PWR_PVDLevelConfig
Table 326. 函数PWR_PVDLevelConfig 函数名 PWR_PVDLevelConfig 函数原形 void PWR_PVDLevelConfig(u32 PWR_PVDLevel) 功能描述 设置PVD的探测电压阈值 输入参数 PWR_PVDLevel:PVD的探测电压阈值 输出参数 无 返回值 无 先决条件 无 被调用函数 无 PWR_PVDLevel:该参数设置了PVD的探测电压阈值(见Table 327.) Table 327. PWR_PVDLevel值 PWR_PVDLevel 描述CR. PLS[2:0]/bit7-5 #define Val PWR_PVDLevel_2V2 PVD探测电压阈值2.2V 0x00000000 PWR_PVDLevel_2V3 PVD探测电压阈值2.3V 0x00000020 PWR_PVDLevel_2V4 PVD探测电压阈值2.4V 0x00000040 PWR_PVDLevel_2V5 PVD探测电压阈值2.5V 0x00000060 PWR_PVDLevel_2V6 PVD探测电压阈值2.6V 0x00000080 PWR_PVDLevel_2V7 PVD探测电压阈值2.7V 0x000000A0 PWR_PVDLevel_2V8 PVD探测电压阈值2.8V 0x000000C0 PWR_PVDLevel_2V9 PVD探测电压阈值2.9V 0x000000E0 例: /* Set PVD detection level to 2.5V */ PWR_PVDLevelConfig(PWR_PVDLevel_2V5); 函数原型如下:
void PWR_PVDLevelConfig(u32 PWR_PVDLevel) {
u32 tmpreg = 0;
/* Check the parameters */
assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel));
tmpreg = PWR->CR;
/* Clear PLS[7:5] bits */
tmpreg &= CR_PLS_Mask;//0xFFFF FF1F
/* Set PLS[7:5] bits according to PWR_PVDLevel value */
tmpreg |= PWR_PVDLevel;
/* Store the new value */ PWR->CR = tmpreg; }
14.2.5 函数PWR_WakeUpPinCmd
Table 328. 函数PWR_WakeUpPinCmd 函数名 PWR_WakeUpPinCmd 函数原形 void PWR_WakeUpPinCmd(FunctionalState NewState) 功能描述 使能或失能唤醒管脚功能 输入参数 NewState: 唤醒管脚功能的新状态(ENABLE或DISABLE) 输出参数 无 返回值 无 先决条件 无 被调用函数 无 例: /* WakeUp pin used for wake-up function */ PWR_WakeUpPinCmd(ENABLE); 函数原型如下:
void PWR_WakeUpPinCmd(FunctionalState NewState) {
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(vu32 *) CSR_EWUP_BB = (u32)NewState; }
CR.PVDE的位操作如下:
#define CSR_EWUP_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (EWUP_BitNumber * 4)) 【1】外设基址别名地址:
#define PERIPH_BB_BASE ((u32)0x42000000) 【2】PWR->CR的偏移地址:
#define PWR_OFFSET (PWR_BASE - PERIPH_BASE) #define PWR_BASE (APB1PERIPH_BASE + 0x7000) #define APB1PERIPH_BASE PERIPH_BASE
#define CSR_OFFSET (PWR_OFFSET + 0x04) 【3】DBP位在CR内的位置:
#define EWUP_BitNumber 0x08
[结果]&CSR_EWUP_BB = 0x 420E 00A0。意义:使能WKUP引脚。
14.2.6 函数PWR_EnterSTOPMode
Table 329. 函数PWR_EnterSTOPMode 函数名 PWR_EnterSTOPMode 函数原形 void PWR_EnterSTOPMode(u32 PWR_Regulator, u8 PWR_STOPEntry) 功能描述 进入停止(STOP)模式 输入参数 1 PWR_Regulator: 电压转换器在停止模式下的状态 输入参数 2 PWR_STOPEntry: 选择使用指令WFE还是WFI来进入停止模式 输出参数 无 返回值 无 先决条件 无 被调用函数 __WFI(), __WFE() PWR_Regulator:该参数设置了电压转换器在停止模式下的状态(见Table 330.) Table 330. PWR_Regulator值 PWR_Regulator 描述 PWR_Regulator_ON 停止模式下电压转换器ON 0x00000000 PWR_Regulator_LowPower 停止模式下电压转换器进入低功耗模式 0x00000001 PWR_STOPEntry :该参数选择使用指令WFE还是 WFI来进入停止模式(见Table 331.) Table 331. PWR_Regulator值 PWR_STOPEntry 描述 PWR_STOPEntry_WFI 使用指令WFI来进入停止模式 0x01 PWR_STOPEntry_WFE 例: 使用指令WFE来进入停止模式 0x02 /* Put the system in STOP mode with regulator on */
PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFE); 函数原型如下:
void PWR_EnterSTOPMode(u32 PWR_Regulator, u8 PWR_STOPEntry) {
u32 tmpreg = 0;
/* Check the parameters */
assert_param(IS_PWR_REGULATOR(PWR_Regulator)); assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));
/* Select the regulator state in STOP mode ---------------------------------*/ tmpreg = PWR->CR;
/* Clear PDDS and LPDS bits */ tmpreg &= CR_DS_Mask;
/* Set LPDS bit according to PWR_Regulator value */ tmpreg |= PWR_Regulator;
/* Store the new value */ PWR->CR = tmpreg;
/* Set SLEEPDEEP bit of Cortex System Control Register */ *(vu32 *) SCB_SysCtrl |= SysCtrl_SLEEPDEEP_Set;
//SCB->SCR. SLEEPDEEP(bit2),用地址直接访问,避免调用SCB库函数。 //&SCB_SysCtrl = 0xE000ED10;SysCtrl_SLEEPDEEP_Set = 0x00000004
/* Select STOP mode entry --------------------------------------------------*/ if(PWR_STOPEntry == PWR_STOPEntry_WFI) {
/* Request Wait For Interrupt */ __WFI(); } else {
/* Request Wait For Event */ __WFE(); } }
上述两个被调用函数的汇编形式如下:
;【1】******************************************************************************* ; Function Name : __WFI
; Description : Assembler function for the WFI instruction. ; Input : None ; Return : None
;******************************************************************************* __WFI
WFI//命令:休眠并且在发生事件时被唤醒 BX r14//转移到由寄存器r14给出的地址
;【2】******************************************************************************* ; Function Name : __WFE
; Description : Assembler function for the WFE instruction. ; Input : None ; Return : None
;******************************************************************************* __WFE
WFE//命令:休眠并且在发生中断时被唤醒
BX r14//转移到由寄存器r14给出的地址
14.2.7 函数PWR_EnterSTANDBYMode
Table 332. 函数PWR_EnterSTANDBYMode 函数名 PWR_EnterSTANDBYMode 函数原形 void PWR_EnterSTANDBYMode(void) 功能描述 进入待命(STANDBY)模式 输入参数 无 输出参数 无