How can I send a string serial from 8051 only once?

I am making the 8051 microcontroller wirelessly with a computer. The microcontroller will send the string to its serial port (DB9), and the computer will receive the string and manipulate it.

My problem is that I do not know how to get 8051 to pass a string only once. Since I need to manipulate a string on a PC, it needs to be taken only once. Currently, although in C code I send a line once, on my computer I get the same line continuously. I suppose this is because everything in the SBUF is continuously transmitted. Is there a way that I can send my string only once? Is there a way to clear SBUF?

I tried using the RTS (Request to Send) pin (7th pin) in DB9 because I read somewhere that if I denied the voltage on this pin, it would stop the data flow to the serial port. So what I did was programmed my microcontroller to send a string, and then sent logic level 0 to the output pin, which was connected to my DB9 RTS pin. However, this did not work.

Does anyone have any suggestions? I would really appreciate it.

EDIT

The software that I use on my PC is X-CTU for Xbee modules. This is the code of my microcontroller:

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

Can anyone confirm that it really only sends one line?

EDIT

It seems like Steve, brookesmoses and Neil hit a nail on the head when they said that this was what happened AFTER my main function that caused the problem. I just tried the suggested Steve code (more specifically for for (;;) and the definition of serTX outside of main), and it worked fine. The controller probably reboots and, therefore, the same code continues to repeat.

Thank you for help!:)

+6
c embedded microcontroller serial-port 8051
source share
5 answers

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; } 
+6
source share

The code you posted doesn't have a loop in main (), so you need to determine what the C compiler runtime does when main () returns after sending "Y". Given your problem, I suppose the compiler generates some code to do some cleanup and then restarts the micro (maybe the hardware reset, maybe just restart the C version of C). It looks like your program works exactly as you wrote it, but you ignored what happens before and after calling main ().

If you want your line to be sent once and only once, ever, you need to add something like while(1) {} after sending the last character. But then your program does nothing - it will just execute an empty loop forever. To start, a reset is required again (for example, a power cycle), and send a line.

Please note that if your microcontroller has a watchdog timer, it may intervene and cause an unexpected reset. If this happens, your line will be sent once for each watchdog reset (which can be about once per second, at a speed depending on your hardware).

Also, if serTx () is defined nested inside main (), then probably not what you want.

+6
source share

It is difficult to say what the problem is without seeing any of the code 8051. For example, a logical error on this side may cause the data to be sent several times, or the 8051 software may wait for an ACK that is never received, etc. d.

Normally, 8051 code should explicitly send each character, but I assume this will take care of you at runtime C.

Using RTS / CTS (send / clear request for sending) is designed to control the flow (i.e. to prevent buffer overflows), usually there are not enough buffers on such microcontrollers) and generally do not stop the transmission.

+2
source share

Echo of Neil's answer (in the answer, since I still cannot repeat the comment): In a typical situation with a microcontroller without an OS, it is not immediately clear that the exit () function, which is implicitly called at the end of main (), should do - or, more precisely, cannot do the usual β€œend of the program and return to the OS”, because there is no OS to return.

In addition, in a real application, you almost never want the program to just stop if you do not disconnect the system. So, one thing that the exit () implementation should definitely not do is take up a lot of code space.

On some systems I worked on, the exit () function is not implemented at all - if you are not going to use it, don’t even spend a byte on it! As a result, when the execution path reaches the end of main (), the chip simply goes to the lala land, doing everything that happens in the next bit of memory, and usually quickly ends up either stuck in a loop or a failure with an illegal operation code. And the usual result of an error with an illegal opcode is a chip reset.

This seems like a plausible theory of what is happening here.

+2
source share

This article is intended to be build, not C, but it may be useful: http://www.8052.com/tutser.phtml

-one
source share

All Articles