PIC to PIC Communication using UART

PIC to PIC Communication using UART

Introduction

PIC to PIC communication will be needed in some embedded applications. We have two options to transmit data through transmission lines.

  • Parallel Transmission
  • Serial Transmission

Parallel Transmission

Parallel Data Transmission

Parallel Data Transmission

In parallel communication an entire byte of data is transmitted at a time. That is each bit has dedicated line. Thus for 8-bit data transfer we need 8 dedicated lines as shown above.

Serial Transmission

Serial Data Transmission

Serial Data Transmission

In Serial Transmission only one bit of a byte is transmitted at a time. There is only one communication line, thorough which bits are transmitted sequentially.

Data can be transmitted using Parallel or Serial techniques, as the pros and cons of two methods are equal and the selection depends on application. Parallel Transmission is very fast compared to serial transmission, as it transmits a byte at a time. Serial Transmission is cost effective as compared to Parallel Transmission as it requires only one line for transmission.


Transmission Systems are also classified into 2 on the basis of transmission synchronization.

  • Synchronous Transmission
  • Asynchronous Transmission

Need for Synchronization

When an electronic device transmits data to other there must be certain synchronization between them, ie the receiving device must have a way to know the beginning and end of each unit (byte) of data.

Synchronous Transmission

Synchronous Transmission are synchronised using a clock line, ie the communications are time synchronised. An external clock line is also used along with data line to synchronize transmission and reception ends.

Asynchronous Transmission

There is no separate clock line in this system. Transmitter and Receiver works on separate clocks. Start and Stop bits are also send along with data to identify start and end of a byte.

We can transmit data in three ways.

  • Simplex
  • Half Duplex
  • Full Duplex

Simplex

Simplex Transmission

Simplex Transmission

In Simplex Transmission, data is transmitted only in one direction.

Half Duplex

Half Duplex Transmission

Half Duplex Transmission

In Half Duplex Transmission, data can be transmitted in both directions but to one side at a time.

Full Duplex

Full Duplex Transmission

Full Duplex Transmission

In Full Duplex Transmission data can be transmitted simultaneously in two directions.

USART – Universal Synchronous Asynchronous Receiver Transmitter

USART is the most commonly used serial I/O module. It is also known as Serial Communications Interface (SCI). USART can be easily configured as a full-duplex asynchronous communication system that can communicate with peripheral devices, such as personal computers and CRT terminals, or it can be configured as a half-duplex synchronous communication system that can communicate with peripheral devices, such as serial EEPROMs,  A/D or D/A integrated circuits, etc. USART can be configured in the following modes.

  • Synchronous Master – Half Duplex
  • Synchronous Slave – Half Duplex
  • Asynchronous – Full Duplex

We don’t want to bother about configuring USART registers as MikroC Pro for PIC Microcontrollers have built-in library function to handle asynchronous communication.

Circuit Diagram

PIC to PIC Communication using USART

PIC to PIC Communication using USART

The above circuit can demonstrate the PIC to PIC  Communication using USART. Here we are using Asynchronous communication. The switch status read by the first PIC is transmitted to the second PIC and displayed using LED’s.

MikroC Code

 Transmitter

void main()
{
  TRISB = 0xFF;
  PORTB = 0;
  UART1_Init(9600); // Initialize UART module at 9600bps
  Delay_ms(100); // Wait for UART module to stabilize
  while (1)
  { // Endless loop
    UART1_Write(PORTB); // and send data via UART
    Delay_ms(500);
  }
}

Receiver

void main() 
{
  TRISB = 0;
  PORTB = 0;
  UART1_Init(9600); // Initialize UART module at 9600bps
  Delay_ms(100); // Wait for UART module to stabilize
  while (1) 
  { // Endless loop
    if (UART1_Data_Ready()) 
    { // If data is received,
      PORTB = UART1_Read(); // read the received data,
    }
   }
}

Download Here

You can download MikroC files and Proteus files here..
PIC to PIC Communication using UART

Share this post

Comments (49)

  • lalala

    can you update your article with mplab and assembler codes? 🙂

    December 3, 2012 at 4:51 pm
  • Ligo George

    I will update…it after some days…..Now I am busy……..

    December 3, 2012 at 7:08 pm
  • lalala

    I would be grateful if you can do that in this week 🙂

    December 3, 2012 at 8:48 pm
  • lalala

    can you put this code in few days? I really need this, please, this would be great 🙁

    December 10, 2012 at 2:43 pm
  • Ligo George

    I will try……….. I am not yet used mplab….I am going to start learning it now…

    December 10, 2012 at 6:08 pm
  • Ligo George

    Do you need complete assembly language code or Hi-Tech C code?

    December 10, 2012 at 6:22 pm
  • Ligo George

    Why don’t you use a high level language like MikroC……………??

    Now most of the peoples are using high level languages.. for project developing.. as it don’t require more hardware knowledge and saves time….

    December 10, 2012 at 6:51 pm
  • lalala

    mplab/assembler 🙂 I cannot use MicroC, because I need this code only in these programming languages

    December 10, 2012 at 8:19 pm
  • lalala

    how long does it takes to write code in mplab for you?

    December 11, 2012 at 7:35 pm
  • lalala

    are you forgot about this project? :(((

    December 16, 2012 at 7:08 pm
  • Ligo George

    Sorry, I didn’t forgot…….. I have no time….. I don’t know the abcd of pic assembly language programming…. So it is a time consuming process for me….Please ask this question in edaboard , some assembly language experts may help you soon …

    December 16, 2012 at 7:42 pm
  • Rohan Amin

    Does this work with two PIC16F877A?

    April 5, 2013 at 1:35 pm
  • Ligo George

    Yes……….the above tutorial is with two pic 16f877a.

    April 5, 2013 at 3:50 pm
  • pra

    sir ,i build this code with Mikro c Pro and it is not succesfully build,it show error:
    undeclire UART1 error.What is the problem? please post yout proteus file and source code.

    May 10, 2013 at 12:32 pm
  • Ligo George

    Make sure that your are using MikroC Pro..
    If you are using mikroC pro.. then you shouldn’t include UART library..
    goto View >>Library Manager … for that..

    I posted the proteus files and mikroc fies as per your request.. you can download it from the end of this article..

    May 10, 2013 at 12:58 pm
  • pra

    sir,can you help for this pic to pic communication done in mikro c pro.if any way to done this in mikroc pro? please help.

    May 10, 2013 at 1:20 pm
  • pra

    sir,can i use this for any microcontroller. sorry for my last question.

    May 10, 2013 at 2:30 pm
  • Ligo George

    Programming are done in mikroc pro..itself…

    May 10, 2013 at 4:15 pm
  • Ahmed M. Hassan

    Hello, THANK you sir for this GREAT work, I have been looking for EXACTLY the same project with exactly code, thanks a lot for spending time teaching people good things.

    November 24, 2013 at 3:22 am
  • Ligo George

    Thanks for the feedback..

    November 24, 2013 at 10:31 pm
  • Ahmed M. Hassan

    Worked with me like charm 🙂 Thanks a million

    November 25, 2013 at 5:12 pm
  • Mhyli Jade Cereneo

    do you have pic to pic communications using uart with lcd and keypad? thanks

    September 15, 2014 at 10:08 pm
  • Ligo George

    No, but you can develop it easily. .similar to above example.

    September 17, 2014 at 8:17 am
  • Shakil Ahmed

    Many Many thanks sir for your Nice Explanation.

    December 19, 2014 at 11:24 am
  • Ligo George

    Thanks for the feedback.

    December 22, 2014 at 12:25 pm
  • Ananth Boss

    hi sir i wrote program in MikroC….no error and its works fine in proteus too….but when i test in breadboard no result…even no voltage at RC7(RX) and RC6(TX)…pls help soon as possible..i have only two weeks to submit my project..

    April 7, 2015 at 8:08 am
  • Ananth Boss

    Sir need your help…im doing program for pic to pic communication using RF Module(RS433MHz)…i successfully wrote program and its works fine in proteus simulator….but its not works in breadboard….pls help me soon as possible i have only two weeks to submit my project

    April 7, 2015 at 8:47 am
  • Ligo George

    Voltage in RC7 and RC6 may vary…. .you need a CRO for evaluating signals on those lines.
    Above program is 100% working.. You should make GND of both devices common and make sure that RX is connected to TX of other device.

    April 13, 2015 at 10:30 am
  • Ligo George

    For using ASK RF modules you should reduce the baud rate to lowest possible value. .. otherwise it won’t work..

    April 13, 2015 at 10:31 am
  • Ananth Boss

    Thabk you very much sir….Actually forgot to put delay after UART…..now its working… 🙂

    April 16, 2015 at 6:51 am
  • Ananth Boss

    i have another question..how to use this receiver pic to detect metal..how to add two while loop in same program..help me pls..

    April 16, 2015 at 6:52 am
  • Ligo George

    Two while loop means ?

    April 27, 2015 at 10:03 pm
  • Ananth Boss

    hi sir..my uart suddenly stop working..before this its works…i have check my crystal and transmitter and microcontroller all working good.but i cannot transmit or receive anything..pls help me sir..

    June 24, 2015 at 8:31 pm
  • Ligo George

    Sorry, I am unable to find the exact reason without sufficient information. Try changing ICs.

    July 1, 2015 at 7:27 pm
  • jeff

    hello please i’ve some problem with this tutorial.

    i’ve realised it with the pic 16F628A. but i have some problem in my simulation on proteus.

    Proteus warning me: ” logic contention(s) detected on net #0003″. and the programm simulation don’t work.
    please help me

    December 6, 2015 at 7:53 pm
  • jeff

    Hello please i need some help with the tutorial “PIC TO PIC COMMUNICATION USART”
    i’ve done it with pic 16F628A.
    but proteus warning me : “logic contention(s) detected on net #0003”;
    and the programm don’t work.
    please help me to resolve my problem
    Thanks.

    December 6, 2015 at 7:59 pm
  • Ligo George

    It seems like you connected TX to TX and RX to RX INSTEAD OF TX to RX and vice versa.

    December 19, 2015 at 3:46 pm
  • srikanth sailesh

    hello sir,
    i am using 2 pic 18f45k22 controllers which communicate which each other in serial,asychronous full duplex mode.
    One UC sends 3 bytes data on receiving which the 2nd UC should send 8 bytes of data to it in return, through serial tx and rx. I have compiled it (in mikroc) and works fine. when i simulate in proteus, the second controller’s 8 data bytes are visible in virtual terminal. but after connecting it to RX of 1st UC they are not appearing in VT. But I can see 8 bits transmitting in the line when i see through oscilloscope. I am not sure about what data is passing. I can’t understand where i have got wrong. Please tell me if i have missed something in the procedure.

    February 18, 2016 at 7:45 pm
  • Ligo George

    May be due to baud rate mismatch.

    February 27, 2016 at 7:58 pm
  • Jaafer Mansouri

    hi this programme work but in industrie it is not securité to order motors i choud integret this informations in a trame ID binaire for example (1111) and in the end (00) and between then i make portb and i say for the pic recever iformations is (1111portb00) do portb in the metre pic =portb in the sclave pic (sorry im not good in english) i need ur help in programaation in mikroc

    March 7, 2016 at 4:52 pm
  • Sohail

    please send me the code for sending and receiving characters for PIC to PIC serial communication.

    June 18, 2016 at 11:09 am
  • Ligo George

    Sorry we don’t have any codes for that.

    Kindly use our forums ( https://electrosome.com/forums/ ) for asking doubts outside the scope of above article.

    August 27, 2016 at 12:28 pm
  • vsathyan

    If I have two RF receivers, how do i make sure that the data is received by only one of the receiver?
    (without using HT12E/12D chips). Thanks

    October 20, 2016 at 9:25 pm
  • Ligo George

    You need to use two separate frequencies.
    Otherwise you can use a duty cycle/frequency based transmission such that it won’t interfere each other.

    November 27, 2016 at 1:21 pm
  • eniyavan

    sir i also want that program kindly send me plz

    January 19, 2017 at 1:19 pm
  • Payal Gandhi

    Hello Sir,
    I am a beginner. I’m using MPLABX IDE 8.92.
    I’m trying to write a code to interface gsm 800A with PIC16F877A. i wrote it and in the code i called uart1_write and uart1_init functions but the problem is i’m unable todefine these 2 functions.Please help me.

    April 28, 2017 at 3:33 pm
  • Ádám Szemán

    Hello
    I get this error: Error – could not find definition of symbol ‘UART1_Read’ in file ‘./project.o’.
    Can someone help?

    January 27, 2018 at 9:02 pm
  • vikram singh

    MPLAB IDE natively doesn’t support these functions .You have to create them in your program.For info on how to create one refer book …..

    Programming PIC Micro-controllers with XC8

    By Armstrong Subero

    August 10, 2018 at 9:39 am
  • Tony Nguyen

    Help me!!!
    I cannot receive and send data at the same time. Do you have any solution?

    December 28, 2018 at 1:04 am

Leave a Reply

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