打印
[技术相关]

keil 下如何使用printf

[复制链接]
142|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
pixhw|  楼主 | 2023-8-29 21:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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了~可以一试
keil 非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语句了~


使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

27

主题

4397

帖子

1

粉丝