Jan 9, 2017

How to use 4x4 matrix keyboard along with 16x2 character LCD

In this article, we will discuss the main program between relation of LCD and keyboard. For basic 16x2 character LCD article, visit this link and for basic 4x4 matrix keyboard visit this link.
This program is divided into 4 parts
  1. main program
  2. LCD program
  3. Keyboard program
  4. software delay program
In software delay program we are just defining software delay which will be used in all other 3 programs. LCD program deals with the required functions related to initialization of LCD, LCD commands and writing functions to LCD. Keyboard program deals with the checking of which key is pressed and main program has the code for task which should be carried.
in main code, we first check which key is pressed and for each key we can assign a specific task through switch case. Here we are printing the spelling of each pressed key on LCD. Each .c program has its own .h header for required definitions.
Follow the following code for main program.

 //header files  
 #include<reg51.h>  
 #include"lcd.h"  
 #include"kbd.h"  
 #include"delay.h"  
   
 void process();  
   
 int main(void)  
 {  
      init_LCD();  
      while(1)  
      {  
           process();  
      }  
 }  
   
 void process()  
 {  
      unsigned char key;  
        
      //check if any key is pressed  
      key=getkey();  
        
      //check entered number  
      switch(key)  
      {  
           case 1:  
                //clear screen  
                wrcmd(0x01,0);  
             
                //move cursor to line 1's start  
                wrcmd(0x80,0);  
                  
                //display entered number  
                write_to_LCD("one");  
             
                //minimum delay between 2 entries  
                delay(200);  
                break;  
             
           case 2:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("two");  
                delay(200);  
                break;  
             
           case 3:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("three");  
                delay(200);  
                break;  
             
           case 4:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("four");  
                delay(200);  
                break;  
             
           case 5:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("five");  
                delay(200);  
                break;  
             
           case 6:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("six");  
                delay(200);  
                break;  
             
           case 7:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("seven");  
                delay(200);  
                break;  
             
           case 8:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("eight");  
                delay(200);  
                break;  
             
           case 9:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("nine");  
                delay(200);  
                break;  
             
           case 10:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("ten");  
                delay(200);  
                break;  
             
           case 11:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("eleven");  
                delay(200);  
                break;  
             
           case 12:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("twelve");  
                delay(200);  
                break;  
             
           case 13:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("thirteen");  
                delay(200);  
                break;  
             
           case 14:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("fourteen");  
                delay(200);  
                break;  
             
           case 15:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("fifteen");  
                delay(200);  
                break;  
             
           case 16:  
                wrcmd(0x01,0);  
                wrcmd(0x80,0);  
                write_to_LCD("sixteen");  
                delay(200);  
                break;  
             
           default:  
                break;  
      }  
 }  
   

Interfacing of 8051 microcontroller with 16x2 character LCD and 4x4 matrix keyboard
8051 with LCD and Keybord
Output video:



Note : this is not the optimum code. You can use function for doing same task in for wrcmd functions used in each of switch case but here I want to show you different tasks hence I have written separate code for each case

No comments:

Post a Comment