原创 stm32 转载

2009-6-22 21:46 1603 1 1 分类: MCU/ 嵌入式
 
2009-04-19 19:18:35 | 关于stm32 usb虚拟串口 程序问题的讨论
 









 


 转自  www.stmsky.com  在路上 stm32 论坛


 


        http://www.stmsky.com/bbs/viewthread.php?tid=1168&extra=page%3D1


主要问题是  串口有时收到乱码 有时收到丢包数据:
  关于 乱码 我们其实很容易想到 奇偶校验的问题,而我们平常都是默认为没有奇偶校验。
    请看程序:
         * USART1 default configuration */
  /* USART1 configured as follow:
        - BaudRate = 9600 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - Parity Odd
        - Hardware flow control desabled
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge
        - USART LastBit: The clock pulse of the last data bit is not output to
                         the SCLK pin
  */
  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_Odd;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_InitStructure.USART_Clock = USART_Clock_Disable;
  USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

只要改成odd  或者程序里作修改即可


问题2 :既然是虚拟串口,那么利用pc串口软件 两边应该可以正常通信,但是源程序出现的情况是。
         真--虚  ok
             虚--真  则严重丢数据

  看了一下程序  原来捣鬼的是这里:
      void USB_To_USART_Send_Data(u8* data_buffer, u8 Nb_bytes)
{
  u32 i;

  for (i = 0; i < Nb_bytes; i++)
  {
    USART_SendData(USART1, *(data_buffer + i));
       }
}
串口发送数据后 没有等待串口发送完成

改为下边即可
   void USB_To_USART_Send_Data(u8* data_buffer, u8 Nb_bytes)
{
  u32 i;

  for (i = 0; i < Nb_bytes; i++)
  {
    USART_SendData(USART1, *(data_buffer + i));
      while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
   
  }
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
1
关闭 站长推荐上一条 /2 下一条