使用芯片
STM32F101 ,usart3 RS485半双工,同样的硬件板子,串口会收到发送出的数据帧,即数据为 发送帧+对方回复帧,有时又收到正确数据,即对方回复帧。一直想不通,请
论坛里的大佬们解答。
以下为一、初始化代码:Uart_Ini
tial(USART3,4800,0);
- void Uart_Initial(USART_TypeDef* USARTx,u32 BRate,u16 WorkMode)
- {
- u16 WordLength;
- u16 StopBits;
- u16 Parity;
- u16 FlowControl;
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- USART_ClockInitTypeDef USART_ClockInitStructure;
- if(USARTx==USART3)
- {
- /* 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_IN_FLOATING;
- GPIO_Init(GPIOB, GPIO_InitStructure);
- }
- //0x1010
- WordLength = WorkMode 0xf000;
- StopBits = WorkMode 0x0f00;
- StopBits <<= 4;
- Parity = WorkMode 0x00f0;
- Parity <<= 4;
- FlowControl = WorkMode 0x000f;
- FlowControl <<= 8;
- USART_InitStructure.USART_BaudRate = BRate;
- USART_InitStructure.USART_WordLength = WordLength;
- USART_InitStructure.USART_StopBits = StopBits;
- if(Parity==0)
- { USART_InitStructure.USART_Parity = USART_Parity_No;
- }
- else
- {
- USART_InitStructure.USART_Parity = USART_Parity_Even;//Parity;
- }
- USART_InitStructure.USART_HardwareFlowControl = FlowControl;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
- USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
- USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
- USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
- /* Configure the USARTx */
- USART_Init(USARTx, USART_InitStructure);
- USART_ClockInit(USARTx, USART_ClockInitStructure);
- /* Enable USARTx RX INT*/
- USART_ITConfig(USARTx,USART_IT_RXNE,ENABLE);
- //USART_ITConfig(USARTx,USART_IT_TXE,ENABLE);
- FeedDog();
- /* Enable USARTx */
- USART_Cmd(USARTx, ENABLE);
- }
二、中断优先级设置
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- /* Enable the USART1 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel =USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init( NVIC_InitStructure);
- /* Configure the NVIC Preemption Priority Bits */
- //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- /* Enable the USART2 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init( NVIC_InitStructure);
- /* Enable the USART3 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init( NVIC_InitStructure);
- /* Enable the USART3 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init( NVIC_InitStructure);
- /* Enable the TIM2 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init( NVIC_InitStructure);
- /* Enable the TIM3 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init( NVIC_InitStructure);
- /* Enable the TIM4 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init( NVIC_InitStructure);
- }
三、中断处理
- void USART3_IRQHandler(void)
- {
- static u8 TxCounter3;
- u8 TempChar;
- uint32_t tempint;
- static u8 aaai;
- //RUN_LED_1
- if (USART_GetFlagStatus(USART3, USART_FLAG_PE) != RESET)
- {
- USART_ReceiveData(USART3);
- USART_ClearFlag(USART3, USART_FLAG_PE);
- }
- if (USART_GetFlagStatus(USART3, USART_FLAG_ORE) != RESET)
- {
- USART_ReceiveData(USART3);
- USART_ClearFlag(USART3, USART_FLAG_ORE);
- }
- if (USART_GetFlagStatus(USART3, USART_FLAG_FE) != RESET)
- {
- USART_ReceiveData(USART3);
- USART_ClearFlag(USART3, USART_FLAG_FE);
- }
- if (USART_GetFlagStatus(USART3, USART_FLAG_LBD) != RESET)
- {
- USART_ClearFlag(USART3, USART_FLAG_LBD);
- }
- if(USART_GetFlagStatus(USART3,USART_IT_RXNE)==SET)
- {
- USART_ClearFlag(USART3, USART_FLAG_RXNE);//USART3->SR = ~USART_FLAG_RXNE;
- USART_ClearITPendingBit(USART3, USART_IT_RXNE);
- tempint = USART_ReceiveData(USART3);
- TempChar = (u8)(tempint 0x00ff);
- *usart_user_3.p_buff = TempChar;
- usart_user_3.p_buff++;
- usart_user_3.rcv_num++;
- if(usart_user_3.rcv_num >= (BUF_SPACE-1))
- {
- usart_user_3.p_buff = usart_user_3.buff;
- usart_user_3.rcv_num = 0;
- }
- usart_user_3.rcv_overtime_add = 0;
- }
- if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
- {
- /* Write one byte to the transmit data register */
- USART_SendData(USART3, usart_user_3.send_buff[TxCounter3++]);
- if(TxCounter3 == usart_user_3.send_num )
- { USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
- TxCounter3 = 0;
- }
- }
- }
四、数据处理
中断10ms定时器程序中,定时到80ms,延迟接收结束,处理数据。处理数据不做展开。
问题1.我
仿真时就看80ms后的收数据buffer现在200字节,足够大,但是会看到数据前部分就是我发送的数据后面就是接收到的数据。程序得到数据:
发:C2 03 08 33 00 0E 27 52
收:C2 03 08 33 00 0E 27 52 C2 03 1C 00 0C 00 00 00 20 00 00 00 5A 00 8F 00 E8 00 01 00 00 01 61 00 00 00 65 00 52 00 7E 1A A2
正确帧数据应该是
发:C2 03 08 33 00 0E 27 52
收:C2 03 1C 00 0C 00 00 00 20 00 00 00 5A 00 8F 00 E8 00 01 00 00 01 61 00 00 00 65 00 52 00 7E 1A A2
曾以为是硬件问题,但用串口工具收数据,就是看到正确帧数据。说明硬件链路上数据确实正确。
2.是另外一个问题,串口没有开通LIN功能,但是USART_FLAG_ORE会置位,不明白。。。我只能通过清除位来使得串口正常工作,想知道USART_FLAG_ORE置位的原因。