最近学习到串口发送数据时遇到一个问题:第一个字节总是FE,后面才是对的数据。
最终解决的方法是:
讲GPIO复用的操作放到GPIO配置之前!
体现在代码中就是:
1 //打开GPIO时钟 2 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); 3 //打开USART的时钟 4 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); 5 6 //GPIOA的Pin9和Pin10复用为串口1使用 7 GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); 8 GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); 9 10 //配置USART_TX为推挽复用 11 GPIO_InitStruct.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN; 12 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 13 GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 14 GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 15 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; 16 GPIO_Init(DEBUG_USART_TX_GPIO_PORT,&GPIO_InitStruct); 17 18 //配置USART_RX为浮空输入 19 GPIO_InitStruct.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN; 20 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; 21 GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; 22 GPIO_Init(DEBUG_USART_RX_GPIO_PORT,&GPIO_InitStruct);