10回答

0收藏

STM32F103串口接收数据会丢失

STMCU STMCU 16851 人阅读 | 10 人回复 | 2014-12-08

用串口调试助手发数据给STM32F103VCT6的时候,发送一串数据的时候会有中间字节数据的丢失,求大神指点!谢谢
分享到:
回复

使用道具 举报

回答|共 10 个

倒序浏览

沙发

Currant

发表于 2014-12-8 17:57:28 | 只看该作者

把接收的程序发来看看吧,掉数据一般应该是没有及时的读取接收字节造成的
板凳

jwdxu2009

发表于 2014-12-8 23:09:42 | 只看该作者

参考和学习
地板

飞翔-396652

发表于 2014-12-9 11:13:11 | 只看该作者

稍等 晚上发
5#

飞翔-396652

发表于 2014-12-9 17:47:11 | 只看该作者

Currant 发表于 2014-12-8 17:57
把接收的程序发来看看吧,掉数据一般应该是没有及时的读取接收字节造成的 ...

/******************** (C) COPYRIGHT 2010 HY嵌入式开发工作室 ********************

* Description        : 演示的是4个蓝色LED(D1-D4) 轮流闪烁
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>








#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */


PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}


GPIO_InitTypeDef GPIO_InitStructure;


void RCC_Configuration(void);
void UART_Init(void);
void Uart1Putchar(u8 ch);

u8 temp_trx;


                                                 // NVIC_SetPriority
int main(void)
{
   /* System Clocks Configuration **********************************************/
  
  RCC_Configuration();   
  

  /* Configure all unused GPIO port pins in Analog Input mode (floating input
     trigger OFF), this will reduce the power consumption and increase the device
     immunity against EMI/EMC *************************************************/
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
                         RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO, ENABLE);
    UART_Init();

                            
                                 
   printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");
   

  while (1)
  {
  
  
  
  }
}


void RCC_Configuration(void)
{   
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();
}


/*void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--);               
}           */

void UART_Init(void)
{
   USART_InitTypeDef  USART_InitStructure ;
   NVIC_InitTypeDef NVIC_InitStructure;


   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;                                      //TX                             
  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_10 ;                                    //RX  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);       




  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_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
  
  

  
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);          //串口1接收采用中断方式
    USART_Cmd(USART1, ENABLE);
      
        USART_ClearFlag(USART1,USART_FLAG_TC);        //        防止串口第一字节丢失
        USART_ClearFlag(USART1,USART_FLAG_RXNE);//
       
       
        USART_ClearITPendingBit(USART1,   USART_IT_RXNE);
        USART_ClearITPendingBit(USART1,   USART_IT_TC);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

  
}


  void Uart1Putchar(u8 ch)       
  {
     USART_SendData(USART1, ch);

          while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  ;
                           

  
  }

void  USART1_IRQHandler(void)
{

      if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
            {         
                temp_trx=USART_ReceiveData(USART1);                  //这个地方忘记修改了没有修改前为USART2
  
           Uart1Putchar(temp_trx);       
         }
  }

          



#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */



void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif



/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
6#

小菜头1

发表于 2014-12-11 21:36:33 | 只看该作者

                                学习
7#

Currant

发表于 2014-12-12 13:12:08 | 只看该作者

我想掉数据的原因应该是接收中断中调用了发送函数,导致下一个数据没能及时处理,被再下一个数据给覆盖了。
解决办法:在接收中断里最好不要写发送,一定要写的话在中断中置位一个标志量,在主程序中调用发送函数
8#

飞翔-396652

发表于 2015-1-28 10:01:12 | 只看该作者

Currant 发表于 2014-12-12 13:12
我想掉数据的原因应该是接收中断中调用了发送函数,导致下一个数据没能及时处理,被再下一个数据给覆盖了。 ...

中断里面要发的
9#

youyexinyu

发表于 2015-7-14 14:12:38 | 只看该作者

飞翔-396652 发表于 2014-12-9 17:47
/******************** (C) COPYRIGHT 2010 HY嵌入式开发工作室 ********************

* Description    ...

楼主,你的问题解决了吗?
我学习的是stm32f303,出现的问题跟你一样。如果中断写成这样的话就没有问题。
void  USART1_IRQHandler(void)
{
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
     {         
           USART_SendData(USART1, USART_ReceiveData(USART1));
     }
  }
但是,只要调用自己的发送函数就会出现丢数据的情况。
10#

飞翔-396652

发表于 2015-7-28 17:42:53 | 只看该作者

没有 没有 没有 没有 没有 没有
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条