方式一
在keil中勾选Use MicroLIB
重写fputc函数
#include <stdio.h>
// 根据具体芯片型号编写 串口发送一个字节数据的函数
extern void UART_SendByte(unsigned char Byte);
/******************************************************************************************/
int fputc(int ch, FILE *f)
{
UART_SendByte(ch);
return ch;
}
方式二
重写_ttywrch,_sys_exit,_sys_command_string和fputc
#include <stdio.h>
// 根据具体芯片型号编写 串口发送一个字节数据的函数
extern void UART_SendByte(unsigned char Byte);
/******************************************************************************************/
/* 加入以下代码, 支持printf函数, 而不需要选择use MicroLIB */
#if 1
#if (__ARMCC_VERSION >= 6010050) /* 使用AC6编译器时 */
__asm(".global __use_no_semihosting\n\t"); /* 声明不使用半主机模式 */
__asm(".global __ARM_use_no_argv \n\t"); /* AC6下需要声明main函数为无参数格式,否则部分例程可能出现半主机模式 */
#else
/* 使用AC5编译器时, 要在这里定义__FILE 和 不使用半主机模式 */
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
#endif
/* 不使用半主机模式,至少需要重定义_ttywrch\_sys_exit\_sys_command_string函数,以同时兼容AC6和AC5模式 */
int _ttywrch(int ch)
{
ch = ch;
return ch;
}
/* 定义_sys_exit()以避免使用半主机模式 */
void _sys_exit(int x)
{
x = x;
}
char *_sys_command_string(char *cmd, int len)
{
return NULL;
}
/* FILE 在 stdio.h里面定义. */
FILE __stdout;
/* 重定义fputc函数, printf函数最终会通过调用fputc输出字符串到串口 */
int fputc(int ch, FILE *f)
{
// while ((USART1->SR & 0X40) == 0); /* 等待上一个字符发送完成 */
// USART1->DR = (uint8_t)ch; /* 将要发送的字符 ch 写入到DR寄存器 */
UART_SendByte(ch);
return ch;
}
#endif
/***********************************************END*******************************************/
参考资料: https://www.bilibili.com/video/BV1bv4y1R7dp/?p=44&share_source=copy_web&vd_source=786f325062a2f8ee24cc2ef9bf4a8bc0&t=220