STM32F103C8的USART1如何发送超过一个字节的数据?

2026-04-16 20:583阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计608个文字,预计阅读时间需要3分钟。

STM32F103C8的USART1如何发送超过一个字节的数据?

使用UART编写了Bootloader代码,遇到了一个奇怪的现象。代码如下:简单介绍下,首先是对MCU的IO端口进行配置,然后设置串口参数,接着循环发送0和\r\n字符,16进制是0x30+0x0d。

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

代码如下:简单介绍一下就是先统一配置MCU的IO端口,然后配置串口参数,然后循环发送‘0’和‘\r’。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(‘\r‘); 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

STM32F103C8的USART1如何发送超过一个字节的数据?

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

本文共计608个文字,预计阅读时间需要3分钟。

STM32F103C8的USART1如何发送超过一个字节的数据?

使用UART编写了Bootloader代码,遇到了一个奇怪的现象。代码如下:简单介绍下,首先是对MCU的IO端口进行配置,然后设置串口参数,接着循环发送0和\r\n字符,16进制是0x30+0x0d。

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

代码如下:简单介绍一下就是先统一配置MCU的IO端口,然后配置串口参数,然后循环发送‘0’和‘\r’。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(‘\r‘); 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

STM32F103C8的USART1如何发送超过一个字节的数据?

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