1.自创:单片机printf()格式输出;
优势:适用于程序空间紧缺的单片机,如51内核单片机,占用资源空间远远小于系统的printf();
源文件获取地址:
点击获取源文件
2.函数原型
//****************************************************************************
// Method: DebugLog
// Access: public
// Returns: void
// Qualifier:
// Parameter: const char * famat
// Parameter: u32 Num
// Parameter: double fnum
// Remarks: DebugLog
//****************************************************************************
void DebugLog(const char* famat, u32 Num, double fnum);
参数1:famat,字符串格式
参数2:Num,需要打印的数值,支持10进制和16进制
参数3:fnum,浮点数
3.重写2个函数,串口重定义
需要重写2个串口重定义,来指定用到哪个串口来打印,以STM32F103为例:
//*********************自定义调试函数需要重写实现这2个函数,指定串口*******************************************************
void USART_WaitSedEmpty(void)
{
while ((USART1->SR & 0X40) == 0);
}
void USART_REG_DR_Send(u8 Byte)
{
USART1->DR = Byte;
}
4.编译-选择文件说明
Q_printfLib_C51.LIB
Q_printfLib_CM0.lib
Q_printfLib_CM0P.lib
Q_printfLib_CM3.lib
Q_printfLib_CM4.lib
5个文件后缀写出对应适用的内核,分别对应是C51,Cortex-M0/M0P/M3/M4,在使用时,用错文件,会编译出错或功能失效,错误类型如下:
.\Objects\Q_Project.axf: Error: L6366E: usart.o attributes are not compatible with the provided attributes .
Object usart.o contains Build Attributes that are incompatible with the provided attributes.
放心啦,换个内核适用的文件即可。如有问题,请留言
5.使用范例
调用形式:
printfLog(" printfLog:123=%d, 456.123456=%f",255,456.1234);
printfLog(" printfLog:&KeysOld=0x%x, 456.123456=%f",(u32)&KeysOld,456.123456);
输出:
感谢阅读,欢迎获取源文件点击获取源文件