Header Ads

USART data communication using PIC microcontroller and Mikro C

 In this tutorial, we will learn how to data communication using the USART module of PIC Microcontroller. For data communication we have created an example. In this example we will do data communication between two microcontrollers. There using Matrix Keypad at the second microcontroller  we will control the Lcd display at  the first microcontroller. Here's we have set the first microcontroller as  a receiver and second microcontroller as a transmitter.  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 Asynchronous Simplex data transmission.



In Asynchronous Transmission, data is sent in form of byte or character. This transmission is the half duplex type transmission. In this transmission start bits and stop bits are added with data. It does not require synchronization.





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

Circuit schematic:

Here's the circuit schematic. It has two parts. One is Receiver and other is Sender. The Lcd display is connected to the Receiver and Keypad is connected to the Sender. Here the TX pin of the Sender is connected to the RX pin at the Receiver. In this tutorial we have set C and D button of keypad module for LCD display clear and Shift Cursor. If you press the C button, the LCD will clear. And if you press the D button, the LCD Cursor will shift second row.





Sender Mikro C code:


// Keypad module connection
char  keypadPort at PORTD;
unsigned char kp;
void main() {
     UART1_Init(9600);           // Initialize USART module at 96000kbps
     delay_ms(100);              // Wait for UART module to stabilize
     Keypad_Init();            // Initialize Keypad module
     while(1){
              kp = 0;
              do {
                  // Wait for key to be pressed and released
                  kp = Keypad_Key_Click();     // Store key code in kp variable
                  }while(!kp);
                  // Prepare value for output, transform key to it's ASCII value
                  switch (kp) {
                          case  1: kp = 49; break; // 1
                          case  2: kp = 50; break; // 2
                          case  3: kp = 51; break; // 3
                          case  4: kp = 65; break; // A
                          case  5: kp = 52; break; // 4
                          case  6: kp = 53; break; // 5
                          case  7: kp = 54; break; // 6
                          case  8: kp = 66; break; // B
                          case  9: kp = 55; break; // 7
                          case 10: kp = 56; break; // 8
                          case 11: kp = 57; break; // 9
                          case 12: kp = 67; break; // C
                          case 13: kp = 42; break; // *
                          case 14: kp = 48; break; // 0
                          case 15: kp = 35; break; // #
                          case 16: kp = 68; break; // D
                          case 10: kp = 42; break;  // '*'
                          case 11: kp = 48; break;  // '0'
                          case 12: kp = 35; break;  // '#'
                          default: kp += 48;
                }
              UART1_Write(kp);   // send kp variable value
              }
 
}

Code Explain:

Here's the Mikro C code for Sender. In this code we have included UART library. 
In define section, we have defined Keypad module connection. The Keypad module is connected to PORTD. We have to need a unsigned character variable "kp" that will store the Keypad key value. 

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 Keypad module. 

In the main loop, we have to set the "kp" variable value is 0. We have created do while loop which wait for Keypad key click. Next we have written Switch statement. The UART1_Write ( ) function send the "kp" variable value to the Receiver.

Receiver Mikro C Code:

// Lcd module conncetions
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
unsigned char chr;
void main() {
     UART1_Init(9600);      // Initialize the USART module at 9600kbps
     delay_ms(100);              // Wait for UART module to stabilize
     LCD_Init();         // Initialize the Lcd module
     Lcd_Cmd(_LCD_CLEAR);    // Clear the Lcd display
     Lcd_Cmd(_LCD_CURSOR_OFF);     // Cursor of the display
     Lcd_Out(1,1,"Developed By");
     Lcd_Out(2,1,"MINA TECHNOLOGY");
     delay_ms(2000);
     Lcd_Cmd(_LCD_CLEAR);       // Clear the Lcd display
     chr = 0 ;     // Clear the variable
     while(1){
              if(UART1_Data_Ready()){
                       chr = UART1_Read();
                       if(chr == 67 ){
                              Lcd_Cmd(_LCD_CLEAR);
                              }
                       else if(chr == 68){
                              Lcd_Cmd(_LCD_SECOND_ROW);
                              }
                       else {
                              Lcd_Chr_Cp(chr);
                              }
                       }
              }
     

}

Code Explain:

Here's the Mikro C code for Sender.  In this code we have included UART library. 
In define section, we have defined LCD module connections . We have to need a unsigned character variable "chr" that will store the USART buffer register value. 

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 to write some command for LCD module.

In the main loop section, we have to need if statement which will check the receive data. Using the UART1_Read() function we read and store the data into the “chr” variableNext, we have created if else statement which compare the "chr" variable value with predefined value. If the "chr" variable value is 67 , the LCD will clear. And if the ''chr" variable value is 68, the LCD cursor will shift second row.

Click the download button for source code:


No comments

Theme images by 5ugarless. Powered by Blogger.