To write data to serial port
- To write a byte to the serial port one must simply write the value to the SBUF (99h) SFR.
- if you wanted to send the letter "A" to the serial port, it could be accomplished as easily as
MOV SBUF,#A
- Upon execution of the above instruction the 8051 will begin transmitting the character via the serial port.
- Obviously transmission is not instantaneous--it takes a measureable amount of time to transmit.
- And since the 8051 does not have a serial output buffer we need to be sure that a character is completely transmitted before we try to transmit the next character.
- The 8051 lets us know when it is done transmitting a character by setting the TI bit in SCON.
- When this bit is set we know that the last character has been transmitted and that we may send the next character.
CLR TI ;Be sure the bit is initially clear
MOV SBUF,#A ;Send the letter A to the serial port
JNB TI,$ ;Pause until the TI bit is set.
MOV SBUF,#A ;Send the letter A to the serial port
JNB TI,$ ;Pause until the TI bit is set.
- The above three instructions will successfully transmit a character and wait for the TI bit to be set before continuing.
- The last instruction says "Jump if the TI bit is not set to $"--$, in most assemblers, means "the same address of the current instruction.".
- Thus the 8051 will pause on the JNB instruction until the TI bit is set by the 8051 upon successful transmission of the character.
To read Data from serial port
- Reading data received by the serial port is equally easy.
- To read a byte from the serial port one just needs to read the value stored in the SBUF (99h) SFR after the 8051 has automatically set the RI flag in SCON.
- For example, if your program wants to wait for a character to be received and subsequently read it into the Accumulator, the following code segment may be used:
MOV A,SBUF ;Read the character from the serial port
- The first line of the above code segment waits for the 8051 to set the RI flag; again, the 8051 sets the RI flag automatically when it receives a character via the serial port.
- So as long as the bit is not set the program repeats the "JNB" instruction continuously.
- Once the RI bit is set upon character reception the above condition automatically fails and program flow falls through to the "MOV" instruction which reads the value.
PREVIOUS: Serial communication NEXT: Power Saving Operation
2 comments:
I am very thankful to you for providing such a great information. It is simple but very accurate information.
You have lots of great content that is helpful to gain more knowledge. Best wishes.
Post a Comment