[技术相关] keil 下如何使用printf

[复制链接]
 楼主| pixhw 发表于 2023-8-29 21:50 | 显示全部楼层 |阅读模式
1. keil Use MicroLIB
  1. 使用微库(平台式keil-MDK)

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

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

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

  5. int fputc(int ch, FILE *f)
  6. {
  7.         USART_SendData(DEBUG_USART, (unsigned char) ch);
  8.        
  9.         while (!(DEBUG_USART->SR & USART_FLAG_TXE));
  10.        
  11.         return (ch);
  12. }

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

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

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

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

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

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

  11. FILE __stdin;

  12. //int _sys_exit(int x)
  13. void _sys_exit(int x)
  14. {
  15.         x = x;
  16. }

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

  24.   return ch;
  25. }

  26. 这样,就完成了,同样可以使用printf语句了~


您需要登录后才可以回帖 登录 | 注册

本版积分规则

50

主题

4917

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部