How to perform serial communication on PIC microcontroller?

Introduction to serial communication

How to perform serial communication on PIC microcontroller? There are two main types of communication: Serial Transfer and Parallel Transfer. In parallel transfer, a chunk of data is transmitted at a time. While in serial transfer, one bit is transferred at a time. Parallel communication is fast while serial communication is slow. But for long distance transmission, serial communication is preferred. CPU uses parallel transfer of data with the hard disk. Similarly, keyboard transfers the data parallel to computer.

Serial Vs Parallel
Serial Vs Parallel

Lets say you have connected a temperature sensor to a microcontroller and you want to monitor the temperature on your computer  screen, then you can use the serial communication for sending that information to your computer Via PIC microcontroller.

Though parallel communication is fast but it is expensive since more cables are required for transferring the data. In serial communication, a single wire is enough for carrying the data.

That is why serial communication is used for long distance transmission.

Types of communication

There are three types for data communication

  • Simplex
  • Half Duplex
  • Full Duplex

Simples communication

In simplex communication you can transmit the data in only one direction. That is why it is also called unidirectional communication. There is only one sender who will be sending data and a receiver who will be receiving the data. Its example is a printer that can only send data to computer for printing.

Half Duplex Transmission

This is the bidirectional transmission on a single dedicated channel or link. If the sender is sending the data, the receiver will be only receiving it. There is a switch for the mode selection. Both senders and receivers can not send or receive the data simultaneously. A simple example of Half Duplex communication is the Walkie Talkie, in which there is a reserve word for ending your message. Each time a sender wants to send a message, it has to say a particular word for telling the receiver it has finished the message.

Full Duplex Trans

It is also bidirectional but the difference is it has two separate dedicated channels between sender and receiver so that they both can send and receive the message at a time. Modern mobile phones are the examples of full duplex transmission.

The following figure shows the block diagram of these transmission types.

Types of transmission
Types of transmission

Why do we use MAX232 IC in serial communication for PIC?

PIC microcontroller has a built in UART for performing the asynchronous serial communication. Since PIC microcontroller operates on 5V while the computer’s communication port works on RS232 standards according to which -25V to -3V produce logic 1 and +3v to +25v produce logic 0. The voltage level -3v to +3v is undefined.

In order to accommodate their  voltage level, a level converter MAX232 IC is added in between them. MAX232 will convert TTL voltage level to RS232 levels and vice versa.

What are the RS232 handshaking signals?

Handshaking is the procedure for initiating the communication. In order to see all the communications equipment and the data terminal equipments are ready for the communication, handshaking is done.

PC and modems perform handshaking by using the signals DTR (Data Terminal Ready) and DSR (Data set ready). DTR is the output signal form the PC once it tests its communication port while DSR is the output signal that modem send to computer after going through the self test. They both are active low signals. Once both computer and modem have gone through self testing procedure, the computer generates a signal RTS (Request to send) to indicate the modem that it has some data for transmission. Modem after checking its internal buffer either it has room for new data or not sends a signal to computer that is CTS (Clear to Send) and also it will ring if it establishes the connection to the modem at the receiving end.

What is the Null Modem connection?

In the Null modem connection there is no need of modem to be connected. The DTE (Data Terminal Equipment) are connected directly as shown below:

Null modem connection
Null modem connection

How to connect MAX232 to PIC18 microcontroller?

The following figure shown the connection of MAX232 with PIC microcontroller.

connection of MAX232 with PIC microcontroller
connection of MAX232 with PIC microcontroller

PIC microcontroller has dedicated pins on its chip for serial communication that are PIN no. 25 (Tx : Transmitter) and pin no. 25 (Rx: Receiver). The Tx Pin of PIC is connected to Pin number 11/Tin (Transmitter input) of MAX232 IC for sending data from PIC microcontroller to PC. While the Rx (receiver) is connected to Rout pin of MAX232 for getting data that will be sent by computer. Similarly Tout Pin of MAX232 is connected to Tx (in) of DB connector pin.

For understanding the idea of PIC microcontroller serial communication register, click here. 

MPLAB code for PIC for serial communication

#INCLUDE<P18F4550.INC>
org 0x0
MOVLW 0X20
MOVWF TXSTA
MOVLW D’15’
MOVWF SPBRG
BCF TRISC,TX
BSF RCSTA,SPEN
OVER MOVLW A’Y’
CALL TRANS
MOVLW A’E’
CALL TRANS
MOVLW A’S’
CALL TRANS
MOVLW 0X0
CALL TRANS
BRA OVER

TRANS
S1 BTFSS PIR1,TXIF
BRA S1
MOVWF TXREG
RETURN
END

MPLAB code for serial communication
MPLAB code for serial communication

Serial Port Programming 

Also read here

How to program counters in microcontroller?

Leave a Reply

Your email address will not be published. Required fields are marked *