[技术问答] HC32L136J8TA 串口0中断

[复制链接]
1153|3
 楼主| t9080350 发表于 2022-4-11 23:09 | 显示全部楼层 |阅读模式
问题描述:串口0使用PA9和PA10,可以发送无法进入接收中断。程序是从官方例程PA2+PA3修改来的,版本1.9.2困扰两三天了,求各位高手指点,万分感谢!!


我的代码:
  1. #include "ddl.h"
  2. #include "uart.h"
  3. #include "gpio.h"
  4. #include "sysctrl.h"

  5. /******************************************************************************
  6. * Local pre-processor symbols/macros ('#define')
  7. ******************************************************************************/
  8. #define     T1_PORT                 (3)
  9. #define     T1_PIN                  (3)

  10. /******************************************************************************
  11. * Global variable definitions (declared in header file with 'extern')
  12. ******************************************************************************/

  13. /******************************************************************************
  14. * Local type definitions ('typedef')
  15. ******************************************************************************/

  16. /******************************************************************************
  17. * Local function prototypes ('static')
  18. ******************************************************************************/

  19. /******************************************************************************
  20. * Local variable definitions ('static')                                      *
  21. ******************************************************************************/
  22. volatile static uint8_t u8RxData;
  23. volatile static uint8_t u8TxCnt=0;
  24. volatile static uint8_t u8RxCnt=0;

  25. /*****************************************************************************
  26. * Function implementation - global ('extern') and local ('static')
  27. ******************************************************************************/
  28. void App_UartCfg(void);
  29. void App_PortInit(void);

  30. /**
  31. ******************************************************************************
  32. ** \brief  Main function of project
  33. **
  34. ** \return uint32_t return value, if needed
  35. **
  36. ** This sample
  37. **
  38. ******************************************************************************/
  39. int32_t main(void)
  40. {
  41.     //串口引脚配置
  42.     App_PortInit();

  43.     //串口配置
  44.     App_UartCfg();

  45.     while(1)
  46.     {
  47.         if(u8RxCnt>=1)
  48.         {
  49.             u8RxCnt = 0;
  50.             Uart_SendDataIt(M0P_UART0, ~u8RxData); //启动UART0发送第一个字节
  51.         }

  52.     }
  53. }

  54. //UART0中断函数
  55. void Uart0_IRQHandler(void)
  56. {
  57.     if(Uart_GetStatus(M0P_UART0, UartRC))         //UART0数据接收
  58.     {
  59.         Uart_ClrStatus(M0P_UART0, UartRC);        //清中断状态位
  60.         u8RxData = Uart_ReceiveData(M0P_UART0);   //接收数据字节
  61.         u8RxCnt++;
  62.     }

  63.     if(Uart_GetStatus(M0P_UART0, UartTC))         //UART0数据发送
  64.     {
  65.         Uart_ClrStatus(M0P_UART0, UartTC);        //清中断状态位
  66.         u8TxCnt++;
  67.     }

  68. }

  69. //串口引脚配置
  70. void App_PortInit(void)
  71. {
  72.     stc_gpio_cfg_t stcGpioCfg;

  73.     DDL_ZERO_STRUCT(stcGpioCfg);

  74.     Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //使能GPIO模块时钟

  75.     ///<TX
  76.     stcGpioCfg.enDir = GpioDirOut;
  77.     Gpio_Init(GpioPortA, GpioPin9, &stcGpioCfg);
  78.     Gpio_SetAfMode(GpioPortA, GpioPin9, GpioAf1);          //配置PA09 端口为URART1_TX

  79.     ///<RX
  80.     stcGpioCfg.enDir = GpioDirIn;
  81.     Gpio_Init(GpioPortA, GpioPin10, &stcGpioCfg);
  82.     Gpio_SetAfMode(GpioPortA, GpioPin10, GpioAf1);          //配置PA10 端口为URART1_RX
  83. }

  84. //串口配置
  85. void App_UartCfg(void)
  86. {
  87.     stc_uart_cfg_t    stcCfg;

  88.     DDL_ZERO_STRUCT(stcCfg);

  89.     ///< 开启外设时钟
  90.     Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0,TRUE);///<使能uart0模块时钟

  91.     ///<UART Init
  92.     stcCfg.enRunMode        = UartMskMode3;          ///<模式3
  93.     stcCfg.enStopBit        = UartMsk1bit;           ///<1bit停止位
  94.     stcCfg.enMmdorCk        = UartMskEven;           ///<偶检验
  95.     stcCfg.stcBaud.u32Baud  = 9600;                  ///<波特率9600
  96.     stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;       ///<通道采样分频配置
  97.     stcCfg.stcBaud.u32Pclk  = Sysctrl_GetPClkFreq(); ///<获得外设时钟(PCLK)频率值
  98.     Uart_Init(M0P_UART0, &stcCfg);                   ///<串口初始化

  99.     ///<UART中断使能
  100.     Uart_ClrStatus(M0P_UART0,UartRC);                ///<清接收请求
  101.     Uart_ClrStatus(M0P_UART0,UartTC);                ///<清接收请求
  102.     Uart_EnableIrq(M0P_UART0,UartRxIrq);             ///<使能串口接收中断
  103.     Uart_EnableIrq(M0P_UART0,UartTxIrq);             ///<使能串口接收中断
  104.     EnableNvic(UART0_IRQn, IrqLevel3, TRUE);       ///<系统中断使能

  105. }

  106. /******************************************************************************
  107. * EOF (not truncated)
  108. ******************************************************************************/


官方库使用UART1(PA2+PA3)的代码:
  1. #include "ddl.h"
  2. #include "uart.h"
  3. #include "gpio.h"
  4. #include "sysctrl.h"

  5. /******************************************************************************
  6. * Local pre-processor symbols/macros ('#define')
  7. ******************************************************************************/
  8. #define     T1_PORT                 (3)
  9. #define     T1_PIN                  (3)

  10. /******************************************************************************
  11. * Global variable definitions (declared in header file with 'extern')
  12. ******************************************************************************/

  13. /******************************************************************************
  14. * Local type definitions ('typedef')
  15. ******************************************************************************/

  16. /******************************************************************************
  17. * Local function prototypes ('static')
  18. ******************************************************************************/

  19. /******************************************************************************
  20. * Local variable definitions ('static')                                      *
  21. ******************************************************************************/
  22. volatile static uint8_t u8RxData;
  23. volatile static uint8_t u8TxCnt=0;
  24. volatile static uint8_t u8RxCnt=0;

  25. /*****************************************************************************
  26. * Function implementation - global ('extern') and local ('static')
  27. ******************************************************************************/
  28. void App_UartCfg(void);
  29. void App_PortInit(void);

  30. /**
  31. ******************************************************************************
  32. ** \brief  Main function of project
  33. **
  34. ** \return uint32_t return value, if needed
  35. **
  36. ** This sample
  37. **
  38. ******************************************************************************/
  39. int32_t main(void)
  40. {
  41.     //串口引脚配置
  42.     App_PortInit();

  43.     //串口配置
  44.     App_UartCfg();

  45.     while(1)
  46.     {
  47.         if(u8RxCnt>=1)
  48.         {
  49.             u8RxCnt = 0;
  50.             Uart_SendDataIt(M0P_UART1, ~u8RxData); //启动UART1发送第一个字节
  51.         }

  52.     }
  53. }

  54. //UART1中断函数
  55. void Uart1_IRQHandler(void)
  56. {
  57.     if(Uart_GetStatus(M0P_UART1, UartRC))         //UART1数据接收
  58.     {
  59.         Uart_ClrStatus(M0P_UART1, UartRC);        //清中断状态位
  60.         u8RxData = Uart_ReceiveData(M0P_UART1);   //接收数据字节
  61.         u8RxCnt++;
  62.     }

  63.     if(Uart_GetStatus(M0P_UART1, UartTC))         //UART1数据发送
  64.     {
  65.         Uart_ClrStatus(M0P_UART1, UartTC);        //清中断状态位
  66.         u8TxCnt++;
  67.     }

  68. }

  69. //串口引脚配置
  70. void App_PortInit(void)
  71. {
  72.     stc_gpio_cfg_t stcGpioCfg;

  73.     DDL_ZERO_STRUCT(stcGpioCfg);

  74.     Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //使能GPIO模块时钟

  75.     ///<TX
  76.     stcGpioCfg.enDir = GpioDirOut;
  77.     Gpio_Init(GpioPortA, GpioPin2, &stcGpioCfg);
  78.     Gpio_SetAfMode(GpioPortA, GpioPin2, GpioAf1);          //配置PA02 端口为URART1_TX

  79.     ///<RX
  80.     stcGpioCfg.enDir = GpioDirIn;
  81.     Gpio_Init(GpioPortA, GpioPin3, &stcGpioCfg);
  82.     Gpio_SetAfMode(GpioPortA, GpioPin3, GpioAf1);          //配置PA03 端口为URART1_RX
  83. }

  84. //串口配置
  85. void App_UartCfg(void)
  86. {
  87.     stc_uart_cfg_t    stcCfg;

  88.     DDL_ZERO_STRUCT(stcCfg);

  89.     ///< 开启外设时钟
  90.     Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);///<使能uart1模块时钟

  91.     ///<UART Init
  92.     stcCfg.enRunMode        = UartMskMode3;          ///<模式3
  93.     stcCfg.enStopBit        = UartMsk1bit;           ///<1bit停止位
  94.     stcCfg.enMmdorCk        = UartMskEven;           ///<偶检验
  95.     stcCfg.stcBaud.u32Baud  = 9600;                  ///<波特率9600
  96.     stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;       ///<通道采样分频配置
  97.     stcCfg.stcBaud.u32Pclk  = Sysctrl_GetPClkFreq(); ///<获得外设时钟(PCLK)频率值
  98.     Uart_Init(M0P_UART1, &stcCfg);                   ///<串口初始化

  99.     ///<UART中断使能
  100.     Uart_ClrStatus(M0P_UART1,UartRC);                ///<清接收请求
  101.     Uart_ClrStatus(M0P_UART1,UartTC);                ///<清接收请求
  102.     Uart_EnableIrq(M0P_UART1,UartRxIrq);             ///<使能串口接收中断
  103.     Uart_EnableIrq(M0P_UART1,UartTxIrq);             ///<使能串口接收中断
  104.     EnableNvic(UART1_IRQn, IrqLevel3, TRUE);       ///<系统中断使能

  105. }

  106. /******************************************************************************
  107. * EOF (not truncated)
  108. ******************************************************************************/





mutable 发表于 2022-4-13 13:52 | 显示全部楼层
找官网的例程直接用啊
七毛钱 发表于 2022-4-24 11:10 来自手机 | 显示全部楼层
楼主这么喜欢走弯路啊,哈哈
wubangmi 发表于 2022-4-26 10:03 | 显示全部楼层
换个低版本的keil,估计你的编译器版本太高
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

21

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部