- #include "ddl.h"
- #include "uart.h"
- #include "gpio.h"
- #include "sysctrl.h"
- /******************************************************************************
- * Local pre-processor symbols/macros ('#define')
- ******************************************************************************/
- #define T1_PORT (3)
- #define T1_PIN (3)
- /******************************************************************************
- * Global variable definitions (declared in header file with 'extern')
- ******************************************************************************/
- /******************************************************************************
- * Local type definitions ('typedef')
- ******************************************************************************/
- /******************************************************************************
- * Local function prototypes ('static')
- ******************************************************************************/
- /******************************************************************************
- * Local variable definitions ('static') *
- ******************************************************************************/
- volatile static uint8_t u8RxData;
- volatile static uint8_t u8TxCnt=0;
- volatile static uint8_t u8RxCnt=0;
- /*****************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- void App_UartCfg(void);
- void App_PortInit(void);
- /**
- ******************************************************************************
- ** \brief Main function of project
- **
- ** \return uint32_t return value, if needed
- **
- ** This sample
- **
- ******************************************************************************/
- int32_t main(void)
- {
- //串口引脚配置
- App_PortInit();
- //串口配置
- App_UartCfg();
- while(1)
- {
- if(u8RxCnt>=1)
- {
- u8RxCnt = 0;
- Uart_SendDataIt(M0P_UART0, ~u8RxData); //启动UART0发送第一个字节
- }
- }
- }
- //UART0中断函数
- void Uart0_IRQHandler(void)
- {
- if(Uart_GetStatus(M0P_UART0, UartRC)) //UART0数据接收
- {
- Uart_ClrStatus(M0P_UART0, UartRC); //清中断状态位
- u8RxData = Uart_ReceiveData(M0P_UART0); //接收数据字节
- u8RxCnt++;
- }
- if(Uart_GetStatus(M0P_UART0, UartTC)) //UART0数据发送
- {
- Uart_ClrStatus(M0P_UART0, 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(GpioPortA, GpioPin9, &stcGpioCfg);
- Gpio_SetAfMode(GpioPortA, GpioPin9, GpioAf1); //配置PA09 端口为URART1_TX
- ///<RX
- stcGpioCfg.enDir = GpioDirIn;
- Gpio_Init(GpioPortA, GpioPin10, &stcGpioCfg);
- Gpio_SetAfMode(GpioPortA, GpioPin10, GpioAf1); //配置PA10 端口为URART1_RX
- }
- //串口配置
- void App_UartCfg(void)
- {
- stc_uart_cfg_t stcCfg;
- DDL_ZERO_STRUCT(stcCfg);
- ///< 开启外设时钟
- Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0,TRUE);///<使能uart0模块时钟
- ///<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_UART0, &stcCfg); ///<串口初始化
- ///<UART中断使能
- Uart_ClrStatus(M0P_UART0,UartRC); ///<清接收请求
- Uart_ClrStatus(M0P_UART0,UartTC); ///<清接收请求
- Uart_EnableIrq(M0P_UART0,UartRxIrq); ///<使能串口接收中断
- Uart_EnableIrq(M0P_UART0,UartTxIrq); ///<使能串口接收中断
- EnableNvic(UART0_IRQn, IrqLevel3, TRUE); ///<系统中断使能
- }
- /******************************************************************************
- * EOF (not truncated)
- ******************************************************************************/
官方库使用UART1(PA2+PA3)的代码:
- #include "ddl.h"
- #include "uart.h"
- #include "gpio.h"
- #include "sysctrl.h"
- /******************************************************************************
- * Local pre-processor symbols/macros ('#define')
- ******************************************************************************/
- #define T1_PORT (3)
- #define T1_PIN (3)
- /******************************************************************************
- * Global variable definitions (declared in header file with 'extern')
- ******************************************************************************/
- /******************************************************************************
- * Local type definitions ('typedef')
- ******************************************************************************/
- /******************************************************************************
- * Local function prototypes ('static')
- ******************************************************************************/
- /******************************************************************************
- * Local variable definitions ('static') *
- ******************************************************************************/
- volatile static uint8_t u8RxData;
- volatile static uint8_t u8TxCnt=0;
- volatile static uint8_t u8RxCnt=0;
- /*****************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- void App_UartCfg(void);
- void App_PortInit(void);
- /**
- ******************************************************************************
- ** \brief Main function of project
- **
- ** \return uint32_t return value, if needed
- **
- ** This sample
- **
- ******************************************************************************/
- 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(GpioPortA, GpioPin2, &stcGpioCfg);
- Gpio_SetAfMode(GpioPortA, GpioPin2, GpioAf1); //配置PA02 端口为URART1_TX
- ///<RX
- stcGpioCfg.enDir = GpioDirIn;
- Gpio_Init(GpioPortA, GpioPin3, &stcGpioCfg);
- Gpio_SetAfMode(GpioPortA, GpioPin3, 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_IRQn, IrqLevel3, TRUE); ///<系统中断使能
- }
- /******************************************************************************
- * EOF (not truncated)
- ******************************************************************************/