打印
[其他ST产品]

串口接收字节不正确,串口初始化程序如下

[复制链接]
90|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
各位大侠,我的串口测试程序,在PC机端使用串口调试助手(输入HEX显示HEX)向STM32发送一串字符,当接收到0x0d,0x0a则判断一帧数据接收完毕,然后关接收中断使用查询方式将接收到的所有字符原样回送到串口调试助手显示。
问题是我PC机发出一串字符,测试板也能接收和回送,但回送帧中总有几个不一致的字节,具体错误情况看下面贴图。
经查找最终发现规律,不一致的都是最高BIT位为1的字符,当发送字节的最高位为1时,接收过来变成0,其他7个bit都正确,从而导致发送的与接收到的不一样。
这种情况还真不知道是怎么回事,不知道有没有描述清楚,急盼各位帮助啊。

贴上我的串口初始化程序
void Usart1_Init(void)
{
USART_InitTypeDef USART_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  //使能串口1时钟
        
USART_InitStructure.USART_BaudRate = 9600;      //速率9600bps
USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //数据位8位
USART_InitStructure.USART_StopBits = USART_StopBits_1;   //停止位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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);//配置串口参数函数

/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                  
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);      

/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);  
}
void GPIO_Configuration(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;           
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;      
   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);
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

262

主题

1394

帖子

2

粉丝