问答

汇集网友智慧,解决技术难题

21ic问答首页 - GD32F150C8T6配置USART1不成功

技术交流 嵌入式 国产芯片 单片机 MUC

GD32F150C8T6配置USART1不成功

额沽额沽2022-08-11
#define EVAL_COM1                        USART1
#define EVAL_COM1_CLK                    RCU_USART1

#define EVAL_COM1_TX_PIN                 GPIO_PIN_2   // PA2
#define EVAL_COM1_RX_PIN                 GPIO_PIN_3   // PA3

#define EVAL_COM_GPIO_PORT               GPIOA
#define EVAL_COM_GPIO_CLK                RCU_GPIOA
#define EVAL_COM_AF                      GPIO_AF_1
void gd_eval_com_init(uint32_t COM)

{
    uint32_t COM_ID = 0U;
    if(EVAL_COM1==COM){
    COM_ID = 0U;
    }


    rcu_periph_clock_enable(RCU_GPIOA);
    // USART时钟使能
    rcu_periph_clock_enable(RCU_USART1);

    // 配置TX为推挽复用模式
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
    gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,GPIO_PIN_2);
    // 配置RX为浮空输入模式
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3);
    gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,GPIO_PIN_3);

    usart_deinit(USART1);
    usart_baudrate_set(USART1, 9600U);                        // 波特率
    usart_word_length_set(USART1, USART_WL_8BIT);               // 帧数据字长
    usart_stop_bit_set(USART1, USART_STB_1BIT);                 // 停止位
    usart_parity_config(USART1, USART_PM_NONE);                 // 奇偶校验位
    usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE);  // 硬件流控制RTS
    usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE);  // 硬件流控制CTS
    usart_receive_config(USART1, USART_RECEIVE_ENABLE);         // 使能接收
    usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);       // 使能发送
    usart_enable(USART1);

    nvic_irq_enable(USART1_IRQn, 0, 1);
    // 使能串口接收中断
    usart_interrupt_enable(USART1, USART_INT_RBNE);
    usart_interrupt_enable(USART1, USART_INT_TBE);
}

void USART0_IRQHandler(void)
{
    if(RESET != usart_interrupt_flag_get(EVAL_COM1, USART_INT_FLAG_RBNE)){
        /* receive data */
        receiver_buffer[rxcount++] = usart_data_receive(EVAL_COM1);
        if(rxcount == receivesize){
            //usart_interrupt_disable(EVAL_COM1, USART_INT_RBNE);
        }
    }

    if(RESET != usart_interrupt_flag_get(EVAL_COM1, USART_INT_FLAG_TBE)){
        /* transmit data */
        usart_data_transmit(EVAL_COM1, transmitter_buffer[txcount++]);
        if(txcount == transfersize){
            //usart_interrupt_disable(EVAL_COM1, USART_INT_TBE);
        }
    }
}


不知道哪里出了问题,运行的时候,串口一直无法进入中断,而且程序会死在串口配置里面出不去。
我看芯片手册里明明写了PA2和PA3可以复用为串口。
请大神指教!





回答 +关注 6
758人浏览 4人回答问题 分享 举报
4 个回答
  • Bsp.zip (4.92 KB)
  • ////////中断执行程序//////
    void USART0_IRQHandler(void)
  • 额沽额沽 发表于 2022-8-11 17:13
    void gd_eval_com_init(uint32_t COM)
    {
        /* enable GPIO clock */

    你这个初始化的是USART1,,,中断的函数却是USART0_IRQHandler
  • void gd_eval_com_init(uint32_t COM)
    {
        /* enable GPIO clock */

        uint32_t COM_ID = 0U;
        if(EVAL_COM1==COM){
        COM_ID = 0U;
        }
        rcu_periph_clock_enable(RCU_GPIOA);
        // USART时钟使能
        rcu_periph_clock_enable(RCU_USART1);
       
        // 配置TX为推挽复用模式
        gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
        gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,GPIO_PIN_2);
        // 配置RX为浮空输入模式
        gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3);
        gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,GPIO_PIN_3);

        usart_deinit(USART1);
        usart_baudrate_set(USART1, 9600U);                        // 波特率
        usart_word_length_set(USART1, USART_WL_8BIT);               // 帧数据字长
        usart_stop_bit_set(USART1, USART_STB_1BIT);                 // 停止位
        usart_parity_config(USART1, USART_PM_NONE);                 // 奇偶校验位
        usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE);  // 硬件流控制RTS
        usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE);  // 硬件流控制CTS
        usart_receive_config(USART1, USART_RECEIVE_ENABLE);         // 使能接收
        usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);       // 使能发送
        usart_enable(USART1);

        nvic_irq_enable(USART1_IRQn, 0, 1);
        // 使能串口接收中断
        usart_interrupt_enable(USART1, USART_INT_RBNE);
        usart_interrupt_enable(USART1, USART_INT_TBE);
    }

    ////////中断执行程序//////
    void USART0_IRQHandler(void)
    {
        if(RESET != usart_interrupt_flag_get(EVAL_COM1, USART_INT_FLAG_RBNE)){
            /* receive data */
            receiver_buffer[rxcount++] = usart_data_receive(EVAL_COM1);
            if(rxcount == receivesize){
                //usart_interrupt_disable(EVAL_COM1, USART_INT_RBNE);
            }
        }

        if(RESET != usart_interrupt_flag_get(EVAL_COM1, USART_INT_FLAG_TBE)){
            /* transmit data */
            usart_data_transmit(EVAL_COM1, transmitter_buffer[txcount++]);
            if(txcount == transfersize){
                //usart_interrupt_disable(EVAL_COM1, USART_INT_TBE);
            }
        }
    }

    上述截图不清晰,重新截图一次

您需要登录后才可以回复 登录 | 注册