This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

using printf for serial & LCD together

in C51, the default printf() is used for serial comm., depends on putchar.c
now, i'm trying to modify it(putchar.c) for my 8-bit LCD.

the question is, can putchar.c or printf() be used on both, serial comm. and 8-bit LCD, at same time (at the same main program)?

nb: i want to display floating point, that's why i'm trying to use printf()

  • 1) Yes, you can write code that sends the data to multiple destinations. It's just up to you to implement the back-end code that takes care of the resulting characters and distributes them where you want them.

    2) Alas, I don't think Keil have fcvt() or ecvt() which would have been alternatives to printf()/sprintf(). But it is often possible to work with fixed-point arithmetic instead of floating point with very good results.

  • "in C51, the default printf() is used for serial comm"

    No: you need to be clear that printf() is entirely unaware of what physical interface is used to send the output that it generates!

    It is entirely a matter of the putchar() implementation.

    "the question is, can putchar() be used on both, serial comm. and 8-bit LCD, at same time"

    Why would that not be possible??

    char putchar( char c)
    {
       put_to_serial( c );
       put_to_lcd( c );
       put_to_whatever_else_you_like( c );
       return c;
    }
    

  • Hello,

    This method sends data to the two peripherals simultaneously.
    What if we want to send different data to LCD, than to the UART ?

    Best regards.
    Wilu

  • You can implement own printf() functions by using sprintf() to a temporary buffer and have:

    int lcd_printf(const char fmt,...) {
        ...
    }
    
    int com0_printf(const char *fmt,...) {
        ...
    }
    
    int com1_printf(const char *fmt,...) {
    }
    

    or use fprintf() to direct the output:

    fprintf(lcd,"Hello world!\n");
    fprintf(com0,"This is com0.\n");
    fprintf(com1,"This is com1.\n");
    

  • "You would have to create your own..."

    Which isn't very hard to do. Either by use of our friend "sprintf()" or by setting a global variable to inform where the data is expected to be sent and have a putchar() that looks at that global variable. Just as long as the fprintf() implementation resets that global variable after the call so direct use of printf() doesn't get the output rerouted.

    A large part of the C runtime library is standard C code, and potentially using other C RTL functions. And the parts that isn't standard C is normally assembler for extra speed or size gains - not because of limitations the C language.

    In the end, the user doesn't need any presupplied file system implementation to be able to play with fprintf(), fputs(), fgets(), fputchar(), ...