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; } } /**************************************/
Hanging where, how. How did you detect this? What will your CPU do once your for() loop is finished and it falls off the end of main()?
Dear Hans, Regards, I have received only '0' in hyperterminal is this show somethings ? I think the loop runs once and cpu hangs . Is my cpu (W78E65P from winbond) important ? All The Best Majid
Suggestion: Add a loop counter variable to tell you exactly how many times it executes before er.. hanging. I would suggest adding a delay inside the loop, enough to allow for the uart to transmit the byte before writing a new value into sbuf, say 2ms @ 9600baud. Why use printf() for this? It's as easy as that.
Run it in the simulator and see what happens.
I think the loop runs once and cpu hangs . You think right. Now take you darn PC hat off and put on a '51 hat. Ok, now, with the right hat on, you know why. Erik
William, You are right, i faced a similar problem, which was because of sending a byte too early when the previous one is being transmitted.
I would suggest adding a delay inside the loop, enough to allow for the uart to transmit the byte before writing a new value into sbuf To what purpose, what is wrong with testing RI? Erik
For starters... it wouldn't work. Testing "TI" might though. :)
OOPS Erik
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); }
Unless the OP has replaced the standard putchar() function then printf() will wait for TI.
Stefan, Thanks for the tip. I've never actually found a reason to use printf myself, when it's so easy to just load up SBUF. Thanks for pointing that out though, or I could end up further frustrating the original poster.
"I've never actually found a reason to use printf myself, when it's so easy to just load up SBUF." If you think about it it has to wait for TI otherwise it wouldn't be able to output more than one character. How you live without printf() I just can't imagine!
How you live without printf() I just can't imagine! I can, 50+ designs and 600.000+ installed units late, I still have not seen the need to use it. Erik
Stefan, I guess I had always imagined that it handled things via interrupt and a circular buffer or something of the like. In retrospect, I'm not sure how it would have been able to install an ISR via just including stdio.h. :) As far as living without it. I dunno. I've yet to design a product that didn't require my actually using the serial communications channel, so I'd typically just hook into the routines that I wrote, or find some other way to get debug data out. You'd be amazed what you can do with just a few spare pins tied to some inverters. :)