Home / Home made Projects / Microcontroller Projects / Serial communication between PIC Microcontroller and PC

Serial communication between PIC Microcontroller and PC

In this article we will learn about Serial communication between PIC microcontroller and PC. Step by step procedure, block diagram, program and simulation results of Serial communication between PIC microcontroller and PC will be discussed.

Methods of communication

There are two different methods of communication between microcontroller and PC which are Serial communication and parallel communication. In Parallel communication, multiple bits of data is sent simultaneously through a number of parallel lines.

In serial communication, data is sent bit by bit through a single line or conductor. Parallel communication is fast however it requires a number of parallel paths therefore it is not feasible for long distance communication. On the other hand, serial communication is slower as compared to parallel communication but it requires only one path therefore it is very economical and can be used for long distance communication.

In order to understand the serial communication between PIC microcontroller we need to go through some of the basic terminologies of serial communication.

Synchronous and Asynchronous Communications

There are two basic types of serial communications  (i) synchronous (ii) asynchronous.

In synchronous communication, two devices initially connect and synchronize before they share data. In synchronous communication, the devices continuously send and receive null character in order to stay in synchronism. Synchronous communication is faster and it does not need any additional bits at start and at the end.

In Asynchronous communication devices do not need to be synchronizing first or remain continuously in synchronism. However the communication is started and ended by null characters to mark the start and end of communication or data byte. As compared to synchronous, it is slower but system does not have to remain in continuous synchronism.

UART Serial communication

UART stands for Universal Asynchronous Receiver Transmitter. It is a microchip facility which allows serial communication. UART protocol generates signal that is compatible with RS 232.

Simplex and Duplex

The serial port on PC is a full-duplex device meaning that it can receive and send data at the simultaneously. To accomplish this task, it employs separate lines for transmitting and receiving data. Some kind of  devices can only perform one-way communications and therefore use only two wires in the cable – one is to transmit line and second is the signal ground.

RS 232 standard
RS-232 protocol is used for low data rate serial communication. There are other protocols RS485, RS422, RS423 etc. (you can give hyperlinks about these protocols)

Requirements  

In order to perform serial communication between PIC microcontroller and PC, following components are needed

RS 232 (DB9)

RS-232 stands for Recommend Standard number 232. RS232 is a standard (protocol) for serial data communication. A port called Rs232 (DB 9) port which can be used for RS232 serial communication. It consists of 25 ports among which 22 are used. For communication with PCs, most of the Pins are not used. Most new PCs are equipped with male D type connectors having only 9 pins.

MAX 232

The Max232 is an IC that contains the necessary drives for serial data transmission and receiving. It converts voltage level adaptable to the RS-232 signal voltage levels to TTL logic. It should be noted that the MAX232 (A) is just a driver/receiver. It does not generate the necessary RS-232 sequence of marks and spaces with the right timing, it does not decode the RS-232 signal, it does not provide a serial/parallel conversion. All it does is to convert signal voltage levels.

 

Hyper Terminal
Hyper Terminal (also known as Hyper Term) is a communications and terminal emulation program that comes with the Windows operating system.It can be used to exchange files between two PCs or btweeen a PC and other electronic device like controllers using serial communication. It should be used for fairly simple communication but not for complex tasks.

Schematic diagram

Following figure shows the schematic of serial communication between PIC microcontroller and PC.

Schematic for serial communication between PIC microcontroller and PC

To perform serial communication between PIC microcontroller and PC in simulation only, virtual hyper terminal is connected to microcontroller specified for serial communication. Those pins are TX (RC6) for transmission and RX (RC7) for receiving data from PC.

For Hardware, MAX 232 IC is connected to the controller as shown in the figure. Than DB9 port of the PC can be used to send and receive data. Hyper terminal of the operating system can be used to view and send data to microcontroller the same way as virtual hyper terminal is used in simulation.

Simulation results

To verify our project, a simple calculator program is written which will take values from the user through PC and it will display the result. Taking input from user will verify the receiving of data from PC while displaying the result will confirm the sending of data to PC. So in this way both ways of communication is illustrated. The calculator performs four simple mathematical operations that are addition, subtraction, multiplication and division. First input takes the first number, second input takes second number and third inputs ask for mathematical function to be performed. Mathematical functions addition, subtraction, multiplication and division can be entered in terms of its symbols which are + – * & / respectively. Following figure shows the simulation results which verify communication between PIC microcontroller and PC via calculator.

simulation results of communication between PIC microcontroller and PC

It can be seen that the calculator asks for two numbers and operand and displays the correct result.

Program for serial communication between PIC Microcontroller and PC

The program is written in C language using MIKRO C compiler.

#define Enter 13
#define Plus ‘+’ // define + symbol for mathematical operation of addition
#define Minus ‘-‘ // define – symbol for mathematical operation of subtraction
#define Multiply ‘*’ // define * symbol for mathematical operation of multiplication
#define Divide ‘/’ // define symbol for mathematical operation of division
// This function is for carriage-return and line-feed to UART
// when user presses the enter it should stop writing in the current line and move to the next one
void Newline() // switch to new line
{
UART1_Write(0x0D);
UART1_Write(0x0A);
}
void Text_to_Uart(unsigned char *m) // This function sends a text to UART
{
   unsigned char i;
   i=0;
   while(m[i]!=0)
   {
     UART1_Write(m[i]);
     i++;
           }}
void main() {
           unsigned char MyKey,i,j,kbd[5],op[12]; // defining characters
           unsigned long Calc, Op1, Op2, Key;
           unsigned char msg1[]=”Calculator”; // messages to be used during operation
           unsigned char msg2[]=”Enter 1st no.:”;
           unsigned char msg3[]=”Enter 2nd no.:”;
           unsigned char msg4[]=”Enter operation:”;
           unsigned char msg5[]=”Result=”;
             UART1_init(9600); // 9600 baud rate is selected for serial communication with computer
           for(;;) // infinite loop
           { // initilaizing variables with 0
           MyKey=0;
           Op1=0;
           Op2=0;
           Newline(); // go to next line
           Newline();
           Text_to_Uart(msg1); // send the characters stored in msg1 to computer
           Newline();
           Newline();
           Text_to_Uart(msg2); // send the characters stored in msg2 to computer
//Get character
           do
           {
               if(UART1_Data_Ready()) // if system is ready to read data from computer via UART
             {
               MyKey=UART1_Read(); //Read the character
               if(MyKey==Enter)break; // break the loop if enter is pressed
             UART1_Write(MyKey); //Echo the character means display the data which is read from the computer
               Key=MyKey-‘0’; // remove null char and save the data of char in long variable
             Op1=10*Op1+Key; //conversion ASCII to intiger
             }}
             while(1);
           Newline();
             Text_to_Uart(msg3); // send the characters stored in msg1 to computer
           do
           {
                     if(UART1_Data_Ready())
                   {
                     MyKey=UART1_Read();
                     if(MyKey==Enter)break;
                     UART1_Write(MyKey);
                     Key=MyKey-‘0’;
                   Op2=10*Op2 + Key;
                   }}
while(1);
                   Newline();
                     Text_to_Uart(msg4);
                   do
                   {
                     if(UART1_Data_Ready())
                   {
                     MyKey=UART1_Read();
                     if(MyKey==Enter)break;
                     UART1_Write(MyKey);
                   Key=MyKey;
                   }}
                   while(1);
                   Newline();
                     switch(Key) // Select the operation entered by the user
                   case Plus: //   if + sign is pressed
                     Calc=Op1+Op2; // add the two number
                   break; // break the loop
                   case Minus: //   if – sign is pressed
                     Calc=Op1-Op2; // subtract the two number
                   break;
                   case Multiply:
                     Calc=Op1*Op2;
                   break;
                   case Divide:
                     Calc=Op1/Op2;
                   break;
                   }
                     LongToStr(Calc,op); // converting variable to character for display
                   j=0;
                   for (i=0;i<=11;i++) //if a blank character
                   {
                     if(op[i]!=’/’) // if the value stored in op[i] is not free space
                   {
                     kbd[j]=op[i]; // store the value of op[i] in kbd[j]
                   j++;
                   }}
                     Text_to_Uart(msg5);
                   for (i=0;i<j;i++)UART1_Write(kbd[i]); // display the answere stored in chr kbdss
                   } }

 

About Syed Noman ud din

Syed Noman ud din is an Electrical Engineer and working in Industry from last 3 years. He writes technical articles for electrical and electronic engineers. He has also published several research publications in renowned international journals.

Check Also

Power Factor measurement using Microcontroller

The article will help the readers to use microcontroller for power factor measurement using a …

Leave a Reply

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

*