这是我看之前公司的旧代码摘录下来的, 感觉写的还算可以吧,
void printfsend(UART_TypeDef UARTx, uint8_t *buf, int len)
{
uint8_t printbuf[256];
for (int i = 0; i < len; i++)
{
printbuf[i] = buf[i];
}
#ifdef ENABLE_PERIAL_TEST
if (uart_printf_switch_gloab)
{
UART_SendBuf(UART1, printbuf, len);
}
else
{
UART_SendBuf(UART0, printbuf, len);
}
#else
UART_SendBuf(UARTx, printbuf, len);
#endif
}
void MyPrintf(char *format, ...)
{
uint32_t ulIdx, ulValue, ulPos, ulCount, ulBase, ulNeg;
int8_t *pcStr, pcBuf[16], cFill;
char HexFormat;
va_list vaArgP;
va_start(vaArgP, format);
while (*format)
{
// Find the first non-% character, or the end of the string.
for (ulIdx = 0; (format[ulIdx] != '%') && (format[ulIdx] != '\0'); ulIdx++)
{}
// Write this portion of the string.
if (ulIdx > 0)
{
// printfsend(UART0,format, ulIdx);
printfsend(UART0, (uint8_t *)format, ulIdx);
}
format += ulIdx;
if (*format == '%')
{
format++;
// Set the digit count to zero, and the fill character to space
// (i.e. to the defaults).
ulCount = 0;
cFill = ' ';
again:
switch (*format++)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
if ((format[-1] == '0') && (ulCount == 0))
{
cFill = '0';
}
ulCount *= 10;
ulCount += format[-1] - '0';
goto again;
}
case 'c':
{
ulValue = va_arg(vaArgP, unsigned long);
// printfsend(UART0,(char *)&ulValue, 1);
printfsend(UART0, (uint8_t *)&ulValue, 1);
break;
}
case 'd':
{
ulValue = va_arg(vaArgP, unsigned long);
ulPos = 0;
if ((long)ulValue < 0)
{
ulValue = -(long)ulValue;
ulNeg = 1;
}
else
{
ulNeg = 0;
}
ulBase = 10;
goto convert;
}
case 's':
{
// pcStr = va_arg(vaArgP, char *);
pcStr = (int8_t *)va_arg(vaArgP, char *);
for (ulIdx = 0; pcStr[ulIdx] != '\0'; ulIdx++)
{}
// printfsend(UART0,pcStr, ulIdx);
printfsend(UART0, (uint8_t *)pcStr, ulIdx);
if (ulCount > ulIdx)
{
ulCount -= ulIdx;
while (ulCount--)
{
// printfsend(UART0, " ", 1);
printfsend(UART0, (uint8_t *)" ", 1);
}
}
break;
}
case 'u':
{
ulValue = va_arg(vaArgP, unsigned long);
ulPos = 0;
ulBase = 10;
ulNeg = 0;
goto convert;
}
case 'X':
{
ulValue = va_arg(vaArgP, unsigned long);
ulPos = 0;
ulBase = 16;
ulNeg = 0;
HexFormat = 'X';
goto convert;
}
case 'x':
case 'p':
{
ulValue = va_arg(vaArgP, unsigned long);
ulPos = 0;
ulBase = 16;
ulNeg = 0;
HexFormat = 'x';
convert:
for (ulIdx = 1;
(((ulIdx * ulBase) <= ulValue) &&
(((ulIdx * ulBase) / ulBase) == ulIdx));
ulIdx *= ulBase, ulCount--)
{
}
if (ulNeg)
{
ulCount--;
}
if (ulNeg && (cFill == '0'))
{
pcBuf[ulPos++] = '-';
ulNeg = 0;
}
if ((ulCount > 1) && (ulCount < 16))
{
for (ulCount--; ulCount; ulCount--)
{
pcBuf[ulPos++] = cFill;
}
}
if (ulNeg)
{
pcBuf[ulPos++] = '-';
}
for (; ulIdx; ulIdx /= ulBase)
{
if (HexFormat == 'x')
pcBuf[ulPos++] = g_pcHex1[(ulValue / ulIdx) % ulBase];//x
else
pcBuf[ulPos++] = g_pcHex2[(ulValue / ulIdx) % ulBase];//X
}
// printfsend(UART0, pcBuf, ulPos);
printfsend(UART0, (uint8_t *)pcBuf, ulPos);
break;
}
case '%':
{
// printfsend(UART0, format - 1, 1);
printfsend(UART0, (uint8_t *)format - 1, 1);
break;
}
default:
{
// printfsend(UART0, "ERROR", 5);
printfsend(UART0, (uint8_t *)"ERROR", 5);
break;
}
}//switch
}//if
}//while
va_end(vaArgP);
}