1. 首先 配置 keil 项目 option 

选择 Code Generation ARM Compiler :Use default compiler version 5

2. 取消 Use MicroLIB 选项

3. 在uart 代码中 增加


/* 告知连接器不从C库链接使用半主机的函数 */
#pragma import(__use_no_semihosting)

/* 定义 _sys_exit() 以避免使用半主机模式 */
void _sys_exit(int x)
{
    x = x;
}

/* 标准库需要的支持类型 */
struct __FILE
{
    int handle;
};

FILE __stdout;

int fputc(int ch, FILE *f)
{
    (void)f;

	Uart_SendDataPoll(M0P_UART3,ch);
	
	return ch;
}

 4. 提供一下完整的uart 配置文件

/* 告知连接器不从C库链接使用半主机的函数 */
#pragma import(__use_no_semihosting)

/* 定义 _sys_exit() 以避免使用半主机模式 */
void _sys_exit(int x)
{
    x = x;
}

/* 标准库需要的支持类型 */
struct __FILE
{
    int handle;
};

FILE __stdout;

int fputc(int ch, FILE *f)
{
    (void)f;

	Uart_SendDataPoll(M0P_UART3,ch);
	
	return ch;
}

void uart_init(){

	//串口引脚配置
    App_PortInit();
    
    //串口配置
    App_UartCfg();
}

//UART1中断函数
void Uart3_IRQHandler(void)
{
    if(Uart_GetStatus(M0P_UART3, UartRC))         //UART1数据接收
    {
        Uart_ClrStatus(M0P_UART3, UartRC);        //清中断状态位
       
    }

}

//串口引脚配置
void App_PortInit(void)
{
    stc_gpio_cfg_t stcGpioCfg;
    
    DDL_ZERO_STRUCT(stcGpioCfg);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //使能GPIO模块时钟
    
    ///<TX
    stcGpioCfg.enDir = GpioDirOut;
    Gpio_Init(GpioPortC, GpioPin7, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortC, GpioPin7, GpioAf6);          //配置PA02 端口为URART1_TX
    
    ///<RX
    stcGpioCfg.enDir = GpioDirIn;
    Gpio_Init(GpioPortC, GpioPin6, &stcGpioCfg);
    Gpio_SetAfMode(GpioPortC, GpioPin6, GpioAf6);          //配置PA03 端口为URART1_RX
}

//串口配置
void App_UartCfg(void)
{
    stc_uart_cfg_t    stcCfg;

    DDL_ZERO_STRUCT(stcCfg);
    
    ///< 开启外设时钟
    Sysctrl_SetPeripheralGate(SysctrlPeripheralUart3,TRUE);///<使能uart1模块时钟
    
    ///<UART Init
    stcCfg.enRunMode        = UartMskMode3;          ///<模式3
    stcCfg.enStopBit        = UartMsk1bit;           ///<1bit停止位
    stcCfg.enMmdorCk        = UartMskDataOrAddr;           ///<偶检验
    stcCfg.stcBaud.u32Baud  = 9600;                  ///<波特率9600
    stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;       ///<通道采样分频配置
    stcCfg.stcBaud.u32Pclk  = Sysctrl_GetPClkFreq(); ///<获得外设时钟(PCLK)频率值
    Uart_Init(M0P_UART3, &stcCfg);                   ///<串口初始化
    
    ///<UART中断使能
    Uart_ClrStatus(M0P_UART3,UartRC);                ///<清接收请求
    Uart_ClrStatus(M0P_UART3,UartTC);                ///<清接收请求
	
//	Uart_DisableIrq(M0P_UART1,UartRxIrq);             ///<使能串口接收中断
//  Uart_DisableIrq(M0P_UART1,UartTxIrq);             ///<使能串口接收中断
	
//    Uart_EnableIrq(M0P_UART3,UartRxIrq);             ///<使能串口接收中断
//    Uart_EnableIrq(M0P_UART3,UartTxIrq);             ///<使能串口接收中断    
//    EnableNvic(UART1_3_IRQn, IrqLevel3, TRUE);       ///<系统中断使能

}

以上步骤就可以完成 串口的格式化打印输出

如果还需要串口接收,那么就把 RX 的 中断开启,使能中断 

测试效果:

int i = 0 ;
while(1){

	printf("i:%d \r\n",i++);
	
	delay1ms(1000);
		
		
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐