详情页
首页>资料大全>解决方案
再来一版简易的printf函数实现
   发表于   2015-04-21

以前发过两版简易的串口printf函数实现,近搞了一段时间Linux的库文件,回过头又有不同的理解。

这一版函数基于MSP430F169,%d %x %o %b的实现不再由自己编写函数,

而是调用MSP430-GCC的标准库函数: #include <stdlib.h> 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, ...);

标签: printf 实现

相关阅读

·   AN-DM34 耗尽型MOSFET在工业传感器... 2020-12-18

·   碳化硅器件在车载充电机OBC上的应... 2020-12-16

·   SiC器件助力电动汽车充电模块... 2020-12-16

·   5G应用的可回扫ESD 2020-11-26

·   用开关稳压器为高速ADC供电可节约... 2020-11-26

不同意
同意并继续