这段代码的作用是初始化 GPIOC 和 GPIOB 两个端口,将其中的多个引脚配置为推挽输出模式,并将这些引脚全部置为高电平。

具体来说,代码中使用 GPIO_InitTypeDef 结构体来配置 GPIO 端口的参数,然后通过 RCC_APB2PeriphClockCmd 函数使能 GPIOC 和 GPIOB 的时钟以及 AFIO 时钟。接下来,通过 GPIO_PinRemapConfig 函数开启 SWD,再通过 GPIO_InitStructure 结构体配置 GPIOC 和 GPIOB 的引脚模式和速度参数,最后使用 GPIO_Init 函数进行初始化,并使用 GPIO_SetBits 函数将所有需要输出高电平的引脚置为高电平。最后,代码使用 delay_ms 函数延时 50 毫秒。

初始化语句可以是:

GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|
RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); 
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_9|
GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); 
GPIO_SetBits(GPIOC,GPIO_Pin_10|GPIO_Pin_9|GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; 
GPIO_Init(GPIOB, &GPIO_InitStructure); 
GPIO_SetBits(GPIOB,GPIO_Pin_All); 
delay_ms(50);

代码解释:

  • GPIO_InitTypeDef GPIO_InitStructure;:声明一个 GPIO_InitTypeDef 结构体变量,用于存储 GPIO 端口的配置参数。
  • RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);:使能 GPIOC 和 GPIOB 以及 AFIO 的时钟。
  • GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);:开启 SWD,禁用 JTAG 调试接口。
  • GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_9|GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6;:配置 GPIOC 的引脚 6~10 为输出模式。
  • GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;:配置 GPIOC 的引脚为推挽输出模式。
  • GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;:配置 GPIOC 的引脚速度为 50MHz。
  • GPIO_Init(GPIOC, &GPIO_InitStructure);:初始化 GPIOC 端口。
  • GPIO_SetBits(GPIOC,GPIO_Pin_10|GPIO_Pin_9|GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6);:将 GPIOC 的引脚 6~10 设置为高电平。
  • GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;:配置 GPIOB 的所有引脚为输出模式。
  • GPIO_Init(GPIOB, &GPIO_InitStructure);:初始化 GPIOB 端口。
  • GPIO_SetBits(GPIOB,GPIO_Pin_All);:将 GPIOB 的所有引脚设置为高电平。
  • delay_ms(50);:延时 50 毫秒。

注意:

  • 具体的引脚配置需要根据实际电路设计进行调整。
  • 使用 GPIO_Pin_All 时,需要确保所有引脚都连接到实际电路中,否则可能会造成损坏。
  • 可以根据需要调整引脚模式、速度等参数。

相关资源:

  • STM32 中文参考手册
  • STM32 库函数参考手册
  • STM32 官方网站
STM32 GPIOC 和 GPIOB 端口初始化配置详解及代码示例

原文地址: https://www.cveoy.top/t/topic/m9Vb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录