打印

GD32E230F4P6串口发送中断进几次不能进入了

[复制链接]
133|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
北辰二七|  楼主 | 2025-6-17 09:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void USART0_Init(void)
{
                rcu_periph_clock_enable(RCU_USART0);


    /* configure USART Tx as alternate function push-pull */
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);

    /* configure USART Rx as alternate function push-pull */
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
          // PA10 - USART_RX (浮空输入)
    //gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
            /* connect port to USARTx_Tx */
    gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);

    /* connect port to USARTx_Rx */
    gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
                usart_deinit(USART0);
    usart_baudrate_set(USART0, 115200U);
                usart_word_length_set(USART0, USART_WL_8BIT); // 8位数据
    usart_stop_bit_set(USART0, USART_STB_1BIT); // 1位停止位
    usart_parity_config(USART0, USART_PM_NONE); // 无校验
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
               
                usart_interrupt_enable(USART0, USART_INT_RBNE); // 使能USART0接收中断
    nvic_irq_enable(USART0_IRQn, 0);
                usart_enable(USART0);
}
void USART0_IRQHandler(void)
{
        uint8_t ucChar;

        // 检查是否是接收中断
        if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET)
        {
        // 读取接收到的数据
                ucChar = usart_data_receive(USART0);

                ms_comm.start_end = (ms_comm.start_end << 8) | ucChar;

                if (ms_comm.start_end == MasterStartRx)
                {
                        ms_comm.rx_start_ok = 1;
                        ms_comm.rx_index = 4;
                        ms_comm.rx_len = 12;
                }
                else if (ms_comm.rx_start_ok == 1)
                {
                        ms_comm.rx[ms_comm.rx_index] = ucChar;
                        ms_comm.rx_index++;

                        if (MsStartEnd16_t[0] == MsEnd)
                        {
                                if (ms_comm.rx_index == ms_comm.rx_len)
                                {
                                        ms_comm.rx_start_ok = 0;
                                        Ms_Decode();
                                }
                        }
                        if (ms_comm.rx_index >= sizeof (ms_cmd_t))
                        ms_comm.rx_index = 4;
                }
               
                        //usart_interrupt_flag_clear(USART0,USART_INT_FLAG_RBNE);
        }

        // 检查是否是发送中断(如果启用了发送中断)
        if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_TC) != RESET)
        {

                if (ms_comm.tx_index < ms_comm.tx_len)
                        {
                        // Ms_SendByte(MsTxBuf[ms_comm.TxIndex++]);
                                usart_data_transmit(USART0, MsTxBuf[ms_comm.tx_index++]);
                        }
                else
                        {
                                Clr_Ms_INT_TC;//关闭中断
                                ms_comm.tx_len = 0;
                                ms_comm.wait_time = 10;
                // LCDm_CTL_L;
                        }
                       
        }

}

使用特权

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

本版积分规则

1

主题

1

帖子

0

粉丝