passing variable argument to printf()

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi guys

I have multiple UART in my pic18f65k40, and I want to wrap the printf() in another function so I can print to different UART. How do you guy usually do that? I am using XC8 compiler.

Thanks for your help!

Code:
// XC8 function to determine the destination of the standard output stream
void putch(char data){
    switch(output_uart){
        case DEBUG:
          UART1Put(data);
          break;
        case COM:
          UART2Put(data);
          break;
        case LCD:
          UART3Put(data);
          break;
    }
}

// print to debug output
int debut_printf(const char *str, ...){
  // set flag to direct output to debug uart
  output_uart = DEBUG;
   return printf(str, /* how do I pass ... to here */);
}

// print to LCD
int lcd_printf(const char *str, ...){
  // set flag to direct output to LCD uart
  output_uart = LCD;
  ...
}

// print to communication module.
int com_printf(const char *str, ...){
  // set flag to direct output to com. uart
  output_uart = COM;
  ...
}
 
Last edited:

Thread Starter

bug13

Joined Feb 13, 2012
2,002
I have tried this, but it doesn't work.

Compiler warning:
func.c:18: warning: (1404) unsupported: vprintf() is not supported by this compiler
Code:
int debug_printf(const char *str, ...){
    int result;
    va_list arg;
   
    va_start(arg, str);
    result = vprintf(str, arg);
    va_end(arg);
    return result;
}
 

WBahn

Joined Mar 31, 2012
29,979
While a variadic function can take a variable number of arguments, the number must be known at compile time for each call so that the compiler can set up, and later clean up, the call stack.
 

Picbuster

Joined Dec 2, 2013
1,047
Hi guys

I have multiple UART in my pic18f65k40, and I want to wrap the printf() in another function so I can print to different UART. How do you guy usually do that? I am using XC8 compiler.

Thanks for your help!

Code:
// XC8 function to determine the destination of the standard output stream
void putch(char data){
    switch(output_uart){
        case DEBUG:
          UART1Put(data);
          break;
        case COM:
          UART2Put(data);
          break;
        case LCD:
          UART3Put(data);
          break;
    }
}

// print to debug output
int debut_printf(const char *str, ...){
  // set flag to direct output to debug uart
  output_uart = DEBUG;
   return printf(str, /* how do I pass ... to here */);
}

// print to LCD
int lcd_printf(const char *str, ...){
  // set flag to direct output to LCD uart
  output_uart = LCD;
  ...
}

// print to communication module.
int com_printf(const char *str, ...){
  // set flag to direct output to com. uart
  output_uart = COM;
  ...
}
You do have more then one uart port.

one way of doing it is to modify the putchar() which is used by printf().
in this case when Portnum=0 then data goes to port 1 for all other values to port2.
like
void putch(unsigned char byte)
{
if ( Port_Num)

{
while(!TX1IF)
continue;
TXREG1 = byte;
}
else
{
while(!TX2IF)
continue;
TXREG2 = byte;
}

You have to modify it for your application use the switch statement to select different ports.

disadvantage you can not modify putch hence it is used by printf()


But it works

Picbuster
 
Top