野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11226|回复: 2

STM32串口发送多第一个字节

[复制链接]
发表于 2020-3-23 09:38:05 | 显示全部楼层 |阅读模式
STM32F051调试串口2作为485通讯端口时,发现每次发送都多第一个字节,具体代码:
void UART2_Init()
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
   
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
        RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);

                        
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);         
        /*
        *    USART2_TX -> PA2 , USART2_RX ->    PA3
        */                                            
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;                        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd =  GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;                              
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;          //推挽输出
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        GPIO_ResetBits(GPIOA,GPIO_Pin_1);

        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(USART2, &USART_InitStructure);  

        NVIC_SetPriority(USART2_IRQn, 0); /* (4) */
        NVIC_EnableIRQ(USART2_IRQn); /* (5) */

        USART2->CR1 |= ((1<<5)|(1<<4));//打开接收中断和IDLE中断
        USART_Cmd(USART2, ENABLE);

}

void USART2_Send_byte(uint8_t val)
{   
    USART_SendData(USART2, val);   
    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);    //等待发送完成   
}



void UART2_s_process(volatile uint8_t *buffer, uint8_t len)
{
    uint8_t i;
    GPIO_SetBits(GPIOA,GPIO_Pin_1);
    delay_ms(2);
    for (i = 0;i < len;i ++)
        USART2_Send_byte(buffer);
    GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
调试界面:
第一个字节在波特率9600时是08,波特率115200时是84,大婶们帮忙看下代码部分哪不正常?


回复

使用道具 举报

 楼主| 发表于 2020-3-23 17:17:02 | 显示全部楼层
什么情况,没人搭理!!!
回复 支持 反对

使用道具 举报

发表于 2020-3-24 20:09:38 | 显示全部楼层
多什么字节
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-24 01:55 , Processed in 0.037826 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表