打印

STM32串口发送多第一个字节

[复制链接]
293|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
车水马龙|  楼主 | 2020-4-3 17:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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-4-3 19:38 | 只看该作者
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);

==> while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); 验证下

使用特权

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

本版积分规则

474

主题

476

帖子

0

粉丝