×
单片机 > 单片机程序设计 > 详情

STM32中printf与scanf的重定向问题

发布时间:2020-08-31 发布时间:
|

关于STM32中 printf 与 scanf 的重定向问题在此我仅对不使用 "USE MircoLIB" 的情况做整理(针对Keil RVMDK开发环境)。


① :首先需要在 usart.h 中包含 “stdio.h” 头文件

② :在 usart.c 中,加入如下代码块,以此避免使用半主机模式,并重定向 printf 和scanf 函数;


  1. #if 1  

  2. #pragma import (__use_no_semihosting_swi)  

  3. /*标准库需要的支持函数,use_no_semihosting_swi以避免使用半主机模式*/  

  4. struct __FILE  

  5. {  

  6.     int handle;  

  7. };  

  8.   

  9. FILE __stdout;  

  10. FILE __stdin;  

  11. /*重定向Printf函数*/  

  12. int fputc(int ch,FILE *f)  

  13. {  

  14.     return (SendChar(ch));  

  15. }  

  16. /*重定向Scanf函数*/  

  17. int fgetc(FILE *f)  

  18. {  

  19.     return (SendChar(GetKey()));  

  20.     /*调用scanf()在串口中输入数据时,必须以空格结束,否则无法完成发送*/  

  21. }  

  22.   

  23. void _ttywrch(int ch)  

  24. {  

  25.     SendChar(ch);  

  26. }  

  27.   

  28. int _ferror(FILE *f) {  

  29.   /* Your implementation of ferror */  

  30.   return EOF;  

  31. }  

  32.   

  33. //定义_sys_exit()以避免使用半主机模式  

  34. void _sys_exit(int return_code){  

  35.     //x = x;  

  36. label:goto label;  

  37. }  

  38.   

  39. #endif  


③ :在 usart.c 中添加SendChar()与GetKey()函数


  1. int SendChar(int ch)  

  2. {  

  3.     while(!(USART1->SR & USART_FLAG_TXE));  

  4.     USART1->DR = (ch & 0x1FF);  

  5.   

  6.     return ch;  

  7. }  

  8.   

  9. int GetKey(void)  

  10. {  

  11.     while(!(USART1->SR & USART_FLAG_RXNE));  

  12.     return ((int)(USART1->DR & 0X1FF));  

  13. }  



完成以上三步,即可实现printf()函数与scanf()的串口重定向,将标准输入输出流的来源或去向改为串口。


关于第二步所使用的避免使用半主机模式的代码,其实Kei已经为我们写好了一个Retarget.c文件,在Keil/ARM/Startup目录下.

另外本文针对的开发环境为Keil RVMDK,本人在Emblocks开源开发工具中实验时,本方法是无法实现printf和Scanf的重定向的,以及在开源工具下如何"Use microLIB"的问题都有待进一步探讨。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
单片机数字温度计的课程设计