通过库函数 printf 输出

  1. 勾选微库(MicroLIB)
  2. 添加fputc函数
/* 覆盖了内置的fputc函数,printf函数会调用fputc输出 */
int fputc(int ch, FILE* f) {
    while ((USART1->ISR & 0x40) == 0);
    USART1->TDR = (uint8_t) ch;

    return ch;
}

自定义 uart2_printf 函数输出

#include <stdarg.h>
#include <stdio.h>

uint8_t uart2_tx_buf[200];
void uart2_printf(const char *format, ...) {
    uint16_t len;
    va_list args;

    va_start(args, format);
    len = vsnprintf((char*)uart2_tx_buf, sizeof(uart2_tx_buf)+1, (char*)format, args); // 为什么要 +1 ?
    va_end(args);
    HAL_UART_Transmit(&huart2, UartTxBuf, len, HAL_MAX_DELAY);
}

标签: none

添加新评论