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的外设时钟应该在此外设端口配置之前打开

  • 相关阅读:
    ZOJ 3332 Strange Country II
    ZOJ 3331 Process the Tasks(双塔DP)
    ZOJ 3326 An Awful Problem(模拟)
    HDU 1796 How many integers can you find(容斥原理)
    HDU 4059 The Boss on Mars(容斥原理)
    HDU 4135 Co-prime(容斥原理)
    HDU 5677 ztr loves substring(回文串加多重背包)
    CodeForces 668B Little Artem and Dance
    CodeForces 667A Pouring Rain
    Java实现 LeetCode 764 最大加号标志(暴力递推)
  • 原文地址:https://www.cnblogs.com/IdeaMing/p/11462692.html
Copyright © 2011-2022 走看看