IT评测·应用市场-qidao123.com技术社区

标题: GPIO(通用输入输出端口)详细介绍 [打印本页]

作者: 罪恶克星    时间: 3 天前
标题: GPIO(通用输入输出端口)详细介绍
一、基本概念

GPIO(General - Purpose Input/Output)即通用输入输出端口,是微控制器(如 STM32 系列)中非常重要的一个外设。它是一种软件可编程的引脚,用户能够通过编程来控制这些引脚的输入或输出状态。在嵌入式体系里,GPIO 引脚犹如微控制器与外部天下沟通的桥梁,可连接各种外部设备,像按键、LED 灯、传感器等。
二、GPIO 引脚特点

(一)可编程性

可以借助软件设置每个 GPIO 引脚的工作模式,涵盖输入模式、输出模式、复勤奋能模式和模仿模式等。
(二)多用途

既能够作为简单的数字信号输入输出接口,也能通过复勤奋能告竣更复杂的通信协议,例如 I2C、SPI、UART 等。
(三)电平驱动本领

不同的 GPIO 引脚具备不同的电平驱动本领,能够提供肯定的电流以驱动外部设备。
(四)硬件相关补充

三、GPIO 工作模式

(一)输入模式

(二)输出模式

四、GPIO 在 STM32 中的使用步骤及示例代码

(一)使用步骤

(二)示例代码

1. 简单示例代码

  1. #include "stm32f4xx.h"
  2. int main(void)
  3. {
  4.     // 使能 GPIOD 时钟
  5.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  6.     GPIO_InitTypeDef GPIO_InitStructure;
  7.     // 配置 PD12 为推挽输出模式
  8.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  9.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  10.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  11.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  12.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  13.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  14.     while (1)
  15.     {
  16.         // 点亮 LED
  17.         GPIO_SetBits(GPIOD, GPIO_Pin_12);
  18.         for (int i = 0; i < 1000000; i++); // 简单延时
  19.         // 熄灭 LED
  20.         GPIO_ResetBits(GPIOD, GPIO_Pin_12);
  21.         for (int i = 0; i < 1000000; i++); // 简单延时
  22.     }
  23. }
复制代码
2. 优化延时函数示例代码

  1. #include "stm32f4xx.h"
  2. void delay_ms(uint32_t ms)
  3. {
  4.     TIM_TimeBaseInitTypeDef TIM_InitStructure;
  5.     // 使能 TIM2 时钟
  6.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  7.     // 配置定时器
  8.     TIM_InitStructure.TIM_Period = ms - 1;
  9.     TIM_InitStructure.TIM_Prescaler = SystemCoreClock / 1000 - 1;
  10.     TIM_InitStructure.TIM_ClockDivision = 0;
  11.     TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  12.     TIM_TimeBaseInit(TIM2, &TIM_InitStructure);
  13.     // 使能定时器
  14.     TIM_Cmd(TIM2, ENABLE);
  15.     // 等待定时器计数完成
  16.     while (!TIM_GetFlagStatus(TIM2, TIM_FLAG_Update));
  17.     TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  18.     // 关闭定时器
  19.     TIM_Cmd(TIM2, DISABLE);
  20. }
  21. int main(void)
  22. {
  23.     // 使能 GPIOD 时钟
  24.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  25.     GPIO_InitTypeDef GPIO_InitStructure;
  26.     // 配置 PD12 为推挽输出模式
  27.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  28.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  29.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  30.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  31.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  32.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  33.     while (1)
  34.     {
  35.         // 点亮 LED
  36.         GPIO_SetBits(GPIOD, GPIO_Pin_12);
  37.         delay_ms(1000); // 延时 1 秒
  38.         // 熄灭 LED
  39.         GPIO_ResetBits(GPIOD, GPIO_Pin_12);
  40.         delay_ms(1000); // 延时 1 秒
  41.     }
  42. }
复制代码
3. 代码模块化示例代码

  1. #include "stm32f4xx.h"
  2. void GPIO_LED_Init(void)
  3. {
  4.     // 使能 GPIOD 时钟
  5.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  6.     GPIO_InitTypeDef GPIO_InitStructure;
  7.     // 配置 PD12 为推挽输出模式
  8.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  9.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  10.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  11.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  12.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  13.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  14. }
  15. void LED_On(void)
  16. {
  17.     GPIO_SetBits(GPIOD, GPIO_Pin_12);
  18. }
  19. void LED_Off(void)
  20. {
  21.     GPIO_ResetBits(GPIOD, GPIO_Pin_12);
  22. }
  23. int main(void)
  24. {
  25.     GPIO_LED_Init();
  26.     while (1)
  27.     {
  28.         LED_On();
  29.         for (int i = 0; i < 1000000; i++); // 简单延时
  30.         LED_Off();
  31.         for (int i = 0; i < 1000000; i++); // 简单延时
  32.     }
  33. }
复制代码
五、GPIO 应用场景


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4