jkl21 发表于 2024-9-24 07:55

基于中断的UART通信



#include "ddl.h"
#include "uart.h"
#include "gpio.h"
#include "sysctrl.h"

#define   T1_PORT               (3)
#define   T1_PIN                  (3)

volatile static uint8_t u8RxData;
volatile static uint8_t u8TxCnt=0;
volatile static uint8_t u8RxCnt=0;

void App_UartCfg(void);
void App_PortInit(void);

int32_t main(void)
{      
    //串口引脚配置
    App_PortInit();
   
    //串口配置
    App_UartCfg();
      
    while(1)
    {
      if(u8RxCnt>=1)
      {
            u8RxCnt = 0;
            Uart_SendDataIt(M0P_UART1, ~u8RxData); //启动UART1发送第一个字节         
      }

    }
}

//UART1中断函数
void Uart1_IRQHandler(void)
{
    if(Uart_GetStatus(M0P_UART1, UartRC))         //UART1数据接收
    {
      Uart_ClrStatus(M0P_UART1, UartRC);      //清中断状态位
      u8RxData = Uart_ReceiveData(M0P_UART1);   //接收数据字节
      u8RxCnt++;
    }
   
    if(Uart_GetStatus(M0P_UART1, UartTC))         //UART1数据发送
    {
      Uart_ClrStatus(M0P_UART1, UartTC);      //清中断状态位
      u8TxCnt++;
    }

}

//串口引脚配置
void App_PortInit(void)
{
    stc_gpio_cfg_t stcGpioCfg;
   
    DDL_ZERO_STRUCT(stcGpioCfg);
   
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //使能GPIO模块时钟
   
    ///<TX
    stcGpioCfg.enDir = GpioDirOut;
    Gpio_Init(GpioPortD, GpioPin7, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortD, GpioPin7, GpioAf1);          //配置PA02 端口为URART1_TX
   
    ///<RX
    stcGpioCfg.enDir = GpioDirIn;
    Gpio_Init(GpioPortD, GpioPin6, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortD, GpioPin6, GpioAf1);          //配置PA03 端口为URART1_RX
}

//串口配置
void App_UartCfg(void)
{
    stc_uart_cfg_t    stcCfg;

    DDL_ZERO_STRUCT(stcCfg);
   
    ///< 开启外设时钟
    Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);///<使能uart1模块时钟
   
    ///<UART Init
    stcCfg.enRunMode      = UartMskMode3;          ///<模式3
    stcCfg.enStopBit      = UartMsk1bit;         ///<1bit停止位
    stcCfg.enMmdorCk      = UartMskEven;         ///<偶检验
    stcCfg.stcBaud.u32Baud= 9600;                  ///<波特率9600
    stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;       ///<通道采样分频配置
    stcCfg.stcBaud.u32Pclk= Sysctrl_GetPClkFreq(); ///<获得外设时钟(PCLK)频率值
    Uart_Init(M0P_UART1, &stcCfg);                   ///<串口初始化
   
    ///<UART中断使能
    Uart_ClrStatus(M0P_UART1,UartRC);                ///<清接收请求
    Uart_ClrStatus(M0P_UART1,UartTC);                ///<清接收请求
    Uart_EnableIrq(M0P_UART1,UartRxIrq);             ///<使能串口接收中断
    Uart_EnableIrq(M0P_UART1,UartTxIrq);             ///<使能串口接收中断   
    EnableNvic(UART1_3_IRQn, IrqLevel3, TRUE);       ///<系统中断使能

}https://blog.csdn.net/heqiunong/article/details/125974396

yangxiaor520 发表于 2024-9-24 13:26

使用中断处理串口收发数据,对CPU的利用率更高。

shenxiaolin 发表于 2024-9-29 10:27

感觉用起来好复杂

更多更合适ii 发表于 2024-9-30 17:28

你可以使用环形缓冲区来处理接收到的数据,而不是仅依赖u8RxCnt。这样可以支持更高效的数据接收。
页: [1]
查看完整版本: 基于中断的UART通信