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

Simulator issue with printf?

While using the simulator, the values of i in the serial window are shifted by left 8 bits.

#include "stdlib.h"
#include "stdio.h"
#include "reg51.h"

void main (void)
{
    unsigned char i;
    SCON  = 0x50;
    TMOD |= 0x20;
    TH1   = 221;
    TR1   = 1;
    TI    = 1;

    for (i = 0; i < 8; i++)
    {
        printf("%x\n", i);
    }
}

Parents
  • I HAVE NOW GOT BETTER CODE WITH INITIALOIZED DATA AND IT IS NOW WORKING BETTER

    IT WORKS WITH MOST NUMBERS 0 TO 99
    BUT IT DOES NOT WORK WITH NUMBERS 100 to 255

    THIS MUST WORK 100% TODAY AND PRINT BCD NUMBERS

    WHAT IS WRONG?

    int printbcd(int value)
    {
      printf("%x",binerytobcd(value));
    }
    
    int binerytobcd(int binary)
    {
      int bcd;      /* answer */
    
      bcd = 0;      /* initilase variable */
    
      while (binary > 10) /* while binary is more than 10 */
      {
        bcd = bcd + 16;       /* change bcd */
        binary = binary - 10; /* change binary */
      }
    
      while (binary > 0)   /* while binary is greater than 1 */
      {
        bcd = bcd + 1;    /* chainge bcd */
        binary = binary - 1; /* change binary */
      }
    
      return bcd;        /* result */
    }
    
    

Reply
  • I HAVE NOW GOT BETTER CODE WITH INITIALOIZED DATA AND IT IS NOW WORKING BETTER

    IT WORKS WITH MOST NUMBERS 0 TO 99
    BUT IT DOES NOT WORK WITH NUMBERS 100 to 255

    THIS MUST WORK 100% TODAY AND PRINT BCD NUMBERS

    WHAT IS WRONG?

    int printbcd(int value)
    {
      printf("%x",binerytobcd(value));
    }
    
    int binerytobcd(int binary)
    {
      int bcd;      /* answer */
    
      bcd = 0;      /* initilase variable */
    
      while (binary > 10) /* while binary is more than 10 */
      {
        bcd = bcd + 16;       /* change bcd */
        binary = binary - 10; /* change binary */
      }
    
      while (binary > 0)   /* while binary is greater than 1 */
      {
        bcd = bcd + 1;    /* chainge bcd */
        binary = binary - 1; /* change binary */
      }
    
      return bcd;        /* result */
    }
    
    

Children