How to make PIC to PIC Communication using UART

Microcontroller Topics
Post Reply
User avatar
Shehani
Lieutenant
Lieutenant
Posts: 61
Joined: Mon Aug 19, 2013 2:11 pm

How to make PIC to PIC Communication using UART

Post by Shehani » Mon Nov 25, 2013 2:29 pm

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
1.png
1.png (9.36 KiB) Viewed 6110 times
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
2.png
2.png (7.35 KiB) Viewed 6110 times
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

3.png
3.png (14.53 KiB) Viewed 6110 times
In Simplex Transmission, data is transmitted only in one direction.

Half Duplex
4.png
4.png (13.26 KiB) Viewed 6110 times
In Half Duplex Transmission, data can be transmitted in both directions but to one side at a time.

Full Duplex
5.png
5.png (14.7 KiB) Viewed 6110 times
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 registers as MikroC Pro for PIC Microcontrollers have built-in library function to handle asynchronous communication.

Registers of USART

To use the USART of PIC 16F877A Microcontroller, the following registers must be configured. But when using MikroC we don’t want to bother about it.

TXSTA : TRANSMIT STATUS AND CONTROL REGISTER
TXSTA Register PIC Microcontroller
TXSTA Register PIC Microcontroller
6.png (4.75 KiB) Viewed 6110 times
CSRC : Clock Source Select

Asynchronous Mode
Don’t care.

Synchronous Mode

1 – Master Mode (clock generated internally)
0 – Slave Mode (clock from external source)

TX9 : 9-bit Transmit Enable

1 – 9-bit transmission
0 – 8-bit transmission

TXEN : Transmit Enable

1 – Transmit Enabled
0 – Transmit Disabled
SREN/CREN (in RCSTA register) overrides TXEN in Sync mode.

SYNC : USART Mode Select

1 – Synchronous Mode
0 – Asynchronous Mode

BRGH : High Baud Rate Select

Asynchronous Mode

1 – High speed
0 – Low speed

Synchronous Mode
Not Used

TRMT : Transmit Shift Register Status

1 – Transmit Status Register empty
0 – TSR full

TX9D : 9th bit of Transmit Data or Parity bit

RCSTA : RECEIVE STATUS AND CONTROL REGISTER
RCSTA Register PIC Microcontroller
RCSTA Register PIC Microcontroller
7.png (4.56 KiB) Viewed 6110 times
SPEN : Serial Port Enable

1 – Serial port is enabled
0 – Serial port is disabled

RX9 : 9-bit Receive Enable

1 – Sets 9-bit reception
0 – Sets 8-bit reception

SREN : Single Receive Enable

Asynchronous Mode
Not Used.

Synchronous Master Mode

1 – Enables single receive
0 – Disables single receive
This bit will be cleared after reception.

Synchronous Slave Mode
Not Used.

CREN : Continuous Receive Enable

Asynchronous Mode

1 – Enables continuous receive
0 – Disables continuous receive

Synchronous Mode

1 – Enables continuous receive till enable bit CREN is cleared (Note : CREN overrides SREN)
0 – Disables continuous receive

ADDEN : Address Detect Enable


Asynchronous 9-bit Mode

1 – Enables address detection
0 – Disables address detection

FERR : Framing Error

1 – Framing error
0 – No framing error

OERR : Overrun Error


1 – Overrun error
0 – No overrun error

RX9D : 9th bit or Parity Bit of Received Data


Now I am not writing about configuring these registers as MikroC Pro for PIC Microcontroller has built in function to handle Asynchronous Communication.


Circuit Diagram

PIC to PIC Communication using USART
PIC to PIC Communication using USART
8.png (157.44 KiB) Viewed 6110 times
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

Code: Select all

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

Code: Select all

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,
    }
   }
}


Article courtesy of electrosome.com
Post Reply

Return to “Microcontrollers”