华大单片机HC32L136实现printf

一、华大的库
hc32l13x_ddl_Rev1.9.2 Lite
1.打开 hc32l13x_ddl_Rev1.9.2 Lite\driver\src\ddl.c 约208行

#if defined (__CC_ARM)          //KEIL
#pragma import(__use_no_semihosting)
void _sys_exit(int x)
{
    x = x;
}
struct __FILE
{
    int handle;
    /* Whatever you require here. If the only file you are using is */
    /* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef?d in stdio.h. */
FILE __stdout;

#endif

#ifdef __DEBUG
/**
 ******************************************************************************
 ** \brief  Re-target putchar function
 ******************************************************************************/
int fputc(int ch, FILE *f)
{

    if (((uint8_t)ch) == '\n')
    {
        Debug_Output('\r');
    }
    Debug_Output(ch);

    return ch;
}
#endif

修改fputc函数

int fputc(int ch, FILE *f)
{
	Uart_SendDataPoll(M0P_UART0,ch); 	
    return ch;
}

二、初始化串口

只是要串口0就行,和上面的fputc要对应.由于hc32l136k8有多pin都可以配置为串口0,所以用宏来定义,决定用哪个pin。
新建usart.h

//#define DEBUG_UART0_B6B7          //测试成功
#define DEBUG_UART0_B8B9            //测试成功
//#define DEBUG_UART0_A9A10         //测试成功

三个pin随便用一个

usart.c

//串口引脚配置
void UART0_PortInit(void)
{
  	stc_gpio_cfg_t stcGpioCfg;
    DDL_ZERO_STRUCT(stcGpioCfg);
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //使能GPIO模块时钟

    ///
    stcGpioCfg.enDir = GpioDirOut;	
#if defined(DEBUG_UART0_B6B7)			
    Gpio_Init(GpioPortB, GpioPin6, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortB, GpioPin6, GpioAf2);          //配置Pb6 端口为 010 ---- UART0_TXD UART0模块TXD信号	
#elif defined(DEBUG_UART0_B8B9)
	Gpio_Init(GpioPortB, GpioPin8, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortB, GpioPin8, GpioAf7);          //配置Pb8 端口为 ---- UART0_TXD UART0模块TXD信号	
#elif defined(DEBUG_UART0_A9A10)
	Gpio_Init(GpioPortA, GpioPin9, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortA, GpioPin9, GpioAf1);   
#endif
	
    ///
    stcGpioCfg.enDir = GpioDirIn;
#if defined(DEBUG_UART0_B6B7)		
    Gpio_Init(GpioPortB, GpioPin7, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortB, GpioPin7, GpioAf2);          //配置Pb7 端口010 ---- UART0_RXD UART0模块RXD信号
#elif defined(DEBUG_UART0_B8B9)
	Gpio_Init(GpioPortB, GpioPin9, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortB, GpioPin9, GpioAf7);          //配置Pb9 端口 ---- UART0_RXD UART0模块RXD信号			
#elif defined(DEBUG_UART0_A9A10)
	Gpio_Init(GpioPortA, GpioPin10, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortA, GpioPin10, GpioAf1); 
#endif	
		
}

三、测试

int32_t main(void)
{
	UART0_PortInit();
	Uart0Cfg(38400);
	printf("HC32L136K8TA-MINI \r\n");
	while(1)
	{ 
		printf("+++......\r\n");
		delay1ms(1000);
	}
}

https://shop170824944.taobao.com/
https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w4004-23044783907.23.56114554bzW4t2&id=650977624658
华大单片机HC32L136实现printf_第1张图片

你可能感兴趣的:(华大)