華大單片機HC32L13X系列MCU默認PA09為UART0 TXD功能引腳。我們可以在ddl.c中對其進行串口初始化設定,相關代碼如下:
void Debug_UartInit(void) //串口初始化
{
#ifdef __DEBUG
M0P_GPIO->PA09_SEL_f.SEL = 1;
M0P_GPIO->PADIR_f.PA09 = 0;
M0P_UART0->SCNT = 52; //波特率9600
M0P_UART0->SCON_f.OVER = 1;
M0P_UART0->SCON_f.SM = 1;
#endif
}
void Debug_Output(uint8_t u8Data) //發送一個位元組
{
M0P_UART0->SCON_f.REN = 0;
M0P_UART0->SBUF = u8Data;
while (TRUE != M0P_UART0->ISR_f.TC)
{
;
}
M0P_UART0->ICR_f.TCCF = 0;
}
int fputc(int ch, FILE *f)
{
if (((uint8_t)ch) == '\n')
{
Debug_Output('\r');
}
Debug_Output(ch);
return ch;
}
主程式中代碼如下:
#define DEBUG
#include "ddl.h"
#include "uart.h"
#include "gpio.h"
#include "sysctrl.h"
int main()
{
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);
Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0,TRUE);
#ifdef DEBUG
Debug_UartInit(); //除錯串口初始化
#endif
#ifdef DEBUG
printf("This is a UART Test!"); //輸出除錯內容
#endif
while(1)
{
;
}
}
程式編譯通過后,上電運行結果如下:
上面的代碼已經打包放在附件中,可以下載進行測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/131662.html
標籤:單片機/工控
上一篇:Twincat3
