代码如下,更换管脚为PA9/PA10,数据收发OK;但是复用为PC4/PC5管脚后只能发送,不能接收;求解
/************************************************************************/
void USART1_InterruptConfig()
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 设置NVIC参数 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RTO, ENABLE);//使能或者失能接收超时的串口中断
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SetReceiverTimeOut(USART1, 3); //设定超时时间
USART_ReceiverTimeOutCmd(USART1, ENABLE); //使能超时机制
}
/************************************************************************/
void STM_EVAL_COM1_Init(USART_InitTypeDef* USART_InitStruct_)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_7);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_7);
//TX-RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
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(GPIOC, GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOC, GPIO_InitStructure);
USART_Init(USART1, USART_InitStruct_);
USART1_InterruptConfig();
USART_Cmd(USART1, ENABLE);
}
/*************************************************************************/
void USART_COM1_Init()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_2;
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_DeInit(USART1);
STM_EVAL_COM1_Init( USART_InitStructure);
}
使用电脑的串口调试助手,配置的也是两个停止位.上述代码修改复用端口为PA9,PA10数据收发OK,使用了
STM32CubeMax验证后结果相同,PA9PA10收发OK,PC4/PC5只能发不能收。纠结,,感觉PC5管脚配置问题(官方的或需要开启他的配置),但是关于管脚复用的寄存器应该就那三个GPIOx_AFRL、GPIOx_AFRH、GPIOx_MODER.昨天直接填的寄存器试了下也不行.