[misc]如何在嵌入式平台使用printf功能

转自:http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html

摘要:

    当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上,来判断我们的程序是否按预期的运行,printf函数很好的做到了这一点,它能直接以字符的方式输出变量名和变量的值,这样使输出的信息很直观;但printf函数在使用时,不仅仅要初始化串口,还需要其它的一些设置或者要调用其它的一些函数,否则printf函数将不能按我们想要的方式执行。

    由于不同的编译器studio函数不一样,所以使用的方法也不一样,这需要大家去看编译器的help,这里我以STM32、LPC24和AVR整理了几个串口打印程序,供需要的朋友参考。

简介:

   1、在KEIL下使用printf函数,以STM32为例

    在uart.c中添加如下代码

/*******************************************************************************
    函数名:fputc
    输  入:
    输  出:
    功能说明:
    重定义putc函数,这样可以使用printf函数从串口1打印输出
*/
int fputc(int ch, FILE *f)
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART */
    USART_SendData(USART1, (uint8_t) ch);

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

    return ch;
}

/*******************************************************************************
    函数名:fputc
    输  入:
    输  出:
    功能说明:
    重定义getc函数,这样可以使用scanff函数从串口1输入数据
*/
int fgetc(FILE *f)
{
    /* 等待串口1输入数据 */
    while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
    {}

    return (int)USART_ReceiveData(USART1);
}

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

2、添加Retarget.c,实现在KEIL下使用printf函数,以LPC2478为例

    首先在Keil安装目录下面ARM/Startup/Retarget.c找到Retarget.c文件将其复制到你的工程文件夹下面;并将其加入到工程中

    在uart.c中添加如下代码

// Implementation of sendchar (also used by printf function to output data) 
   int sendchar (int ch) {                 // Write character to Serial Port 
   while (!(U0LSR & 0x20)); 
  return (U0THR = ch); 
} 
int getkey (void)  {                     // Read character from Serial Port 
  while (!(U0LSR & 0x01)); 
  return (U0RBR); 
}

    这样,只要在需要用printf的文件里#include <stdio.h>就可以了,printf会通过Retarget中的fputc函数调用sendchar来实现串口数据的输出。

   事实上,和第一种的方式是一样的。

3、自定义printf函数,以AVR为例

   前面介绍的是在KEIL编译器上使用printf函数,但不是所有的编译器平台都能适用,因此有时候我们需要自定义printf函数,下面以AVR在GCC下为例

   在usart.c中添加如下代码

#include    <stdio.h>
#include    <stdarg.h>

/*********************************************************/
//向串口usart0发送一个字节函数   
 void Uart0_putchar( unsigned char sdbyte)  
 {
     UDR0=sdbyte;
     while(!(UCSR0A&0x40));
     UCSR0A|=0x40;
 }


//像串口发送一串数据,...表示函数实参不定
void Uart0_printf(char *str,...)
{
 char  buf[128];
 unsigned char i = 0;
 va_list ptr;
 va_start(ptr,str);
 vsprintf(buf,str,ptr);
 while(buf[i])
 {
     Uart0_putchar(buf[i]);
     i++;
 }
}

结语:

   有了printf格式化输出函数,调试起来就方便多了。

原文地址:https://www.cnblogs.com/aaronLinux/p/6684211.html