snyanglq 发表于 2015-2-4 11:54:53

关于STM32F103RCT6串口重定向数据发送和接收

本帖最后由 snyanglq 于 2015-2-4 11:56 编辑

不知道各位有没有出现过这样的情况,我使用STM32F103RCT6芯片编写串口程序,重定向printf函数到串口1,数据可以正常地发送和接收,但是有个问题
我仿真的时候发现,我每次用printf发出去的数据,接收数组同样会接收到,举个例子,我用串口发送123到上位机,上位机发送456回到MCU,我在中断的
接收函数里的数组 W_Rec[]会接收到123456,本来应该是456才对,我想问下是不是重定向后都会这样,如果不是要如何解决?请大家指点下小弟

附一下代码:
串口配置
void USART1_Init(void)
{
      GPIO_InitTypeDef GPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;
      
      /* config USART1 clock */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
      
      /* USART1 GPIO config */
      /* Configure USART1 Tx (PA.09) as alternate function push-pull */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);   
      /* Configure USART1 Rx (PA.10) as input floating */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
      
      /* USART1 mode config */
      USART_InitStructure.USART_BaudRate = 115200;
      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(USART1, &USART_InitStructure);
      USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
      
      USART_Cmd(USART1, ENABLE);
}void USART1_NVIC(void)
{
      NVIC_InitTypeDef NVIC_InitStructure;
      //分配到第0组
      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
      
      /* Enable the USARTy Interrupt */
      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);
}重定向函数
int fputc(int ch, FILE *f)
{
      /* 将Printf内容发往串口 */
      USART_SendData(USART1, (unsigned char) ch);
      while (!(USART1->SR & USART_FLAG_TXE));
      
      return (ch);
}中断函数
void USART1_IRQHandler(void)
{
      uint8_t c;
      if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
      {         
                   USART_ClearITPendingBit(USART1, USART_IT_RXNE);
         // W_Rec = USART1->DR;
                        c = USART_ReceiveData(USART1);
                        W_Rec = c;

      }
         
}请大家指点下如何才能不接收MCU发出去的数据

沐紫 发表于 2015-2-5 09:57:28

顶,另外建议提问发布悬赏贴,赠送ST金币,大家会更热心来帮你的。我们也会补发金币给你

snyanglq 发表于 2015-2-5 17:15:15

原来是我的程序关回显部分出了问题,不好意思,麻烦大家了
页: [1]
查看完整版本: 关于STM32F103RCT6串口重定向数据发送和接收