C库函数重定向

来源:互联网 发布:pythonxy与python 编辑:程序博客网 时间:2024/06/04 17:46

#include <stdio.h>   头文件 


#ifdef __GNUC__ 
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf 
    set to 'Yes') calls __io_putchar() */ 
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) 
#else 
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) 
#endif /* __GNUC__ */ 

/** 
   * @brief   Retargets the C library printf function to the USART. 
   * @param   None    重定向C库printf函数 
   * @retval None 
   */ 
PUTCHAR_PROTOTYPE 

   /* Place your implementation of fputc here */ 
   /* e.g. write a character to the USART */ 
   USART_SendData(EVAL_COM1, (uint8_t) ch); 

   /* Loop until the end of transmission */ 
   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET) 
   {} 

   return ch; 


定义了以上的 

然后就可以调用   printf("\n\rPlease enter valid number between 0 and 9");

===============================================================

C库函数重定向
用户能定义自己的C语言库函数,连接器在连接时自动使用这些新的功能函数。这个过程叫做重定向C语言库函数,如下图所示。
举例来说,用户有一个I/O设备(如UART)。本来库函数fputc()是把字符输出到调试器控制窗口中去的,但用户把输出设备改成了UART端口,这样一来,所有基于fputc()函数的printf()系列函数输出都被重定向到UART端口上去了。
下面是实现fputc()重定向的一个例子:
externvoidsendchar(char*ch);
intfputc(intch,FILE*f)
{/*e.g.writeacharactertoanUART*/
chartempch=ch;
sendchar(&tempch);
returnch;

这个例子简单地将输入字符重新定向到另一个函数sendchar(),sendchar()假定是个另外定义的串口输出函数。在这里,fputc()就似乎目标硬件和标准C库函数之间的一个抽象层。

  C库函数重定向 - liuyunqian@yeah - 嵌入式学习

0 0
原创粉丝点击