STM32F103VET6,引脚PC12、PD2,无法控制高低电平。
发现问题时初始化为串口5,一直不发送数据。
串口5初始化代码:
void uart5_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOD, &GPIO_InitStructure);
//NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(UART5, &USART_InitStructure); //初始化串口5
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(UART5, ENABLE); //使能串口5
}
尝试初始化为普通IO。
结果:PD2一直是高电平(3.3V),PC12一直是0.2V左右
初始化代码
void test(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC, GPIO_Pin_12);
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
}
以上尝试是在空白工程里面,想请教一下问题出在哪里
【以下回答由 GPT 生成】
首先,检查代码中的引脚定义和初始化是否正确。确保PC12和PD2的引脚初始化代码正确且没有被其他代码修改。检查代码中是否有其他地方对这些引脚进行了配置。
如果确认引脚的初始化代码正确,而且PC12和PD2引脚仍然无法控制高低电平,可能存在以下问题:
GPIO_InitStructure.GPIO_Mode
中使用GPIO_Mode_Out_PP
或者GPIO_Mode_AF_PP
来配置引脚为普通推挽输出。```c GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 设置为普通推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 设置为普通推挽输出 GPIO_Init(GPIOD, &GPIO_InitStructure); ```
GPIO_PuPd_UP
或者GPIO_PuPd_DOWN
来配置上拉或下拉电阻。```c GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; // 设置为输入模式 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉电阻 GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; // 设置为输入模式 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉电阻 GPIO_Init(GPIOD, &GPIO_InitStructure); ```
如果以上方法都不能解决问题,可能存在硬件故障或者其他问题导致PC12和PD2无法正常输出控制高低电平。检查硬件连接、供电等问题,可以尝试使用示波器或者逻辑分析仪来检查信号的变化情况,或者使用其他引脚来测试是否存在相同的问题。
请注意,以上提供的解决方案是基于通过代码配置引脚的方法来解决问题。