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");
}

  • Exactly how do you know that the baudrate is correct? Have you checked the bit length with an oscilloscope?

    A second thing: Are you making sure that the transmit register is empty before you write the next character to it?

  • When i write that code it works both in simulation and on microcontroller

    #include <DS89C4xx.H>
    
    void main (void)
    {
      SCON0=0x50;
      TMOD=0x20;
      TH1=0xFC;
      TR1=1;
      TI_0=1;
      while (1)
      {     while (!TI_0);
            if (TI_0==1){
                    TI_0=0;
                    SBUF0='t';
                    }
            while (!TI_0);
            if (TI_0==1){
                    TI_0=0;
                    SBUF0='e';
                    }
            while (!TI_0);
            if (TI_0==1){
                    TI_0=0;
                    SBUF0='s';
                    }
            while (!TI_0);
            if (TI_0==1){
                    TI_0=0;
                    SBUF0='t';
                    }
            }
    }
    
    

    this program writes "testtesttest...". I observe it on Terminal Emulator. This means the baud rate is correct. Unfortunately, when I use printf() instead of sending each character by using SBUF0 it does not work on microcontroller. I cannot observe onTerminal Emulator's screen but i can observe on Keil's Serial Window.

  • Sounds like you have a bug in your putchar(), then?

  • I changed all SBUF to SBUF0, RI to RI_0, and TI to TI_0 in PUTCHAR.c and saved as PUTCHAR2.C and add to my source code but it didn't work.

  • 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.

  • 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.
    you say nothing about your hardware, but I do hope you have a MAX232(equivalent) correctly implemented between the uC and the PC.
    One poster a long time ago had the same problem and it was using the wrong capacitor values.

    since it works on the simulator, I guess hardware or baudrate error.

    Erik

  • 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.. :)