硬汉嵌入式论坛

 找回密码
 立即注册
查看: 5097|回复: 1
收起左侧

[有问必答] STM32F407 USART1发送字符串乱码

[复制链接]

4

主题

19

回帖

31

积分

新手上路

积分
31
发表于 2016-4-28 09:49:12 | 显示全部楼层 |阅读模式
int main(void)   //主函数
{
uint8_t *str;
       while(1)     
       {
              str = "Welcome to STM321111";
             TData(str, 20);
       }
}


void TData(uint8_t *_usBuf, uint8_t _usLen)   //数据发送函数
{  
    uint8_t  Tx_Status, Txread, Txwrite, Txcounter;
    uint16_t  i;        
   
    Txread = p_uart.Tx_read;
    Txcounter = p_uart.Tx_counter;   
   
    //将待发送数据缓冲区写入UART发送数据缓冲区
    for(i=0; i<_usLen; i++)
    {
        if(++Txread >= USART1_TX_BUF_SIZE)
        {
            Txread = 0;
        }   
        
        if(++Txcounter >= USART1_TX_BUF_SIZE)  //判断发送缓冲区数据个数
        {
            Txcounter = 0;
        }
        
        p_uart.pTxBuf[p_uart.Tx_write] = _usBuf;     
        if(++p_uart.Tx_write >= USART1_TX_BUF_SIZE)     
        {
            p_uart.Tx_write = 0;
        }
        p_uart.Tx_counter++;               
    }
    USART_ITConfig(USART1, USART_IT_TXE, ENABLE);   
}

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)        //中断函数
{
        USART_ClearITPendingBit(USART1, USART_IT_TXE);
        if(p_uart.Tx_read == p_uart.Tx_write)            //没有新数据
        {
            USART_ITConfig(USART1, USART_IT_TXE, DISABLE);              
            USART_ITConfig(USART1, USART_IT_TC, ENABLE);   
        }
        else
        {
            USART_SendData(USART1, p_uart.pTxBuf[p_uart.Tx_read]);
            while(USART_GetFlagStatus(USART1, USART_FLAG_TXE == RESET));            
            if(++p_uart.Tx_read >= USART1_TX_BUF_SIZE)
            {
              p_uart.Tx_read = 0;
            }
            p_uart.Tx_counter--;
        }
}
   
else if(USART_GetITStatus(USART1, USART_IT_TC) !=RESET)
{
      USART_ClearITPendingBit(USART1, USART_IT_TC);    //清除发送完成标志位
      if(p_uart.Tx_read == p_uart.Tx_write)            //没有新数据
      {
        USART_ITConfig(USART1, USART_IT_TC, DISABLE);   //关闭发送完成中断
      }
      else
        {
            USART_SendData(USART1, p_uart.pTxBuf[p_uart.Tx_read]);
            while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);            
            if(++p_uart.Tx_read >= USART1_TX_BUF_SIZE)
            {
                p_uart.Tx_read = 0;
            }
            p_uart.Tx_counter--;
        }
}
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
117546
QQ
发表于 2016-4-28 11:56:17 | 显示全部楼层
乱码一般是波特率问题,是不是板子的波特率跟电脑端的波特率一致,另外重要的一点是,你是在往电脑端发送数据吧,看看串口线是否好用。
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-8-14 06:34 , Processed in 0.036710 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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