1. 程式人生 > >在stm32 微控制器程式中使用printf()進行串列埠除錯

在stm32 微控制器程式中使用printf()進行串列埠除錯

在微控制器使用printf()函式進行程式除錯很方便,官方給的串列埠輸出函式功能比較單一,又滿足不了要求。

修改方法如下:

1、在.c檔案中包含如下程式碼:

#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  USART_SendData(USART1, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}
  return ch;
}
2、然後包含標頭檔案。
#include "stdio.h"

如此,便可使用其進行格式化輸出。