大佬教程收集整理的这篇文章主要介绍了stm32f103c8串口USART1发送多一字节,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
用UART写了一段Bootloader代码,遇到了@L_674_1@很奇怪的现象。
代码如下:简单介绍一下就是先统一配置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,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); }
也是可以正常输出0x30 0x0d
总结:stm32的外设时钟应该在此外设端口配置之前打开。
以上是大佬教程为你收集整理的stm32f103c8串口USART1发送多一字节全部内容,希望文章能够帮你解决stm32f103c8串口USART1发送多一字节所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。