打印

GD32F450VET6串口IRDA功能接受数据正确,发送数据错误

[复制链接]
2081|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 封弊账号 于 2021-6-17 12:24 编辑

硬件连接如上图所示


实际连接单片机串口连接TFD4101,电脑串口连接红外头
软件相关设置
串口1开启irda功能,特殊设置都在下面函数中,正常设置都在gd_eval_com_init中
void all_usart_config(void)
{   
    //  config baud rate
    gd_eval_com_init(EVAL_COM1, 9600U);
    gd_eval_com_init(EVAL_COM2, 9600U);
    gd_eval_com_init(EVAL_COM3, 9600U);
    gd_eval_com_init(EVAL_COM4, 9600U);
    gd_eval_com_init(EVAL_COM5, 9600U);
    gd_eval_com_init(EVAL_COM6, 9600U);
    gd_eval_com_init(EVAL_COM7, 9600U);
    //  config USART interrupt the pre-emption priority
    nvic_irq_enable(USART0_IRQn, 1, 0); //  the pre-emption priority needed to set
    nvic_irq_enable(USART1_IRQn, 0, 1); //  the pre-emption priority needed to set
    nvic_irq_enable(USART2_IRQn, 1, 2); //  the pre-emption priority needed to set
    nvic_irq_enable(UART3_IRQn, 1, 3);  //  the pre-emption priority needed to set
    nvic_irq_enable(USART5_IRQn, 1, 4); //  the pre-emption priority needed to set
    nvic_irq_enable(UART6_IRQn, 1, 5);  //  the pre-emption priority needed to set
    nvic_irq_enable(UART7_IRQn, 1, 6);  //  the pre-emption priority needed to set     
    //  enable USART TBE interrupt
    usart_interrupt_enable(USART0, USART_INTEN_RBNEIE);

    //usart_irda_mode_enable(USART1);
    usart_word_length_set(USART1,USART_WL_8BIT);
    usart_data_first_config(USART1, USART_MSBF_MSB);
    //usart_stop_bit_set(USART1, USART_STB_1BIT);
    usart_parity_config(USART1, USART_PM_NONE);
    usart_irda_lowpower_config(USART1,USART_IRLP_NORMAL);
    usart_prescaler_config(USART1,0x01);
    //usart_invert_config(USART1,USART_DINV_DISABLE);   
    usart_irda_mode_enable(USART1);
    usart_lin_mode_disable(USART1);
    usart_synchronous_clock_disable(USART1);
    usart_halfduplex_disable(USART1);
    usart_smartcard_mode_disable(USART1);

    usart_interrupt_enable(USART1, USART_INTEN_RBNEIE);

    usart_interrupt_enable(USART2, USART_INTEN_RBNEIE);
    usart_interrupt_enable(UART3, USART_INTEN_RBNEIE);
    usart_interrupt_enable(USART5, USART_INTEN_RBNEIE);
    usart_interrupt_enable(UART6, USART_INTEN_RBNEIE);
    usart_interrupt_enable(UART7, USART_INTEN_RBNEIE);
}

正常串口初始化,引脚模式、波特率等都在这里
void gd_eval_com_init(uint32_t com, uint32_t baudval)
{
    /* enable GPIO clock */
    uint32_t COM_ID;
    //if(EVAL_COM1 == com)
    //{
    //    COM_ID = 0U;
    //}
    switch(com) //  ÔÚÒ»¸ö´®¿ÚµÄ»ù´¡ÉÏÔö¼Ó
    {
        case EVAL_COM1:
            COM_ID = 0U;
            break;
        case EVAL_COM2:
            COM_ID = 1U;
            break;
        case EVAL_COM3:
            COM_ID = 2U;
            break;
        case EVAL_COM4:
            COM_ID = 3U;
            break;
        case EVAL_COM5:
            COM_ID = 4U;
            break;
        case EVAL_COM6:
            COM_ID = 5U;
            break;
        case EVAL_COM7:
            COM_ID = 6U;
            break;         
        default:
            break;           
    }
    //rcu_periph_clock_enable( EVAL_COM_GPIO_CLK);
    rcu_periph_clock_enable(COM_GPIO_CLK[COM_ID]);

    /* enable USART clock */
    rcu_periph_clock_enable(COM_CLK[COM_ID]);

    /* connect port to USARTx_Tx */
    //gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]);   //  set GPIO alternate function
    gpio_af_set(COM_GPIO_PORT[COM_ID], COM_AF[COM_ID], COM_TX_PIN[COM_ID]);   //  set GPIO alternate function

    /* connect port to USARTx_Rx */
    //gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);
    gpio_af_set(COM_GPIO_PORT[COM_ID], COM_AF[COM_ID], COM_RX_PIN[COM_ID]);

    /* configure USART Tx as alternate function push-pull */
    //gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,COM_TX_PIN[COM_ID]);
    gpio_mode_set(COM_GPIO_PORT[COM_ID], GPIO_MODE_AF, GPIO_PUPD_PULLDOWN,COM_TX_PIN[COM_ID]);
    //gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_TX_PIN[COM_ID]);
    gpio_output_options_set(COM_GPIO_PORT[COM_ID], GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_TX_PIN[COM_ID]);

    /* configure USART Rx as alternate function push-pull */
    //gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,COM_RX_PIN[COM_ID]);
    gpio_mode_set(COM_GPIO_PORT[COM_ID], GPIO_MODE_AF, GPIO_PUPD_PULLDOWN,COM_RX_PIN[COM_ID]);
    //gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_RX_PIN[COM_ID]);
    gpio_output_options_set(COM_GPIO_PORT[COM_ID], GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,COM_RX_PIN[COM_ID]);

    /* USART configure */
    usart_deinit(com);
    usart_baudrate_set(com,baudval);
    usart_stop_bit_set(com, USART_STB_1BIT);
    usart_receive_config(com, USART_RECEIVE_ENABLE);
    usart_transmit_config(com, USART_TRANSMIT_ENABLE);

    usart_enable(com);   
}

串口1中断,直接自发自首
void USART1_IRQHandler(void)
{
    uint16_t gd_usart1_receive_data;
    if((RESET != usart_interrupt_flag_get(USART1, USART_INT_RBNEIE)) &&
       (RESET != usart_flag_get(USART1, USART_FLAG_RBNE)))
    {
        /* receive data */
        gd_usart1_receive_data = (usart_data_receive(USART1) & 0xFF);
        usart_data_transmit(USART1,gd_usart1_receive_data);
    }
}

串口1发送函数
void usart_data_transmit(uint32_t usart_periph, uint32_t data)
{
    USART_DATA(usart_periph) = ((uint16_t)USART_DATA_DATA & data);
    while (RESET == usart_flag_get(usart_periph,USART_FLAG_TBE));
}

串口调试界面发送0xeb后回0xeb发送0x90回复0x70有时还0x50

接受全部正常,发送数据错误

2.png (29.14 KB )

串口调试

串口调试

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

1

粉丝