If the Data transfer is done in bit by bit transfer via a single wire, then we refer it as serial communication. Where as in parallel communication 8-bits are transfer at a time using 8-bit bus.
Difference between serial and parallel Communication
Parallel communication uses 8-bit, 16-bit or 32 bit bus cables for data transfer. Speed of data transfer is very high. Huge amount of data can be transfer within seconds depend upon processor. But cost of these cables increases as length increases. Hence parallel communication is not implemented for longer distances.
Examples for parallel communication: Data transfer between hard disk and mother board in your PC. Off course the size of the bus may be 8-bit, 16-bit, 32-bit.
Serial communication uses only three wires (Tx, Rx, Gnd), so implementation is easy and cost is also very less. That why it is used in longer distances but data transfer is slow compared to parallel communication.
Examples for serial communication: Communication (Com) ports in your PC, keyboard and mouse is also implements serial communication. Imagine the cost that your PC has keyboard with parallel bus cable. Generally this serial communication is used in wireless devices, example old wireless internet modems.
Serial Communication in 8051 Microcontroller:
The 8051 microcontroller contains in-built serial port which provides both synchronous and Asynchronous serial communication.
What is Synchronous serial data communication?
In Synchronous serial data communication, the data is sent along with a clock signal. The Clock signal always synchronizes the two communicating devices. The figure 1.1 shows timing diagram for synchronous data communication between two devices.
Synchronous Data Communication |
What is Asynchronous serial data communication?
Unlike synchronous serial data communication, the two communicating devices are not in synchronizing. So it is difficult to the receiver to detect that when will be the data is coming from the transmitter. Therefore, to give the indication to the receiver device, the transmitters need to frame the data by adding start and stop bits as shown in the figure 1.2.
Asynchronous Data Communication Data Frame Format |
When the two devices are not in communication, at receiver side the logic high is called as Mark signal and the logic zero as Space. The transmitter sends a start bit signal before 8-bit data, finally it add stop bit as shown in data frame format in figure 1.2. The receiver gets its attention by identifying the valid start bit in the serial line and reads the 8-bit data until stop bit is detected.
How the receiver device checks the valid start or stop bit in the Asynchronous serial communication
In asynchronous serial communication the receiver checks its pin continuously for valid start bit. If the receiver see logic low on its pin, then it wait for half the time interval of valid start bit and again checks its pin. If the signal still low then the receiver confirms it’s a valid start bit.
Block Diagram for Serial port control in 8051 micro controller
The Block diagram of the serial port of the 8051 microcontroller is shown in figure in 1.2. The serial port in 8051 microcontroller supports full duplex, synchronous and Asynchronous communication. Because of full duplex the serial port block diagram shows two separate lines for transmitter and receiver. The 8051 serial port internally contains two separate buffers for transmitter and receiver points to single SFR address (0x99).
Block diagram of serial port in 8051 microcontroller |
The transmit buffer (SBUF) is filled with a character by the 8051 program written by us (programmers). Whenever we load some character into the SBUF register, the character immediately transfers to parallel to serial converter. Now parallel to serial convertor sends out the converted bit stream to TXD line with given baud rate frequency. The baud rate can be generated by dividing the CPU clock with certain value (baud rate divisor) calculated and loaded into the timer. Whenever the parallel to serial converter sends out the last bit of character then the transmitter flag TI is set automatically. If TI flag is set to 1, indicates that the last bit of character in transmitter buffer is sent out i.e., the transmitter is empty. Now we can load another character into the buffer. If we load a new character before the present character is sent out completely, then the new character will override the present character. For this purpose, while writing the program for serial transmission in 8051 microcontroller we always check for the TI flag is 1 or not.
Similarly, when the entire bit stream is received by the serial to parallel converter from RXD line will send to received buffer (SBUF). When the receiver buffer is full then the RI flag is set to 1 by indicating the receiver buffer is full. Now our program has to be read the character in the receiver buffer (SBUF).
The SCON Register in 8051 is used to initialize the serial communication setting like baud rate, data length, no. of stop bits etc.,. The figure 1.4 shows the SCON Register with bits.
SCON Register:
SCON register |
SM0, SM1 bits in SCON are used to define the type of the serial communication, baud rate and framing.
SM0 | SM1 | Mode | Function |
---|---|---|---|
0 | 0 | 0 | Shift register, baud rate = f/12, Synchronous serial communication mode |
0 | 1 | 1 | 8-bit UART; baud = variable , asynchronous serial communication mode |
1 | 0 | 2 | 9-bit UART; baud = f/32 or f/64, asynchronous serial communication mode |
1 | 1 | 3 | 9-bit UART; baud = variable, asynchronous serial communication mode |
SM2 - bit is used for multiprocessor communication. Set or cleared by the program to enable multiprocessor communications in mode 2 and 3. When set to 1 an interrupt is generated if bit 9 of the received data is a 1; no interrupt is generated if bit 9 is 0;
REN – Receiver enable bit. To accept reception of data this bit must be 1;
TB8 – Transmitted bit 8.
RB8 – Received bit 8.
TI – Transmit interrupt flag. This will be enabled when all bits in the transmitted buffer is shifted out.
RI – Receive interrupt flag. This will be enabled when a character is received in the receiver buffer.
How to write a program to transmit a character with 9600 baud rate using 8051 assembly code in polling method? F= 11.0592MHz
Step1: Initialize SCON register
Step2: Calculated baud rate.
Step3: Initialize TMOD register, so that make the timer0 in auto re-load feature.
Step4: Start timer
Step5: Load a character in SBUF.
Step6: wait for TI to be 1.
Step7: end or load new character.
How to initialize SCON register?
Here I am choosing mode 1 (8-bit UART), so that M0 and M1 bit combination is 01. In this program I disable the receiver and multiprocessor communication. And also TI and RI bit are cleared to zero.
Now the data loaded into SCON register = 01000000 = 40H
Assembly Code: MOV SCON, #40H
How to calculate Timer value to be loaded with the given baud rate?
The given baud rate is 9600 bits/sec, the value loaded into timer register is given by
THX = maxCount - (Clock Frequency)/ (12*32* baud rate).
Therefore, THX = 256 - (11.0592M)/ (12*32*baud rate).
THX = 256 – 3 = 253d = FDh
How to initialize timer0 in auto reload mode?
TMOD register |
From the figure 1.5, the Timer 0 can be initialized in auto reload feature by making first MSB bit combination would be – ‘10’
TMOD = 00000010b = #02 h.
Program:
Org 0000h
MOV SCON, #40h; initialize SCON register
MOV TMOD, #02h; initialize TMOD register
MOV TH0, #0FDh; load timer 0 with FD for 9600 bits/sec baud rate.
SETB TR0; start timer 0
MOV SBUF, ‘A’; load ‘A’ IN SBUF register
Here: JNB TI, Here; wait here until TI flag is set to one. Means all bits in the TXD buffer is shifted out
CLR TR0; STOP timer0;
END
Interrupt method serial communication:
Program: The program is written to accept a received character from serial port using interrupt method. If character is not received, our program itself transmits a character ‘N’ via serial port using polling method.
ORG 0000
LJMP Main_Label ; redirect the control to some other location
ORG 0023
JNB RI, end; Check whether the interrupt is occurred on the reception of character
MOV A, SBUF; read received character into A register.
End : nop
RETI
ORG 0040h
Main_Label : MOV SCON, #50h; initialize SCON register with reception enable
MOV TMOD, #02h; initialize TMOD register
MOV TH0, #0FDh; load timer 0 with FD for 9600 bits/sec baud rate.
SETB TR0; start timer0
Back: MOV SBUF, ‘N’; load ‘N’ IN SBUF register
Here: JNB TI, Here; wait here until TI flag is set to one.
SJMP Back
END