这几天调试modbus,使用STM32F103VBT6的串口2,在配置完成后查询方式和中断方式都不能发送数据。不知道是配置还是哪里出错,请帮忙看下,谢谢! /** * @brief 串口初始化 * @param ucPORT 串口号 * ulBaudRate 波特率 * ucDataBits 数据位 * eParity 校验位 * @retval None */ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; (void)ucPORT; //不修改串口 (void)ucDataBits; //不修改数据位长度 (void)eParity; //不修改校验格式 //使能USART2,GPIOA RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); // RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//复位串口2 // RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位 USART_InitStructure.USART_BaudRate = ulBaudRate; //只修改波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; 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(USART2, &USART_InitStructure); USART_ITConfig(UART2, USART_IT_RXNE, ENABLE); /* 使能接收中断*/ USART_ITConfig(UART2, USART_IT_TC, ENABLE); /* 使能接收中断*/ //使能USART2 USART_Cmd(USART2, ENABLE); USART_ClearFlag(UART2, USART_FLAG_TC); //设定USART2 中断优先级 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); return TRUE; } /** * @brief 通过串口发送数据 * @param None * @retval None */ BOOL xMBPortSerialPutByte( CHAR ucByte ) { //发送数据 USART_SendData(USART2, ucByte); return TRUE; } /** * @brief 从串口获得数据 * @param None * @retval None */ BOOL xMBPortSerialGetByte( CHAR * pucByte ) { //接收数据 *pucByte = USART_ReceiveData(USART2); return TRUE; } /* Create an interrupt handler for the transmit buffer empty interrupt * (or an equivalent) for your target processor. This function should then * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that * a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character. */ static void prvvUARTTxReadyISR( void ) { //mb.c eMBInit函数中 //pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM //发送状态机 pxMBFrameCBTransmitterEmpty(); } /* Create an interrupt handler for the receive interrupt for your target * processor. This function should then call pxMBFrameCBByteReceived( ). The * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the * character. */ static void prvvUARTRxISR( void ) { //mb.c eMBInit函数中 //pxMBFrameCBByteReceived = xMBRTUReceiveFSM //接收状态机 pxMBFrameCBByteReceived(); } /** * @brief USART2中断服务函数 * @param None * @retval None */ void USART2_IRQHandler(void) { //发生接收中断 if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) { prvvUARTRxISR(); //清除中断标志位 USART_ClearITPendingBit(USART2, USART_IT_RXNE); } //发生完成中断 if(USART_GetITStatus(USART2, USART_IT_TC) == SET) { prvvUARTTxReadyISR(); //清除中断标志 USART_ClearITPendingBit(USART2, USART_IT_TC); } } |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
评分
查看全部评分
RX也要设置GPIO_Mode_AF_PP?这样你就收不到数据了,GPIO_Mode_AF_PP是复用推挽输出的
官方的例程参考一下看看能否解决: