c疯鸟 发表于 2018-12-8 23:44:10

求:STM32串口设置为 7 Even 1 的方法

在设置stm32的时候,我发现我将串口设置为7 Even 1的模式,可以发送但是不能正确接收数据,我用的芯片的型号是stm32f303,用stm32F1的开发板时也发现这个问题。以下是stm32f303的串口初始化代码:USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDefNVIC_InitStructure;
    GPIO_InitTypeDefGPIO_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);   



c疯鸟 发表于 2018-12-8 23:47:10

本帖最后由 c疯鸟 于 2018-12-9 09:38 编辑

以下是stm32f103的串口初始化代码:
void USART_Config(void)
{
      GPIO_InitTypeDef GPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;

      // 打开串口GPIO的时钟
      DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
      
      // 打开串口外设的时钟
      DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);

      // 将USART Tx的GPIO配置为推挽复用模式
      GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);

// 将USART Rx的GPIO配置为浮空输入模式
      GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
      
      // 配置串口的工作参数
      // 配置波特率
      USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
      // 配置 针数据字长
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      // 配置停止位
      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(DEBUG_USARTx, &USART_InitStructure);
      
      // 串口中断优先级配置
      NVIC_Configuration();
      
      // 使能串口接收中断
      USART_ITConfig(DEBUG_USARTx, USART_IT_RXNE, ENABLE);      
      
      // 使能串口
      USART_Cmd(DEBUG_USARTx, ENABLE);            
}
USART_WordLength包含数据位和校验位,故为8

c疯鸟 发表于 2018-12-10 09:56:39

我知道了,输入要&0x7F得到正确数值
页: [1]
查看完整版本: 求:STM32串口设置为 7 Even 1 的方法