Can you confirm that the 8051 really only sends data once? One way to check is to use scope to see what happens on the UART TX pin.
What software do you use on your PC? I would suggest using simple communication software such as HyperTerminal or PuTTY . If they show that the string is sent to the PC several times, then the probability that the error is in the software running on 8051.
EDIT:. To be honest, it sounds like a debugging that engineers encounter on a regular basis, and therefore this is a good opportunity for you to practice a good old-fashioned methodological problem - a solution.
If I can be very dumb, I suggest you do the following:
- Debug. Try everything, but do not guess . Experiment. Make small changes to your code and see what happens. Try everything you can come up with. Find more information on the Internet.
- If this does not resolve, return here and provide us with all the information we need. This includes relevant code snippets, complete information about the hardware that you are using, and information about what you tried in step 1.
EDIT: I have no answer to editing the question, so here is the code posted by OP in a comment on her question:
#include<reg51.h> void SerTx(unsigned char); void main(void) { TMOD = 0x20; TH1 = 0xFD; SCON = 0x50; TR1 = 1; SerTx('O'); SerTx('N'); SerTx('L'); SerTx('Y'); void SerTx(unsigned char x) { SBUF = x; while(TI==0); TI = 0; } }
As Neil and Brooksmoos mention in their answers, in the embedded system, the main function can never stop. Thus, you need to either put your code in an infinite loop (which may happen inadvertently), or add an infinite loop at the end, so the program effectively stops.
In addition, the SerTx function must be defined outside the main one. This may be syntactically correct, but it simplifies everything without declaring functions within other functions.
So try this (I also added some comments in an attempt to make the code more understandable):
#include<reg51.h> void SerTx(unsigned char); void main(void) { /* Initialise (need to add more explanation as to what each line means, perhaps by replacing these "magic numbers" with some #defines) */ TMOD = 0x20; TH1 = 0xFD; SCON = 0x50; TR1 = 1; /* Transmit data */ SerTx('O'); SerTx('N'); SerTx('L'); SerTx('Y'); /* Stay here forever */ for(;;) {} } void SerTx(unsigned char x) { /* Transmit byte */ SBUF = x; /* Wait for byte to be transmitted */ while(TI==0) {} /* Clear transmit interrupt flag */ TI = 0; }
Steve melnikoff
source share