How is the data transferred serially? This has been shown in the figure below illustrating how a byte is transferred.
Suppose we wish to transfer 01000001 serially (as shown above, this is the ASCII value of letter ‘A’). The byte is placed between a start bit and a stop bit. Sometimes we can also have two start bits. The stop bit allows the receiver to organize the data before the next byte is send. So in total we have 10 bits that are transferred one by one, but at receiving end we have to decode the information i.e. remove the stop bit and pack the 8 bits into a byte for storage.
The serial data is transferred through a serial cable (with a DB9 pin socket). Let’s discuss the important characteristics of data transfer for 8051.
SBUF Register: The data is transferred via TxD line (P3.1) and received through RxD (P3.0) line in 8051. The data transferred via TxD line must be placed in SBUF register for its transfer and similarly while receiving through RxD the data is received in the SBUF register.
SCON (Serial control) Register:
a.) Mode selection
- The different modes of serial communication can be set by setting the values of SM0 & SM1.
- There are four modes; we will be discussing here only mode one.
- The mode1 can be selected by setting SM0=0 & SM1=1
- The first mode allows the baud rate (discussed below) to be variable.
- It has one start and one stop bit.
b.) REN (receive enable): If we wish to both transfer and receive, we can set REN= 1 otherwise receiving can be disabled by making REN= 0.
c.) SM2, TB8, RB8: are set to zero in mode 1
d.) TI (transmit interrupt): when 8051 finishes the transfer; it raises this flag to indicate that it is ready for transfer of another byte.
e.) RI (receiving interrupt): when the data is received serially via RxD, it gets rid of start and stop bit and the byte is placed in SBUF register. Then the RI flag is raised to indicate that a byte has been received and should be picked.
Rate of data transmission (baud rate):
The rate of transfer and reciving can be changed by a programmer. This is done with the help of ‘timer1’.
crystal frequency ÷12=machine cycle
machine cycle÷32=baud rate
In case we are using a crystal of frequency 11.0592MHz; machine cycle frequency = 921.6 KHz; baud rate= 28,800 Hz. To set timer1 to this baud rate we have to program it in mode2 (8 bit auto-reload).
To set a particular baud rate what should be the value of TH1->
Let’s discuss this with an example. Suppose we wish to set ‘9600’ baud rate:
- 28,800/9600= 3;
- TH1= -3 or FD hex.
Numbering of DB9 or serial port:
(Note that we have to ground the pin number 5 for transmission of data)
Are serial port of computer and microcontroller logically equivalent?
1.) The computer works on RS232 logic:-
- logic 1 = -3V to -25V
- logic 0 = +3V to + 25V
2.) The microcontroller works on TTL logic:-
- logic 1= 5V
- logic zero= 0V
3.) An IC MAX232 (or MAX233) that can convert the RS232 logic to TTL logic is used.
TRANSFER OF DATA
The program below will keep on transferring string “YES” serially at a baud rate of 4800 continuously.
Let’s write a program to receive data and send it to port 1. We can use an 8-bit display or a LCD to display the output.
Note:- In the above two examples the baud rate is different just to show how you can use different baud rates in your own program. While writing a program baud rate should be same on both transmitter and receiver side.