华大MCU HC32L13X芯片如何使用printf输出
华大单片机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)
{
;
}
}
赞一个! M0P_UART0->SCNT = 52; //波特率9600
此时UART时钟多少? 神奇喔,这样就可以了,例程里为什么要把相关部分注释掉?!
页:
[1]