STM32系列 STM32F4xx sleep mode

现在正在进行项目预研,需要各模块在空闲时进入待机模式,MCU选择sleep mode。

MCU节电模式

MCU支持三种节电模式Sleep mode、Deep-sleep mode、Standby mode
根据datasheet描述,在sleep mode下,只有CPU核心的时钟是关闭的,其他所有外设继续运行,任何中断/事件都可以唤醒系统。
在这里插入图片描述

MCU的Sleep mode

进入Sleep mode

执行WFI(等待中断)或WFE(等待事件)指令可进入Sleep mode,根据SCR中SLEEPONEXIT位的设置,可以通过两种方案选择进入Sleep mode:

  • 立即Sleep mode:如果SLEEPONEXIT位清零,MCU将执行WFI或WFE指令立即进入Sleep mode
  • 退出时Sleep mode:如果SLEEPONEXIT位置1,MCU将在退出优先级最低的ISR时立即进入Sleep mode

退出Sleep mode

如果使用WFI指令进入Sleep mode,则NVIC确认的任意外设中断都会将MCU从Sleep mode唤醒。
如果使用WFE指令进入Sleep mode,MCU将在有事件发生时立即退出Sleep mode。唤醒事件可通过以下方式产生:

  • 在外设的控制寄存器使能一个中断,但不再NVIC中使能,同时使能SCR中的SEVONPEND位。当MCU从WEF恢复时,需要清除相应外设的中断挂起位和外设NVIC中断通道挂起位(在NVIC中断清除挂起寄存器中)。
  • 配置一个外部或内部EXTI线为事件模式。当MCU从WFE恢复时,因为对应时间线的挂起位没有被置位,不必清除相应外设的中断挂起位或NVCI中断通道挂起位。

由于没有在进入/退出中断时浪费时间,此模式下的唤醒时间最短。
进入和退出立即进入Sleep mode

立即进入说明
进入模式WFI(等待中断)或WFE(等待事件),且:
-SLEEPDEEP = 0及
-SLEEPONEXIT = 0
退出模式如果使用WFI退出:中断唤醒
如果使用WFE退出:事件唤醒
唤醒延迟

进入和退出退出时进入Sleep mode

退出时说明
进入模式WFI(等待中断),且:
-SLEEPDEEP = 0 及
-SLEEPONEXIT= 1
退出模式中断唤醒
唤醒延迟

代码实现

void PWR_EnterSleepMode()
{  
	__WFI();
}

注:默认情况下,__WFI()指令就会让MCU进入Sleep mode。
关于SCR的SLEEPONEXIT位和SLEEPDEEP位,根据源码中的注释

在这里插入图片描述

SLEEPONEXIT:在退出时进入Sleep mode,所以SLEEPONEXIT位置零
SLEEPDEEP:低功耗进入Deep Sleep的请求,所以SLEEPDEEP位置零

在PWR_EnterSTOPMode()函数中,SLEEPDEEP位置1,再调用__WFI()实际上进入的是Deep-sleep mode
在这里插入图片描述

    void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)
    {
      uint32_t 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, MRLVDS and LPLVDS bits according to PWR_Regulator value */
      tmpreg |= PWR_Regulator;
      
      /* Store the new value */
      PWR->CR = tmpreg;
      
      /* Set SLEEPDEEP bit of Cortex System Control Register */
      SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
      
      /* Select STOP mode entry --------------------------------------------------*/
      if(PWR_STOPEntry == PWR_STOPEntry_WFI)
      {   
        /* Request Wait For Interrupt */
        __WFI();
      }
      else
      {
        /* Request Wait For Event */
        __WFE();
      }
      /* Reset SLEEPDEEP bit of Cortex System Control Register */
      SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);  
    }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用STM32F4和ILI9341 LCD屏幕输出“张翼鹏”的示例代码: ```c #include "stm32f4xx.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_spi.h" // 定义SPI接口 #define LCD_SPI_PORT SPI5 #define LCD_SPI_RCC RCC_APB2Periph_SPI5 #define LCD_SPI_CLK 45000000 // 定义SPI引脚 #define LCD_SPI_SCK_PIN GPIO_Pin_7 #define LCD_SPI_SCK_PORT GPIOA #define LCD_SPI_MISO_PIN GPIO_Pin_11 #define LCD_SPI_MISO_PORT GPIOC #define LCD_SPI_MOSI_PIN GPIO_Pin_12 #define LCD_SPI_MOSI_PORT GPIOC // 定义LCD控制引脚 #define LCD_CS_PIN GPIO_Pin_2 #define LCD_CS_PORT GPIOB #define LCD_DC_PIN GPIO_Pin_1 #define LCD_DC_PORT GPIOB #define LCD_RST_PIN GPIO_Pin_0 #define LCD_RST_PORT GPIOB // 定义LCD屏幕分辨率 #define LCD_WIDTH 240 #define LCD_HEIGHT 320 // 初始化SPI接口 void LCD_SPI_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; // 使能SPI时钟 RCC_APB2PeriphClockCmd(LCD_SPI_RCC, ENABLE); // 配置SPI引脚 GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(LCD_SPI_SCK_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(LCD_SPI_SCK_PORT, GPIO_PinSource7, GPIO_AF_SPI5); GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(LCD_SPI_MISO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(LCD_SPI_MISO_PORT, GPIO_PinSource11, GPIO_AF_SPI5); GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(LCD_SPI_MOSI_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(LCD_SPI_MOSI_PORT, GPIO_PinSource12, GPIO_AF_SPI5); // 配置SPI SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(LCD_SPI_PORT, &SPI_InitStructure); // 使能SPI SPI_Cmd(LCD_SPI_PORT, ENABLE); } // 初始化LCD控制引脚 void LCD_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN | LCD_DC_PIN | LCD_RST_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure); } // 初始化LCD屏幕 void LCD_Init(void) { LCD_GPIO_Init(); LCD_SPI_Init(); // 复位LCD屏幕 GPIO_ResetBits(LCD_RST_PORT, LCD_RST_PIN); DelayMs(10); GPIO_SetBits(LCD_RST_PORT, LCD_RST_PIN); DelayMs(10); // 发送初始化命令 LCD_WriteCommand(0x01); // 软件复位 DelayMs(10); LCD_WriteCommand(0xCF); LCD_WriteData(0x00); LCD_WriteData(0xC1); LCD_WriteData(0x30); DelayMs(10); LCD_WriteCommand(0xED); LCD_WriteData(0x64); LCD_WriteData(0x03); LCD_WriteData(0x12); LCD_WriteData(0x81); DelayMs(10); LCD_WriteCommand(0xE8); LCD_WriteData(0x85); LCD_WriteData(0x10); LCD_WriteData(0x7A); DelayMs(10); LCD_WriteCommand(0xCB); LCD_WriteData(0x39); LCD_WriteData(0x2C); LCD_WriteData(0x00); LCD_WriteData(0x34); LCD_WriteData(0x02); DelayMs(10); LCD_WriteCommand(0xF7); LCD_WriteData(0x20); DelayMs(10); LCD_WriteCommand(0xEA); LCD_WriteData(0x00); LCD_WriteData(0x00); DelayMs(10); LCD_WriteCommand(0xC0); // 电源控制1 LCD_WriteData(0x23); DelayMs(10); LCD_WriteCommand(0xC1); // 电源控制2 LCD_WriteData(0x10); DelayMs(10); LCD_WriteCommand(0xC5); // VCOM控制1 LCD_WriteData(0x3E); LCD_WriteData(0x28); DelayMs(10); LCD_WriteCommand(0xC7); // VCOM控制2 LCD_WriteData(0x86); DelayMs(10); LCD_WriteCommand(0x36); // MADCTL LCD_WriteData(0x68); DelayMs(10); LCD_WriteCommand(0x3A); // COLMOD LCD_WriteData(0x55); DelayMs(10); LCD_WriteCommand(0xB1); // Frame rate control,最大 70 Hz,同时保证低功耗 LCD_WriteData(0x00); LCD_WriteData(0x18); DelayMs(10); LCD_WriteCommand(0xB6); // Display function control,RGB/MCU interface select LCD_WriteData(0x08); LCD_WriteData(0x82); LCD_WriteData(0x27); DelayMs(10); LCD_WriteCommand(0xF2); // 3Gamma control,disable LCD_WriteData(0x00); DelayMs(10); LCD_WriteCommand(0x26); // Gamma curve selected LCD_WriteData(0x01); DelayMs(10); LCD_WriteCommand(0xE0); // Set Gamma,positive gamma correction LCD_WriteData(0x0F); LCD_WriteData(0x31); LCD_WriteData(0x2B); LCD_WriteData(0x0C); LCD_WriteData(0x0E); LCD_WriteData(0x08); LCD_WriteData(0x4E); LCD_WriteData(0xF1); LCD_WriteData(0x37); LCD_WriteData(0x07); LCD_WriteData(0x10); LCD_WriteData(0x03); LCD_WriteData(0x0E); LCD_WriteData(0x09); LCD_WriteData(0x00); DelayMs(10); LCD_WriteCommand(0XE1); // Set Gamma,negative gamma correction LCD_WriteData(0x00); LCD_WriteData(0x0E); LCD_WriteData(0x14); LCD_WriteData(0x03); LCD_WriteData(0x11); LCD_WriteData(0x07); LCD_WriteData(0x31); LCD_WriteData(0xC1); LCD_WriteData(0x48); LCD_WriteData(0x08); LCD_WriteData(0x0F); LCD_WriteData(0x0C); LCD_WriteData(0x31); LCD_WriteData(0x36); LCD_WriteData(0x0F); DelayMs(10); LCD_WriteCommand(0x11); // Sleep out DelayMs(120); LCD_WriteCommand(0x29); // Display on // 清屏 LCD_Clear(0xFFFF); } // 写入数据到LCD屏幕 void LCD_WriteData(uint8_t data) { GPIO_SetBits(LCD_DC_PORT, LCD_DC_PIN); // DC设置为高电平表示写入数据 GPIO_ResetBits(LCD_CS_PORT, LCD_CS_PIN); // 使能片选 while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); // 等待发送缓冲区为空 SPI_I2S_SendData(LCD_SPI_PORT, data); // 发送数据 while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_RXNE) == RESET); // 等待接收缓冲区非空 SPI_I2S_ReceiveData(LCD_SPI_PORT); // 读取接收到的数据,清除RXNE标志位 GPIO_SetBits(LCD_CS_PORT, LCD_CS_PIN); // 禁止片选 } // 写入命令到LCD屏幕 void LCD_WriteCommand(uint8_t cmd) { GPIO_ResetBits(LCD_DC_PORT, LCD_DC_PIN); // DC设置为低电平表示写入命令 GPIO_ResetBits(LCD_CS_PORT, LCD_CS_PIN); // 使能片选 while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); // 等待发送缓冲区为空 SPI_I2S_SendData(LCD_SPI_PORT, cmd); // 发送命令 while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_RXNE) == RESET); // 等待接收缓冲区非空 SPI_I2S_ReceiveData(LCD_SPI_PORT); // 读取接收到的数据,清除RXNE标志位 GPIO_SetBits(LCD_CS_PORT, LCD_CS_PIN); // 禁止片选 } // 清屏 void LCD_Clear(uint16_t color) { int i, j; LCD_SetArea(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1); for (i = 0; i < LCD_HEIGHT; i++) { for (j = 0; j < LCD_WIDTH; j++) { LCD_WriteData(color >> 8); LCD_WriteData(color & 0xFF); } } } // 设置绘制区域 void LCD_SetArea(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { LCD_WriteCommand(0x2A); LCD_WriteData(x1 >> 8); LCD_WriteData(x1 & 0xFF); LCD_WriteData(x2 >> 8); LCD_WriteData(x2 & 0xFF); LCD_WriteCommand(0x2B); LCD_WriteData(y1 >> 8); LCD_WriteData(y1 & 0xFF); LCD_WriteData(y2 >> 8); LCD_WriteData(y2 & 0xFF); LCD_WriteCommand(0x2C); } // 在指定位置绘制一个像素点 void LCD_DrawPixel(uint16_t x, uint16_t y, uint16_t color) { LCD_SetArea(x, y, x, y); LCD_WriteData(color >> 8); LCD_WriteData(color & 0xFF); } // 在指定位置绘制一个字符 void LCD_DrawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bgcolor) { int i, j; uint8_t font[16]; for (i = 0; i < 16; i++) { font[i] = Font16x16[c*16+i]; } for (i = 0; i < 16; i++) { for (j = 0; j < 8; j++) { if (font[i] & (1 << (7-j))) { LCD_DrawPixel(x+j, y+i, color); } else { LCD_DrawPixel(x+j, y+i, bgcolor); } } } } // 在指定位置绘制一个字符串 void LCD_DrawString(uint16_t x, uint16_t y, char *str, uint16_t color, uint16_t bgcolor) { while (*str) { LCD_DrawChar(x, y, *str++, color, bgcolor); x += 8; } } int main(void) { LCD_Init(); LCD_DrawString(50, 100, "张翼鹏", 0x0000, 0xFFFF); while (1) {} } ``` 在代码中,需要注意以下几点: 1. 需要定义LCD屏幕的SPI接口、引脚和分辨率; 2. 需要初始化SPI接口和LCD控制引脚; 3. 需要发送初始化命令,具体的命令可以参考LCD屏幕的数据手册; 4. 绘制字符和字符串时需要使用字库,本例中使用了一个16x16的字库,可以根据需要自行替换; 5. 绘制字符和字符串时需要注意字体的大小和颜色。 6. 在main函数中,我们先调用LCD_Init()函数初始化LCD屏幕,然后调用LCD_DrawString()函数在屏幕上绘制字符串“张翼鹏”。 需要注意的是,上述代码中的一些函数调用和数据类型可能与你的程序略有不同,需要根据具体情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值