Header Ads

UART PIC to PIC communication and Mikro C

In this tutorial, we will learn how to communicate data between two microcontrollers. We will do UART data communication here using the PIC microcontroller  
You can watch the following video or read the written tutorial below.





Let see the closer look at the USART module:

The Universal Synchronous Asynchronous Receiver Transmitter ( USART )  module is one of the two serial  I/O modules.
USART is also known as a Serial Communication Interface or SCI. The USART can be configured as a full duplex asynchronous system that can configured as a half duplex synchronous system that can communicate with peripheral devices such as CRT terminals and personal computer, or it can be configured as a half duplex synchronous system that can communicate with peripheral device such as A/D or D/A integrated circuits, serial EEPROMs etc.
The USART can be configured in the following modes:
  • Asynchronous ( Full duplex )
  • Synchronous - Master ( Half duplex )
  • Synchronous - Slave ( Half duplex )
In this tutorial we have used Synchronous Half duplex data transmission for USART communication. 





In Synchronous Transmission, data is sent in form of blocks or frames. This transmission is the full duplex type. Between sender and receiver the synchronization is compulsory. In Synchronous transmission, There is no gap present between data. It is more efficient and more reliable than asynchronous transmission to transfer the large amount of data.



Simplex:

In Simplex Transmission, data is transmitted only in one direction. Data will be transmit Terminal 1 to terminal 2 or Terminal 2 to Terminal 1.


Circuit schematic:

This is our circuit schematic. It has two parts once for transmitting data and other for receiving data. We are connected 5 Leds with Receiver and 5 push Button with Sender. If we press any button the specific LED will be activate. 



Receiver Code:


unsigned char uart_rd;
void main() {
     UART1_Init(9600);   // Initialize UART at 9600Kbps
     Delay_ms(100);      // Wait for UART module to stabilize
     TRISB = 0X00;       // PORTB is an output PORT
     PORTB = 0X00;       // Clear the PORTB register
     uart_rd = 0;
     while(1){
              if(UART1_Data_Ready()){     // If data is received
                      uart_rd = UART1_Read();      // read the received data
                      PORTB = uart_rd;        //  received show PORTB
                      }

              }


}

Sender Code Explain:

Here's the Sender code. We have wrote the code in mikro C pro for PIC compiler. Here's we are included UART library. 
In define section we have defined unsigned character variable which mane is uart_rd .
In main section we have defined UART module at 9600 Kilo bits per seconds (Kbps ). We have to need 100 ms (millisecond ) delay which stabilize the UART module. Here's we have set the PORTB is an output port by clearing TRISB register. Now, we have to clear the PORTB register value. Here, we have set the value of uart_rd veritable is 0.
In main loop we have created a if statement which check the received data. Here's the UART1_Data_Ready( ) function to test if data in receive buffer is ready for reading. The UART1_Data_Ready( ) function return 1 if data is ready for reading and otherwise 0 if there is no data in received register. Using the UART1_Read() function you have to read the received data and put that value into the variable “uard_rd”. We have to write the uard_rd variable value in PORTB.  

Sender Code:
unsigned char uart_wr;
void main() {
     UART1_Init(9600);      // UART initialize at 9600Kbps
     Delay_ms(100);         // Wait for UART module to stabilize
     TRISB = 0XFF;          // PORTB is an input PORT
     PORTB = 0X00;          // Clear the PORTB
     uart_wr = 0;
     while(1){
              uart_wr = PORTB ;     // Read the PORTB data
              UART1_Write(uart_wr);
              }


}

Sender Code Explain:

This is our simple sender code. In this code we have included UART library. In the define section we have defined a unsigned character variable which name is uart_wr.
In main section we have defined UART module at 9600 Kilo bits per second (Kbps ). We have to need 100 ms (millisecond ) delay which stabilize the UART module. Here's we have set the PORTB is an input port by setting TRISB register. Now, we have to clear the PORTB register value. Here, we have set the value of uart_wr veritable is 0.
In main loop section we have to write PORTB value in uart_wr variable. We will transmit the uart_wr variable value using UART1_Write( ) function.

Click the download button for source code:




No comments

Theme images by 5ugarless. Powered by Blogger.