|
hc32l130使用内部晶振,开启串口发送和接受功能,串口工具发送数据给mcu,程序debug并没有进入到中断,下面是启用内部时钟并开启串口功能完整单片机代码。
- #include "ddl.h"
- #include "uart.h"
- #include "gpio.h"
- #include "flash.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);
- void App_ClkCfg(void);
- int32 t main (void)
- {
- App_ClkCfg () ;
- App_PortInit () ;
- App_UartCfg();
- while (1)
- {
- if(u8RxCnt>=1)
- {
- u8RxCnt = 0;
- Uart_SendDataIt(MOP_UART1, ~u8RxData);
- }
- }
- }
- void App_ClkCfg(void)
- {
- stc_sysctrl_clk_cfg_t sysctrl_clk_cfg;
- Sysctrl_SetPeripheralGate(SysctrlPeripheralFlash, TRUE);
- Flash_WaitCycle(FlashWaitCycle0);
- sysctrl_SetRCHTrim(SysctrlRchFreq8MHz);
- sysctrl_clk_cfg.enClkSrc = SysctrlClkRCH;
- sysctrl_clk_cfg.enHClkDiv = SysctrlHclkDiv1;
- sysctrl_clk_cfg.enPClkDiv = SysctrlPclkDiv1;
- Sysctrl_ClkInit(&sysctrl_clk_cfg);
- }
- void Uart1_IRQHandler(void)
- {
- if(Uart_GetStatue(M0P_UART1, UartRC))
- {
- Uart_ClrStatus(M0P_UART1, UartRC);
- u8RxData = Uart_ReceiveData(M0P_UART1);
- u8RxCnt++;
- }
- if(Uart_GetStatus(M0P_UART1, UartTC))
- {
- Uart_ClrStatus(M0P_UART1, UartTC);
- u8TxCnt++;
- }
- }
- void App_PortInit(void)
- {
- stc_gpio_cfg_t stcGpioCfg;
- DDL_ZERO_STRUCT(stcGpioCfg);
- Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
- stcGpioCfg.enDir = GpioDirOut;
- Gpio_Init(GpioPortA, GpioPin2, &stcGpioCfg);
- Gpio_SetAFMode(GpioPortA, GpioPin2, GpioAf1);
-
- stcGpioCfg.enDir = GpioDirIn;
- Gpio_Init(GpioPortA, GpioPin3, &stcGpioCfg);
- Gpio_SetAFMode(GpioPortA, GpioPin3, GpioAf1);
- }
- void App_UartCfg(void)
- {
- stc_uart_cfg_t stcCfg;
- DDL_ZERO_STRUCT(stcCfg);
- Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1, TRUE);
- stcCfg.enRunMode = UartMskMode3;
- stcCfg.enStopBit = UartMsk1bit;
- stcCfg.enMmdorCk = UartMskEven;
- stcCfg.stcBaud.u32Baud = 9600;
- stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;
- stcCfg.stcBaud.u32Pclk = Sysctrl_GetPClkFreq();
- Uart_Init(M0P_UART1, &stcCfg);
- 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);
-
- }
复制代码 hc32l130数据手册上写的是,时钟在8MHz下,波特率9600,模式三使用8分频,此时的时钟误差在0.16%,
|
|