With UART Bootloader wrote some code that encountered a very strange phenomenon.
Code is as follows: a brief introduction is the first unified configuration IO port MCU, and then configure the serial port parameters, and then sent cyclically '0' and '\ r'. Hexadecimal is 0x30 0x0d
main int (void) { NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2); RCC_HSE_Configuration (); // initialize external high-speed clock SysTick_Init (); IO_Init (); // initialize port board Uart_Init (115200); // initialize the serial port while (1) / * Infinite * Loop / { UART_SendByte ( '0'); UART_SendByte ( '\ R & lt'); Delay (1000); } } void IO_Init (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, the ENABLE);
// configuration USART1 Tx (PA. 09) as a function pin and a pull mode GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (with GPIOA, & GPIO_InitStructure); GPIO_ResetBits (GPIO, GPIO_Pin_9); //配置USART1 Tx (PA.10)作为功能引脚并是浮空输入模式 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init (GPIO, & 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); }
The results have downloaded the program, the first byte sent out after the operation is normal before 0x3f 0x30 0x0d, feeling very strange, take the time to test.
Try one: two serial port configuration on the serial port configuration function, the following
Uart_Init void (uint32_t Baud) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, the ENABLE); // configuration USART1 Tx (PA.09) as a function pin and a pull mode GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (with GPIOA, & GPIO_InitStructure); // configuration USART1 Tx (PA.10) as a function pin and a floating input mode GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init (with 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); }
This is normal, the power they received 0x30 0x0d
Try two: this modification, the serial clock is opened in the peripheral port configuration function inside.
IO_Init void (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, the ENABLE); RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, the ENABLE); // open the port where the clock is advanced peripheral // configuration USART1 Tx (PA.09) as a function pin and the pull output mode GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (with GPIOA, & GPIO_InitStructure); GPIO_ResetBits (with GPIOA, GPIO_Pin_9); // configuration USART1 Tx (PA.10) as a function pins, and a floating input mode GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init (with GPIOA, & GPIO_InitStructure); }
Also normal output 0x30 0x0d
Summary: the STM32 peripherals clock should be set to open before the addition port configuration .