本篇文章记录STM32C092型单片机进入四种低功耗模式的评测内容及程序。
作为本次测评的进阶内容,这篇文章我准备了很久,尤其是我从来没有玩过单片机的低功耗模式,但是,最后我成功了!以下几套程序的现象是一致的,MCU先配置各种模式,并闪烁LED1 5次。并打印当前的示例程序是进入什么模式的,然后进入while循环,LED2闪烁代表当前为RUN模式,当按下PA0进入低功耗,按下PC13退出低功耗模式,LED2继续闪烁。
睡眠和停止模式下,只需要一个外部中断就可以将其唤醒。所以把PC13配置中断就好,可以参考第一段例程的配置。
睡眠及停止模式配置如下:



睡眠模式:MCU先配置睡眠模式,并闪烁LED1 5次。并打印当前的示例程序是进入睡眠模式,当进入while循环,LED2闪烁代表当前为RUN模式,当按下PA0进入睡眠模式,按下PC13退出睡眠模式,LED2继续闪烁。现象如下:

串口打印如下:

功耗情况如下:

程序如下:
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "stm32c0xx_it.h"
/*定义切换模式用变量*/
int i;
/*函数声明*/
void SystemClock_Config(void);
void LED_Blink(uint8_t Num,int ms);
void Sleep_Mode(void);
/*主函数*/
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
LED_Blink(5,100);//开始先让LED1闪烁5次
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo10:Sleep_Mode\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
/*进入循环体*/
while (1){
while(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_RESET){
HAL_Delay(10);
if(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_SET){//如果按键按下
Sleep_Mode();//进入低功耗模式(Stop模式)
}
}
/*正常情况下一直闪烁LED2*/
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
HAL_Delay(500);
}
}
void Sleep_Mode(void)
{
HAL_SuspendTick();
HAL_PWR_EnterSLEEPMode (PWR_MAINREGULATOR_ON ,PWR_SLEEPENTRY_WFI );
HAL_ResumeTick ();
}
/*进入外部中断时处理*/
void EXTI4_15_IRQHandler(void){
HAL_GPIO_EXTI_IRQHandler(User_button_Pin);//清除用户按键产生的中断
}
/*控制LED闪烁(闪烁次数及延时时间,仅仅控制LED1)*/
void LED_Blink(uint8_t Num,int ms){
for(uint8_t i=0;i<Num;i++){
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_SET);
HAL_Delay(ms);
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_RESET);
HAL_Delay(ms);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
停止模式:MCU先配置停止模式,并闪烁LED1 5次。并打印当前的示例程序是进入停止模式,当进入while循环,LED2闪烁代表当前为RUN模式,当按下PA0进入停止模式,按下PC13退出停止模式,LED2继续闪烁。现象如下:
串口打印如下:

功耗情况如下:

程序如下:
/*
*作者:下行路轨上的C70E 论坛ID钟详
*Nucleo-C092 demo程序任务8:使用按键进入MCU的Stop模式,并配置PC13唤醒单片机
*正常情况下LED2闪烁运行,当按下Key1,进入低功耗模式,按下PC13退出低功耗
*使用意法半导体Nucleo板卡,配置外置晶体48M
*本次列车终点站浦东一号二号航站楼,下一站人民广场,开左边门,可换乘1号线、8号线
*/
/*包含所需头文件*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "stm32c0xx_it.h"
/*定义切换模式用变量*/
int i;
/*函数声明*/
void SystemClock_Config(void);
void LED_Blink(uint8_t Num,int ms);
void Stop_Mode(void);
/*主函数*/
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
LED_Blink(5,100);//开始先让LED1闪烁5次
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo9:Stop_Mode\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
/*进入循环体*/
while (1){
while(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_RESET){
HAL_Delay(10);
if(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_SET){//如果按键按下
Stop_Mode();//进入低功耗模式(Stop模式)
}
}
/*正常情况下一直闪烁LED2*/
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
HAL_Delay(500);
}
}
void Stop_Mode(void)
{
HAL_SuspendTick();
HAL_PWR_EnterSTOPMode (PWR_MAINREGULATOR_ON ,PWR_STOPENTRY_WFI );
//唤醒后需要重新配置RCC时钟
SystemClock_Config();
HAL_ResumeTick ();
}
/*进入外部中断时处理*/
void EXTI4_15_IRQHandler(void){
HAL_GPIO_EXTI_IRQHandler(User_button_Pin);//清除用户按键产生的中断
}
/*控制LED闪烁(闪烁次数及延时时间,仅仅控制LED1)*/
void LED_Blink(uint8_t Num,int ms){
for(uint8_t i=0;i<Num;i++){
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_SET);
HAL_Delay(ms);
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_RESET);
HAL_Delay(ms);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
待机和关断模式需要把PC13配置成专用的wakeup引脚,在按下wakeup引脚后才可以进行唤醒。
待机及关断配置如下:



待机模式:MCU先配置待机模式,并闪烁LED1 5次。并打印当前的示例程序是进入待机模式,当进入while循环,LED2闪烁代表当前为RUN模式,当按下PA0进入待机模式,按下PC13退出待机模式,程序会从头执行,因为待机模式会清掉单片机中的RAM中的内容,所以它从头执行了。现象如下:

串口打印如下:

功耗情况如下:

程序如下:
/*包含所需头文件*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
/*函数声明*/
void SystemClock_Config(void);
void LED_Blink(uint8_t Num,int ms);
void Enter_Standby(void);
/*主函数*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
LED_Blink(5,100);//开始先让LED1闪烁5次
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo8:Standby_Mode\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
/*进入循环体*/
while (1){
while(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_RESET){
HAL_Delay(10);
if(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_SET){//如果按键按下
Enter_Standby();//进入低功耗模式(ShutDown模式)
}
}
/*正常情况下一直闪烁LED2*/
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
HAL_Delay(500);
}
}
/*控制LED闪烁(闪烁次数及延时时间,仅仅控制LED1)*/
void LED_Blink(uint8_t Num,int ms){
for(uint8_t i=0;i<Num;i++){
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_SET);
HAL_Delay(ms);
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_RESET);
HAL_Delay(ms);
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* 进入低功耗模式 */
void Enter_Standby(void){
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN2 );
__HAL_PWR_CLEAR_FLAG (PWR_FLAG_WUF2);
HAL_PWR_EnterSTANDBYMode ();
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
关断模式:MCU先配置关断模式,并闪烁LED1 5次。并打印当前的示例程序是进入关断模式,当进入while循环,LED2闪烁代表当前为RUN模式,当按下PA0进入关断模式,按下PC13退出关断模式,程序会从头执行,因为关断模式会清掉单片机中的RAM中的内容,所以它从头执行了。现象如下:

串口打印如下:

功耗情况如下:
是的,我的15B万能表测不出当前模式消耗了多少电流,它的功耗太低,无法被表探测到。

程序如下:
/*
*作者:下行路轨上的C70E 论坛ID钟详
*Nucleo-C092 demo程序任务7:使用按键进入MCU的ShutDown模式,并配置PC13唤醒单片机
*正常情况下LED2闪烁运行,当按下Key1,进入低功耗模式,按下PC13退出低功耗,程序开始从头执行
*使用意法半导体Nucleo板卡,配置外置晶体48M
*本次列车终点站浦东一号二号航站楼,下一站淞虹路,开左边门
*/
/*包含所需头文件*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
/*函数声明*/
void SystemClock_Config(void);
void LED_Blink(uint8_t Num,int ms);
void Enter_Shutdown(void);
/*主函数*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
LED_Blink(5,100);//开始先让LED1闪烁5次
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo7:ShutDown_Mode\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
/*进入循环体*/
while (1){
while(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_RESET){
HAL_Delay(10);
if(HAL_GPIO_ReadPin(key1_GPIO_Port,key1_Pin)==GPIO_PIN_SET){//如果按键按下
Enter_Shutdown();//进入低功耗模式(ShutDown模式)
}
}
/*正常情况下一直闪烁LED2*/
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin (User_LED2_GPIO_Port,User_LED2_Pin,GPIO_PIN_SET);
HAL_Delay(500);
}
}
/*控制LED闪烁(闪烁次数及延时时间,仅仅控制LED1)*/
void LED_Blink(uint8_t Num,int ms){
for(uint8_t i=0;i<Num;i++){
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_SET);
HAL_Delay(ms);
HAL_GPIO_WritePin (User_LED1_GPIO_Port,User_LED1_Pin,GPIO_PIN_RESET);
HAL_Delay(ms);
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* 进入低功耗模式 */
void Enter_Shutdown(void){
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN2);
__HAL_PWR_CLEAR_FLAG (PWR_FLAG_WUF2);
HAL_PWREx_EnterSHUTDOWNMode();
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
所以,我们完成了对于MCU的低功耗模式的评测,可以说,这个MCU功耗真的很低,尤其是关断模式。