/* * GPIO.c * * Created on: 2025年7月9日 * Author: logan */ #include "debug.h" #define AIN1_PIN GPIO_Pin_4 //右轮 #define AIN2_PIN GPIO_Pin_5 // #define BIN1_PIN GPIO_Pin_6 //左轮 #define BIN2_PIN GPIO_Pin_7 // #define PWMA_PIN GPIO_Pin_8 // TIM1_CH1 (PA8) #define PWMB_PIN GPIO_Pin_9 // TIM1_CH2 (PA9) void GPIO_INIT(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); GPIO_InitTypeDef GPIO_InitStructure={0}; //机械臂-------------------------------------------------------------------------------TIM3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //openmv-----------------------------------------------------------------------------USART3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); //车轮---------------------------------------------------------------------------------TIM1 GPIO_InitStructure.GPIO_Pin = AIN1_PIN | AIN2_PIN | BIN1_PIN | BIN2_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化PWM引脚为复用功能 GPIO_InitStructure.GPIO_Pin = PWMA_PIN | PWMB_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 改为复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //超声波--------------------------------------------------------------------------------TIM2 // PA0作为输入(ECHO) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // 下拉输入 GPIO_Init(GPIOA, &GPIO_InitStructure); // PA1作为输出(TRIG) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //水泵---------------------------------------------------------------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 改为复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //OLED-------------------------------------------------------------------------------- //SCL--PA8 //SDA--PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //ESP8266-----------------------------------------------------------------------------USART1 // TX--PA2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // RX--PA3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } 上述引脚均被使用,更换水位传感器的引脚
时间: 2025-07-09 09:14:45 浏览: 16
在CH32V307VCT6开发板上,GPIO引脚的使用情况取决于具体的硬件设计和外设配置。由于该芯片具有丰富的引脚资源,并且支持多种复用功能,因此需要根据当前项目中已使用的引脚情况进行评估,以确定哪些引脚仍然可用[^1]。
### 未被占用的GPIO引脚选择建议
通常,在默认情况下,以下类型的引脚可能未被使用,适合连接水位传感器:
- **未用于调试接口(SWD)的引脚**:例如`PA13`、`PA14`等,这些引脚在某些开发板上可能被保留为下载/调试接口,但在实际应用中可以作为普通GPIO使用。
- **未用于系统控制或外设的通用I/O引脚**:例如`PB0`、`PB1`、`PB5`、`PB8`、`PB9`、`PC13`、`PC14`、`PC15`等,在未被分配给其他外设时可作为ADC输入或数字输入使用。
- **ADC专用输入通道对应的引脚**:例如`PA0`、`PA1`、`PA2`、`PA3`等,如果这些通道未被其他模拟信号源占用,则可用于水位传感器的数据采集[^1]。
### GPIO配置示例(以PA0为例)
```c
#include "ch32v30x.h"
void GPIO_Init_ADC(void) {
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 配置PA0为模拟输入模式
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; // 模拟输入
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
```
上述代码展示了如何将`PA0`配置为模拟输入模式,以便连接水位传感器并进行ADC采样。如果该引脚未被其他功能占用,则可以安全地用于传感器接入。
### 注意事项
- 在选择具体引脚之前,应查阅CH32V307VCT6数据手册和开发板原理图,确认所选引脚是否已被用于其他功能(如SPI、I²C、USART、定时器PWM输出等)。
- 若需使用中断检测水位变化(例如数字式水位传感器),则应选择支持外部中断的引脚(如`PA0`、`PA1`、`PA2`、`PA3`等)。
- 对于模拟水位传感器,应优先选择与ADC模块绑定的引脚,以简化信号采集流程。
---
阅读全文