Keil MDK 下利用 printf( )的串口编程

来源:互联网 发布:汇编语言和c语言哪个难 编辑:程序博客网 时间:2024/06/05 17:45

重定向是指用户可以自己重写C语言的库函数,当连接器检查到用户编写了与C语言库函数相同名字的函数时,优先采用用户编写的函数,这样就可以对库函数进行修改了。

若要printf( )函数工作,需要把printf( )函数重新定向到串口函数。

为了实现重定向 printf( ) 函数,需要重写fputc( )这个C标准库函数,因为printf()在C标准库函数中实质是一个宏,最终是调用了fputc( )函数。

fputc(int ch, FILE *f)函数可在main.c文件中编写,如下:

int fputc( int ch , FILE *f)

{

     USART_SendData( USART1 , (u8)ch ) ;

    while(USART_GetFlagStatus(USART1 , USART_FLAG_TC) = = RESET)

    {

    }

          return ch;

}

 

————————————————————————————————————————————————

使用printf()之前需要完成以下配置:

1、在main.c文件包含“stdio.h”

2、在main.c文件中加入 fputc( int ch , FILE *f) 函数代码

3、在工程属性对话框中选择“Target”选项卡,在“Code Generation” 区域中选中“Use MicroLIB” 选项

0 0
原创粉丝点击