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

customization of putchar function

I have made the new function putchar for redirect the output to LCD already. But by default, I also want to use this as output to serial port on the same program.
I do not understand how to use both output function in parallel or other way to switch between each other.
Please tell me if possible.
With my thanks.

Parents
  • "So I can not switch between output function (e.g. lcd_putchar and putchar)"

    That's ANSI 'C' for you!

    "which I can do in PIC-C(C compiler for PIC Microcontroller) very flexible as:
    - printf("......",.....);//default serial port
    - printf(lcd_putc,"......",.....);//redirect to LCD"


    Well, who cares about standards anyway - let's just make up the rules as we go along!

    You could create an fprintf (or whatever) that sets up your port selection, then calls the standard printf.
    Maybe you could do it as a Macro?

Reply
  • "So I can not switch between output function (e.g. lcd_putchar and putchar)"

    That's ANSI 'C' for you!

    "which I can do in PIC-C(C compiler for PIC Microcontroller) very flexible as:
    - printf("......",.....);//default serial port
    - printf(lcd_putc,"......",.....);//redirect to LCD"


    Well, who cares about standards anyway - let's just make up the rules as we go along!

    You could create an fprintf (or whatever) that sets up your port selection, then calls the standard printf.
    Maybe you could do it as a Macro?

Children
  • That's ANSI 'C' for you!

    In all fairness, ANSI C has nothing to do with this. What we're seeing here is a direct consequence of a design decision made by Keil: they decided to support printf(), but not fprintf() and fopen(), which would be the brutally obvious way a job like this would be done in actual ANSI C. I.e. the code at hand should be writable like this:

    FILE *lcdout = fopen("mylcd_pseudofile", "w");
    fprintf(lcdout, "this goes to the screen");
    
    FILE *serialout = fopen("serial_pseudofile", "w");
    
    printf("this goes to default output device");
    

    But in Keil, you can't really do it this way, because neither fopen() nor fprintf() exist.

    You could implement them yourself, though, and with the plan of keeping the main body of the code as close to ANSI C as possible, that may even be the best idea available.