Spread the love

keypad interfacing with PIC18F458

____________________________________________________________________________________________________

  • What is keypad(keyboard) ? – What you probably have in front of you, is a keyboard with more than 100 keys on it… If you are not familiar with the key matrices, then you may think that inside this keyboard, there is a chip that has at least the same number of inputs to read each key separately. Well, this is not true…

l5JDN

  • What are the key matrices?–   The matrices are actually an interface technique. According to this technique, the I/O are divided into two sections: the columns and the rows. You can imagine a matrix as an excel sheet. Here is a 4 x 4 matrix. The A,B,C and D are the columns and the 1,2,3 and 4 are the rows. There are 16 knots that the rows and columns intersect. To make a keypad matrix, we will have to connect a switch to each knot. The switch will have a push-to-make contact. When the operator pushes this switch, it will connect the column and the row that it corresponds to.

keypad-schematic

  • How does keypad matrix works? –   To understand the operation principle, i will re-draw the above matrix without colors as you can see on above pic. I will also put connection switch to each row and column wire and show practical hardware pic. For the above 16-button 4×4 matrix, 8 pins of the microcontroller will be used. The first 4 pins will be OUTPUTS (gives logic ‘1’)  and will be connected to the COLUMN wires, while the other 4 pins will be INPUTS (logic ‘0’) and will be connected to the ROW wires. The matrix is controlled by a microcontroller. The OUTPUTS of the microcontroller will NOT all have power at the same time. The outputs will go high one by one in cycle. This happens many times per second.  When operator press switch 11 (highlighted in red color), column C and row 3’s connection established, current pass from column C to row 3, and in actual sense row 3 read logic ‘1’ from column D and microcontroller manipulate this connection and do work as we decided in programming.

1332_MED

  • Applications –  As you can see on above pics keypad is used in ATMs, cell phones and where multi parameter want to input.

1. Circuit Diagram Of  LCD and KEYPAD  Interfacing With  PIC18F458

Circuit Diagram Of  LCD and KEYPAD  Interfacing With  PIC18F458

1. Program of LCD and KEYPAD interfacing with PIC18f458

/******************************************************
www.firmcodes.com
DEVELOPED BY:- FIRMWARE DEVELOPER
WHAT PROGRAM DO:- DATA ENTERED FROM KEYPAD DISPLAYED ON LCD CONNECTED WITH PIC18F458
******************************************************/

/* header file used in this program is already included in software microC pro for pic*/

#define rs portb.RB4
#define rw portb.RB5
#define en portb.RB6
#define lcd portd
#define r1 portc.RC0
#define r2 portc.RC1
#define r3 portc.RC2
#define r4 portc.RC3
#define c1 portc.RC4
#define c2 portc.RC5
#define c3 portc.RC6
#define c4 portc.RC7

int keypad();

void lcd_ini();

void lcd_display(unsigned int);

void cmd(unsigned char);

void lcd_str(unsigned char*);

unsigned int i;
unsigned int m;

void main() 
   {
        trisb=0x00;
        trisc=0x00;
        trisd=0x00;
        lcd_ini();
        while(1)
          {  
               keypad();
               lcd_display(m);

          }
    }

void lcd_display(unsigned int x)
  {
      lcd=x;
      rs=1;
      rw=0;
      en=1;
      delay_ms(100);
      en=0;
  }

void cmd(unsigned char m)
  {
      lcd=m;
      rs=0;
      rw=0;
      en=1;
      delay_ms(10);
      en=0;
  }

void lcd_ini()
  {
      cmd(0x38);
      cmd(0x0e);
      cmd(0x01);
      cmd(0x06); 
      cmd(0x80);
  }

void lcd_str(unsigned char *str)
   {
        while(*str!='\0')
          {
               lcd_display(*str);
               str++;
          }
    }

int keypad()
   {
        portc=0xff;
        while(1) 
          {
              r1=0;
              r4=r3=r2=1;
              if(c1==0)
                 {
                      m='7';
                      delay_ms(10);
                      return m;
                  }
              else if(c2==0)
                {
                     m='8';
                     delay_ms(10);
                     return m;
                }
             else if(c3==0)
                {
                     m='9';
                     delay_ms(10);
                     return m;
               }
           else if(c4==0)
              {
                    m='/';
                    delay_ms(10);
                    return m;
              }
           r1=r3=r4=1;
           r2=0;
           if(c1==0)
              {
                   m='4';
                   delay_ms(10);
                   return m;
               }
           else if(c2==0)
              {
                   m='5';
                   delay_ms(10);
                   return m;
              }
           else if(c3==0)
              {
                   m='6';
                   delay_ms(10);
                   return m;
               }
            else if(c4==0)
               {
                   m='*';
                   delay_ms(10);
                   return m;
               }
            r2=r1=r4=1;
            r3=0;
            if(c1==0)
              {
                  m='1';
                  delay_ms(10);
                  return m;
              }
           else if(c2==0)
              {
                  m='2';
                  delay_ms(10);
                  return m;
              }
           else if(c3==0)
              {
                  m='3';
                  delay_ms(10);
                  return m;
              }
           else if(c4==0)
              {
                  m='-';
                  delay_ms(10);
                  return m;
              }
           r3=r2=r1=1;
           r4=0;
           if(c1==0)
              {
                  cmd(0x01);
                  delay_ms(500);
              }
           else if(c2==0)
              {
                    m='0';
                    delay_ms(10);
                    return m;
               }
            else if(c3==0)
               {
                   m='=';
                   delay_ms(10);
                   return m;
               }
            else if(c4==0)
              {
                  m='+';
                  delay_ms(10);
                  return m;
              }
         }
   }

PROTEUS File for SIMULATION(Password Of RAR file is :-firmcodes.com)

download (1)

Content for the tab VIDEO