这段是IO配置
/* Configure USART3 Tx (PB.10) as alternate function push-pull */
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);
/* Configure USART3 Rx (PB.11) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
下面一段是USART3端口初始化
void USART_Configuration(u16 port, u32 bandrate)
{
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = bandrate;
if(port == 1)
{
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}
else if(port == 2)
{
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
else if(port == 3)
{
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
}
else if(port == 4)
{
USART_Init(UART4, &USART_InitStructure);
USART_Cmd(UART4, ENABLE);
USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
}
else if(port == 5)
{
USART_Init(UART5, &USART_InitStructure);
USART_Cmd(UART5, ENABLE);
USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
}
}//void USART_Configuration(u16 port, u32 bandrate)
会有这句调用:USART_Configuration(3, 9600);
下面是中断处理函数
void USART3_IRQHandler(void)
{
if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE))
{
Uart3_ucReceLen = Uart3_ucReceLen & (UART_BUFF_LEN - 1);
Uart3_ucReceBuff[Uart3_ucReceLen++] = USART_ReceiveData(USART3);
Uart3_udTimeOut = udTickCount + 10;
}
else if(USART_GetFlagStatus(USART3, USART_FLAG_TC))
{
if(Uart3_ucSendPosi < Uart3_ucSendLen) USART_SendData(USART3,Uart3_ucSendBuff[Uart3_ucSendPosi++]);
else {USART_ITConfig(USART3,USART_IT_TC,DISABLE);
Uart3_udTimeOut = udTickCount + 10; }
}
}
采用MODBUS协议,能顺利接收到数据,通过检测知道,也能进发送中断,并将数据发送出去,但TXD3口没有电平变化,以为是芯片的这个口坏了,换了一个也是这样,用的芯片是STM32F105VCT6
求牛人指导 |