keil 下如何使用printf

本文介绍三种实现printf的方法:使用Keil的微库,不使用微库但禁用半主机模式,以及完全自定义实现。同时提供了代码示例。
不管怎么样,printf 都要实现

你可以自己实现,也可以让keil实现

keil 已经实现的有两种, 上面的链接中就展示了这两种,你需要配置才能用

如果要自己实现,那就要 实现 printf 函数 的一切
1. keil Use MicroLIB
使用微库(平台式keil-MDK)

1、点击“魔术棒” Target标签下有个Use MicroLIB---勾选。

2、包含头文件:#include "stdio.h"

3、printf重定向,修改fputc()函数的内容~

int fputc(int ch, FILE *f)
{
	USART_SendData(DEBUG_USART, (unsigned char) ch);
	
	while (!(DEBUG_USART->SR & USART_FLAG_TXE));
	
	return (ch);
}

这里的:USART_SendData(DEBUG_USART, (unsigned char) ch);

while (!(DEBUG_USART->SR & USART_FLAG_TXE));

就是往串口发送一个字节的代码,修改相应的串口,初始化。

这样就能使用printf了~可以一试
2. keil Not Use MicroLIB
方法2--不使用微库(那么就要强调不使用半主机(no semihosting)模式)

1、在调用printf的文件中#include "stdio.h"

2、重写fputc,但需要先加点东西:
#include "stdio.h"
#pragma import(__use_no_semihosting)
struct __FILE
{  
	int handle;
};
FILE __stdout;

FILE __stdin;

//int _sys_exit(int x)
void _sys_exit(int x)
{
	x = x;
}

int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(Open_USART, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(Open_USART, USART_FLAG_TC) == RESET){}

  return ch;
}

这样,就完成了,同样可以使用printf语句了~
3. 自己实现 printf
可以移植一个printf . 
https://github.com/lisider/learn_os/tree/master/print_format/
https://github.com/lisider/learn_os/blob/master/print_format/print.c
其他

printf("SUD File: %s, Line: %05d: %-20s\n",__FILE__, __LINE__,__func__);
/*myprintf.h*/
#ifndef __MY_PRINTF_H_
#define __MY_PRINTF_H_
    
#define __DEBUG_SU
#define __SIMPLEDBG

#ifdef __DEBUG_SU                                     
    #ifdef __SIMPLEDBG
        //#define DEBUG_SU(format,...) printf(""format"\n",##__VA_ARGS__) 
        #define DEBUG_SU(format,...) printf( format "\n",##__VA_ARGS__)   
    #else // __SIMPLEDBG
         //#define DEBUG_SU(format,...) printf("File: %s, Line: %05d: %-30s"format"\n",__FILE__, __LINE__,"thread_uart", ##__VA_ARGS__)              
        #define DEBUG_SU(format, ...) printf("File: %s, Line: %05d " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
    #endif // __SIMPLEDBG
#else  // __DEBUG_SU
    #define DEBUG_SU(format,...)  
#endif // __DEBUG_SU

#endif // __MY_PRINTF_H_
Keil环境中自行编写printf函数,通常需要实现输出重定向,因为printf默认输出到显示器,若要输出到串口等其他设备,就需要重新实现相关函数。 ### 步骤及代码示例 1. **包含必要的头文件**:需要包含`<stdio.h>`头文件,因为标准的printf函数相关的定义在这个头文件中。 ```c #include <stdio.h> ``` 2. **实现fputc函数**:这是关键步骤,printf函数在输出字符时会调用fputc函数,因此要重新实现fputc函数,将输出重定向到目标设备,如串口。以下是一个简单的串口输出重定向示例: ```c #include <stdio.h> // 假设这里是串口发送数据的函数 void usart_data_transmit(int uartNo, uint8_t ch); // 假设这里是获取串口发送缓冲区空标志的函数 int usart_flag_get(int uartNo, int flag); // 全局的串口硬件信息结构体 typedef struct { int uartNo; } UartHwInfo; UartHwInfo g_uartHwInfo; // 重定向fputc函数 int fputc(int ch, FILE *f) { usart_data_transmit(g_uartHwInfo.uartNo, (uint8_t)ch); while(0 == usart_flag_get(g_uartHwInfo.uartNo, 0x20)); // 等待发送缓冲区为空 return ch; } ``` 3. **使用自定义的printf函数**:在完成上述步骤后,就可以像使用标准的printf函数一样使用它了。 ```c int main() { g_uartHwInfo.uartNo = 0; // 假设使用串口0 printf("Hello, World!\n"); return 0; } ``` ### 代码解释 - `#include <stdio.h>`:包含标准输入输出头文件,提供了printf等函数的声明。 - `fputc`函数:该函数接收一个字符`ch`和一个文件指针`f`,将字符`ch`通过串口发送出去,并等待发送缓冲区为空,最后返回该字符。 - `main`函数:初始化串口信息,然后使用printf函数输出字符串。 ### 注意事项 - 示例中的`usart_data_transmit`和`usart_flag_get`函数需要根据具体的硬件平台和串口驱动进行实现。 - 不同的硬件平台和串口配置可能会有所不同,需要根据实际情况调整代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值