独闷闷网

标题: 如何在stm32单片机上使用printf函数 [打印本页]

作者: ligang    时间: 2014-1-31 12:37
标题: 如何在stm32单片机上使用printf函数
在KEIL下使用printf函数,以STM32为例
    在uart.c中添加如下代码
//加入以下代码,支持printf函数,而不需要选择use MicroLIB          
#if 1
#pragma import(__use_no_semihosting)
            
//标准库需要的支持函数                 
struct __FILE
{
        int handle;


};


FILE __stdout;   
   
//定义_sys_exit()以避免使用半主机模式   
_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 */
       
            while (!(USART3->SR & USART_FLAG_TXE));
    USART_SendData(USART3, (uint8_t) ch);




    return ch;
}
#endif
    这样,只要在需要用printf的文件里#include <stdio.h>就可以了,printf会自已的调用fputc函数来实现串口数据的输出





欢迎光临 独闷闷网 (http://www.dumenmen.com/) Powered by Discuz! X3.2