长沙市天心区建设局网站,新河网站,吕梁推广型网站开发,广告传媒有限公司简介以前发过两版简易的串口printf函数实现#xff0c;最近搞了一段时间Linux的库文件#xff0c;回过头又有不同的理解。这一版函数基于MSP430F169#xff0c;%d %x %o %b的实现不再由自己编写函数#xff0c;而是调用MSP430-GCC的标准库函数#xff1a;#include char *itoa(…以前发过两版简易的串口printf函数实现最近搞了一段时间Linux的库文件回过头又有不同的理解。这一版函数基于MSP430F169%d %x %o %b的实现不再由自己编写函数而是调用MSP430-GCC的标准库函数#include char *itoa(int num, char *str, int radix);send_fun函数指针指向调用的UARTx的字节发送函数void uart_printf(send_fun fun, char *fmt, ...){char *pnt (char *)fmt sizeof(fmt);char *str, buf[9];int radix;while (*fmt ! \0) {if (*fmt ! %) {fun(*fmt);fmt 1;continue;}switch (*(fmt 1)) {case c:fun(*((int *)pnt));pnt sizeof(int);fmt 2;continue;case s:str (char *)*((int *)pnt);while (*str ! \0)fun(*str);pnt sizeof(int);fmt 2;continue;case d:radix 10;goto SEND_NUM;case x:radix 16;goto SEND_NUM;case o:radix 8;goto SEND_NUM;case b:radix 2;goto SEND_NUM;SEND_NUM:str itoa(*(int *)pnt, buf, radix);while (*str ! \0)fun(*str);pnt sizeof(int);fmt 2;continue;default:break;}}}实际上库stdio.h中也提供了printf的实现直接调用它们就可以了int __attribute__((format (printf, 2, 3))) uprintf(int (*func)(int c), const char *fmt, ...);