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()

Parents Reply Children
  • 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(), ...