Why Hanging ???

Regards all,

This codes generates a hang on cpu .
Please kindly take this & guid me :

#include <w78e65.H>
#include <stdio.h>
main()
{
unsigned char i;
TMOD=0x20;
SCON=0x52;
TH1=0xfa;
TR1=1;
for(i=0;i<=255;i++)
{
printf("%bd",i);
P2=i;
}
}
/**************************************/

Parents
  • aziz,

    What everyone here is trying to tell you (with various levels of indirection) is that you need to stop imagining that your printf() will get a character out the serial port instantly.

    While the 8051 can call the function very quickly, the actual data will take some significant time to be transmitted by the hardware, assuming you're using some close-to-normal baud rate. So, as William, Erik, and others suggest, you need to change your loop to allow this to happen.

    What's happening NOW is that your program is very quickly calling printf() 255 times, then your program gets to the end and resets. If you are correct about the one character printout, it's because it's all happening in about the time it takes to send one character. I also suspect that you didn't copy-and-paste this code from the editor into the message. I say this because as it's written, the for() loop is infinite. Your test condition is "<= 255", but an unsigned char is always <= 255. So right now, it will get to 255, pass your test condition, be incremented, roll over to 0 and then continue.

    Try changing your for loop to this and see what happens:

    for(i = 0; i < 255; i++) {
      TI = 0;
      printf("%bd",i);
      while(!TI);
    }
    

    Hopefully now you'll understand why that one will work, but yours doesn't.

Reply
  • aziz,

    What everyone here is trying to tell you (with various levels of indirection) is that you need to stop imagining that your printf() will get a character out the serial port instantly.

    While the 8051 can call the function very quickly, the actual data will take some significant time to be transmitted by the hardware, assuming you're using some close-to-normal baud rate. So, as William, Erik, and others suggest, you need to change your loop to allow this to happen.

    What's happening NOW is that your program is very quickly calling printf() 255 times, then your program gets to the end and resets. If you are correct about the one character printout, it's because it's all happening in about the time it takes to send one character. I also suspect that you didn't copy-and-paste this code from the editor into the message. I say this because as it's written, the for() loop is infinite. Your test condition is "<= 255", but an unsigned char is always <= 255. So right now, it will get to 255, pass your test condition, be incremented, roll over to 0 and then continue.

    Try changing your for loop to this and see what happens:

    for(i = 0; i < 255; i++) {
      TI = 0;
      printf("%bd",i);
      while(!TI);
    }
    

    Hopefully now you'll understand why that one will work, but yours doesn't.

Children
More questions in this forum