Header Ads

USART Text Communication using PIC Microcontroller and Mikro C

In this tutorial, we will learn how to "text" communication using the USART module of PIC Microcontroller. For "text" communication we have created an example. In this example we will do "text" communication between two microcontrollers. Using the second microcontroller, we will send the "text" to the display attached to the first microcontroller.  You can watch the following video or read the written tutorial below.




USART Module :

Now let's take a 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 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 USART Asynchronous mode and Simplex data transmission.


USART Asynchronous mode:

In this mode, the USART uses standard non-return-to-zero (NRZ) format (one START bit, eight or nine data bits, and one STOP bit). The most common data format is 8-bits. An on-chip, dedicated, 8-bit baud rate generator can be used to derive standard baud rate frequencies from the oscillator. The USART transmits and receives the LSb first. The transmitter and receiver are functionally independent, but use the same data format and baud rate. 





Simplex:
In Simplex data transmission, data is transmit Sender to Receiver.

Circuit schematic:

Here's the circuit schematic. The circuit schematic is very quick sample. It has two parts. One is Receiver and other is Sender. The LCD display is connected to the PORTB of the Receiver. Here the TX pin of the Sender is connected to the RX pin at the Receiver. 





Sender Mikro C code:


void main() {
     UART1_Init(9600);        // Initialize usart module at 9600kbps
     delay_ms(100);           // wait for staiblizedule
     while(1){
              UART1_Write_Text("Welcome To ok");
              delay_ms(500);
              UART1_Write_Text("MINA TECHNOLOGY ok");
              delay_ms(500);
              }
              
}

Code Explain:

Here's the Mikro C code for Sender. The code is very quick simple. In this code we have included UART library. 

In main section, we have to need initialized the USART module at 9600 Kilo bits per second ( Kbps). Here's the 100 millisecond ( ms ) delay which stabilize the USART module. 

In the main loop, we will send "Welcome To ok " text to the receiver by UART1_Write_Text ( ) function. Here, we set "ok" as "delimiter". When we send a text we have to send a "delimit" with the tax, which the receiver accepts and compares with the "delimit" set earlier. After 500 millisecond we will send another new text.

Receiver Mikro C Code:

// Lcd module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End Lcd module connections
char output[20];
void main() {
     UART1_Init(9600);         // Initialize the USART module at 9600Kbps
     delay_ms(100);          // wait for usart module staiblized
     Lcd_Init();              // Initialize the LCD module
     Lcd_Cmd(_LCD_CLEAR);     // Clear the LCD display
     Lcd_Cmd(_LCD_CURSOR_OFF);    // Cursor off the LCD display
     Lcd_Out(1,1,"Developed By");
     Lcd_Out(2,1,"MINA TECHNOLOGY");
     delay_ms(2000);
     LCD_Cmd(_LCD_CLEAR);
     while(1){
              if(UART1_Data_Ready()){
                                     UART1_Read_Text(output, "ok", 20);
                                     Lcd_Cmd(_LCD_CLEAR);
                                     LCD_Out(1,1,output);
                                     }

              }


}
Code Explain:

Here's the Mikro C code or Receiver.  In this code we have included UART  and LCD library. 
In define section, we have defined LCD module connections . The LCD display module is connected to PORTB. We need a character array whose length will be 20. Which will contain the text send from the transmitter.

In main section, we have to need initialized the USART module at 9600 Kilo bits per second ( Kbps). Here's the 100 millisecond ( ms ) delay which stabilize the USART module. Next, we have initialized the LCD module. We have written some command for LCD module.

In the main loop section, we have to need if statement which will check the receive data. If data is received then the statement will be true. We will receive text using the UART1_Write_Text ( )  function. I set ok as the limit in the function and we have set 20 as an "attempt". The function will search a maximum of 20 times until it finds ok in the received text. Next, we have cleared the LCD display and I will show the text on the LCD display.



Click the download button for source code:


No comments

Theme images by 5ugarless. Powered by Blogger.