在设置stm32的时候,我发现我将串口设置为7 Even 1的模式,可以发送但是不能正确接收数据,我用的芯片的型号是stm32f303,用stm32F1的开发板时也发现这个问题。以下是stm32f303的串口初始化代码:- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
-
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = BAUDRATE_9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//USART_WordLength包涵数据位和校验位,故为8
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_Even;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(UART_TYPE_RS485, &USART_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQ_RS485_MAIN_PRIO;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQ_RS485_SUB_PRIO;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- USART_ITConfig(UART_TYPE_RS485, USART_IT_RXNE, ENABLE);
- USART_Cmd(UART_TYPE_RS485, ENABLE);
复制代码
|
以下是stm32f103的串口初始化代码:
USART_WordLength包含数据位和校验位,故为8