zoukankan      html  css  js  c++  java
  • stm32f103c8串口USART1发送多一字节

    用UART写了一段Bootloader代码,遇到了一个很奇怪的现象。

    代码如下:简单介绍一下就是先统一配置MCU的IO端口,然后配置串口参数,然后循环发送‘0’和' ’。16进制是0x30 0x0d

    int main(void)
    {
      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
      RCC_HSE_Configuration();//外部高速时钟初始化
      SysTick_Init();
      IO_Init();//初始化板子端口
      Uart_Init(115200);//初始化串口
    
      while (1)/* Infinite loop */
      {
        UART_SendByte( '0');
        UART_SendByte('
    ');
        Delay(1000);
      }
    }
    
    void IO_Init(void)
    {
      GPIO_InitTypeDef  GPIO_InitStructure;
      
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    // 配置 USART1 Tx (PA.09) 作为功能引脚并上拉输出模式 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_ResetBits(GPIOA, GPIO_Pin_9); //配置 USART1 Tx (PA.10) 作为功能引脚并是浮空输入模式 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } void Uart_Init(uint32_t Baud) { USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStructure.USART_BaudRate = Baud; 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(USART1, &USART_InitStructure); // USART_ClearFlag(USART1, USART_FLAG_TC); USART_Cmd(USART1,ENABLE); }

    结果下载完程序,运行后发送出来的第一字节是0x3f 然后才正常的0x30 0x0d,感觉很奇怪,花时间去测试。

    尝试一:把串口的两个端口配置放在串口配置函数里,如下

    void Uart_Init(uint32_t Baud)
    {
      USART_InitTypeDef USART_InitStructure;
      GPIO_InitTypeDef  GPIO_InitStructure;
      
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
      
      // 配置 USART1 Tx (PA.09) 作为功能引脚并上拉输出模式
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
    
      //配置 USART1 Tx (PA.10) 作为功能引脚并是浮空输入模式
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
    
      USART_InitStructure.USART_BaudRate = Baud;
      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(USART1, &USART_InitStructure);
    //  USART_ClearFlag(USART1, USART_FLAG_TC);
      USART_Cmd(USART1,ENABLE);
    }

    这样就正常了,上电就收到0x30 0x0d

    尝试二:这样修改,把串口外设时钟在端口配置函数里面就打开。

    void IO_Init(void)
    {
      GPIO_InitTypeDef  GPIO_InitStructure;
    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//这里提前打开串口外设时钟
    
      // 配置 USART1 Tx (PA.09) 作为功能引脚并上拉输出模式
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
      GPIO_ResetBits(GPIOA, GPIO_Pin_9);
    
      //配置 USART1 Tx (PA.10) 作为功能引脚并是浮空输入模式
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
    } 

    也是可以正常输出0x30 0x0d

    总结:stm32的外设时钟应该在此外设端口配置之前打开

  • 相关阅读:
    5.11号团队冲刺(十)
    5.10号团队冲刺(九)
    python day04
    python day03
    python day02
    python day01
    模板语法标签继承关系
    DNS解析详情
    和域名相关的知识
    Webpack 4.X webpack.config.js 文件配置(一)
  • 原文地址:https://www.cnblogs.com/IdeaMing/p/11462692.html
Copyright © 2011-2022 走看看