Interface Keypad with LPC2148

Hey guys! In the last tutorial we saw 'how to add an input button to microcontroller.' Interfacing a keypad is same, instead we have 12 buttons to deal with. To make it simple we are going to divide 12 buttons in rows and columns.
If you look at the keypad above, We have 4 rows and 3 columns. And we are going to use these rows and columns to interface this keypad with LPC2148.

HOW TO-
The concept here is simple, make the columns as input pins and pull them high by using pull-up resistor. Make one of the rows as output and low(0). Whenever we press any button, current will flow from source (Vcc) to ground and we will know which row and column it occurred at. We can display the result in LCD.
eg- Let's say row B is low and all other rows are high. Now if I press 5, current will flow to the ground i.e. the pin, row B is connected to, and column 2 will become low. We can display 5 on LCD whenever this happens.

Registers to be used-
We only have to use IODIR (direction register), IOSET (for setting the pins) and IOCLR(for clearing the pins) and PINSEL of course for normal GPIO operation.

Some insight into the code

As you can see in the figure, my cloumn1 is connected to P0.0, C2 to P0.1 and so on.
1. Make the row 1 as low and other rows as high
    IOCLR0 |= (1<<3);
    IOSET0 |= (1<<4)|(1<<5)|(1<<6);
2. Check and wait for C1 to go low
    if (!(IOPIN0&(1<<0)))
      {
         while (!(IOPIN0&(1<<0)))      // while the pin is low
            {
                lcd_send_data ('1');           // display 1 on lcd
             }
      }

One important thing guys as now our code will be enlarging, I am not going to write all the functions every time. So in this project, I have included the "lcd.h" file, from previous tutorial, for lcd functions. Make sure you initialize lcd before using it.
CODE-
#include <lpc214x.h>
#include <stdint.h>
#include "lcd.h"

uint8_t keypad (void)
{
 while (1)
{
IOCLR0 |= (1<<3);   // Port0.3=0 (rowA=0)
IOSET0 |= (1<<4)|(1<<5)|(1<<6);    //Port0.4, 0.5, 0.6 =1 (rowB, rowC, rowD =1)
if (!(IOPIN0&(1<<0))) 
{ while (!(IOPIN0&(1<<0))); return'1';}   // if pin0 goes low (if c1 = 0), display 1 on lcd
if (!(IOPIN0&(1<<1)))
{ while (!(IOPIN0&(1<<1))); return'2';}  // if pin1 goes low (if c2 = 0), display 2 on lcd
if (!(IOPIN0&(1<<2)))
{ while (!(IOPIN0&(1<<2))); return'3';} // if pin1 goes low (if c3 = 0), display 3 on lcd
IOCLR0 |= (1<<4);   // Port0.4=0 (rowB=0)
IOSET0 |= (1<<3)|(1<<5)|(1<<6);    //Port0.3, 0.5, 0.6 =1 (rowA, rowC, rowD =1)
    if (!(IOPIN0&(1<<0)))
{ while (!(IOPIN0&(1<<0))); return'4';}  //if pin0 goes low (if c1 = 0), display 4 on lcd
if (!(IOPIN0&(1<<1)))
{ while (!(IOPIN0&(1<<1))); return'5';}  // if pin1 goes low (if c2 = 0), display 5 on lcd
if (!(IOPIN0&(1<<2)))
{ while (!(IOPIN0&(1<<2))); return'6';}  // if pin1 goes low (if c3 = 0), display 6 on lcd
IOCLR0 |= (1<<5);   // Port0.5=0 (rowC=0)
IOSET0 |= (1<<3)|(1<<4)|(1<<6);    //Port0.3, 0.4, 0.6 =1 (rowA, rowB, rowD =1)
    if (!(IOPIN0&(1<<0)))
{ while (!(IOPIN0&(1<<0))); return'7';}  //if pin0 goes low (if c1 = 0), display 7 on lcd
if (!(IOPIN0&(1<<1)))
{ while (!(IOPIN0&(1<<1))); return'8';}  // if pin1 goes low (if c2 = 0), display 8 on lcd
if (!(IOPIN0&(1<<2)))
{ while (!(IOPIN0&(1<<2))); return'9';}  // if pin1 goes low (if c3 = 0), display 9 on lcd
IOCLR0 |= (1<<6);   // Port0.6=0 (rowA=0)
IOSET0 |= (1<<3)|(1<<4)|(1<<5);    //Port0.3, 0.4, 0.5 =1 (rowA, rowB, rowC =1)
    if (!(IOPIN0&(1<<0)))
{ while (!(IOPIN0&(1<<0))); return'*';}  //if pin0 goes low (if c1 = 0), display * on lcd
if (!(IOPIN0&(1<<1)))
{ while (!(IOPIN0&(1<<1))); return'0';}  // if pin1 goes low (if c2 = 0), display 0 on lcd
if (!(IOPIN0&(1<<2)))
{ while (!(IOPIN0&(1<<2))); return'#';}  // if pin1 goes low (if c3 = 0), display # on lcd
}
}

int main ()
{
PINSEL0 = 0;
PINSEL1 = 0;
PINSEL2 = 0;    // setting ports as GPIO functions
IODIR0 |= (0<<0)|(0<<1)|(0<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6);  // Port0.0, 0.1, 0.2 are i/p and 0.3,0.4,0.5,0.6 are o/p
IODIR1 = 0xffffffff;    // all pins of PORT1 are o/p
uint8_t key;
lcd_init ();    // initialize lcd
while (1)
{
key = keypad ();
decode_data (key);
}
}

RESULT- 

That's it guys. 
Subscribe for more tutorials.
Download the full code with simulation Here
Interface Keypad with LPC2148 Interface Keypad with LPC2148 Reviewed by Controllerstech on June 09, 2017 Rating: 5

No comments:

Powered by Blogger.