stm32f103c8串口USART1发送多一字节
发布时间:2020-12-16 09:17:23 所属栏目:百科 来源:网络整理
导读:用UART写了一段Bootloader代码,遇到了一个很奇怪的现象。 代码如下:简单介绍一下就是先统一配置MCU的IO端口,然后配置串口参数,然后循环发送‘0’和‘r’。16进制是0x30 0x0d int main(void){ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); RCC_HSE_
用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); 结果下载完程序,运行后发送出来的第一字节是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的外设时钟应该在此外设端口配置之前打开。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |