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

printf does not work on microcontroller

I have a very simple but annoying problem. I use printf () in my code and either on Keil or Proteus simulations it works. However, when I load this code to my microcontroller, which is DS89C450, it does not work.I also add the putchar.c and getkey.c to my source code. Characters, observed on Terminal Emulator are dots and squares and sometimes endless loops. The baud rate is correct. I test it by sending characters via SBUF0. The code is right here.

#include <DS89C4xx.H>
#include <stdio.h>

void main (void)
{
  SCON0=0x50;
  TMOD=0x20;
  TH1=0xFC; //9600 bps with 14.7456 MHz crystal
  TR1=1;
  TI_0=1;
   while (1)
  {     printf ("test\n");
}

Parents Reply Children
  • I changed putchar.c in c51/lib to that.

    /***********************************************************************/
    /*  This file is part of the C51 Compiler package                      */
    /*  Copyright KEIL ELEKTRONIK GmbH 1990 - 2002                         */
    /***********************************************************************/
    /*                                                                     */
    /*  PUTCHAR.C:  This routine is the general character output of C51.   */
    /*  You may add this file to a uVision2 project.                       */
    /*                                                                     */
    /*  To translate this file use C51 with the following invocation:      */
    /*     C51 PUTCHAR.C <memory model>                                    */
    /*                                                                     */
    /*  To link the modified PUTCHAR.OBJ file to your application use the  */
    /*  following Lx51 invocation:                                         */
    /*     Lx51 <your object file list>, PUTCHAR.OBJ <controls>            */
    /*                                                                     */
    /***********************************************************************/
    
    #include <DS89C4xx.H>
    
    
    #define XON  0x11
    #define XOFF 0x13
    
    
    /*
     * putchar (full version):  expands '\n' into CR LF and handles
     *                          XON/XOFF (Ctrl+S/Ctrl+Q) protocol
     */
    char putchar (char c)  {
    
      if (c == '\n')  {
        if (RI_0)  {
          if (SBUF0 == XOFF)  {
            do  {
              RI_0 = 0;
              while (!RI_0);
            }
            while (SBUF0 != XON);
            RI_0 = 0;
          }
        }
        while (!TI_0);
        TI_0 = 0;
        SBUF0 = 0x0d;                         /* output CR  */
      }
      if (RI_0)  {
        if (SBUF0 == XOFF)  {
          do  {
            RI_0 = 0;
            while (!RI_0);
          }
          while (SBUF0 != XON);
          RI_0 = 0;
        }
      }
      while (!TI_0);
      TI_0 = 0;
      return (SBUF0 = c);
    }
    
    
    
    #if 0         // comment out versions below
    
    /*
     * putchar (basic version): expands '\n' into CR LF
     */
    char putchar (char c)  {
      if (c == '\n')  {
        while (!TI_0);
        TI_0 = 0;
        SBUF0 = 0x0d;                         /* output CR  */
      }
      while (!TI_0);
      TI_0 = 0;
      return (SBUF0 = c);
    }
    
    
    /*
     * putchar (mini version): outputs charcter only
     */
    char putchar (char c)  {
      while (!TI_0);
      TI_0 = 0;
      return (SBUF0 = c);
    }
    
    #endif
    

  • I changed putchar.c in c51/lib to that.

    You may still be linking with the library version of putchar(). Try putting a breakpoint in your putchar() and see if it is being called.

  • you saved it as "PUTCHAR2.C"? you need to save it as "PUTCHAR.C" in your project directory.

  • No, I think that's wrong:

    The name of the file shouldn't matter - it's the name of the function that's important (also its return type & parameter types).

  • [...] it's the name of the function that's important [...]

    Depends a bit on what file name the project file mentions :)

  • I think i skipped something and that is it: I changed putchar but printf() works with putchar and _getkey so i have to change _getkey by swapping SBUF with SBUF0 and RI with RI_0 AND TI with TI_O too. I have the equipments at firm. So I'm going to try this Monday. If it wouldn't work, I acquaint you with that.

    P.S: Thank you all for your care. I hope it works.. :)