Thursday 9 June 2016

Interfacing of matrix keypad with LPC2148:



Matrix Keypad: 
Matrix Keypads are commonly used in calculators, telephones etc where a number of input switches are required. We know that matrix keypad is made by arranging push button switches in row and columns. In the straight forward way to connect a 4×4 keypad (16 switches) to a microcontroller we need 16 inputs pins. But by connecting switches in the following way we can read the status of each switch using 8 pins of the microcontroller.






Interfacing of matrix keypad with LPC2148:

#include<lpc214x.h>
#define row0 0
#define row1 1
#define row2 2
#define row3 3
#define col0 4
#define col1 5
#define col2 6
#define col3 7
#define PLLE 0
#define PLLC 1
#define PLOCK 10
#define rs 16
#define rw 17
#define en 18
#define lcdport IO0SET
#define lcdportclr IO0CLR
void daten(void);
void cmnd(void);
void pll_init(void);
void delay(int);
void lcdcmd(char);
void lcddata(char);
void lcd_init(void );
void keyscan();
void lcdstring(char *);
int main()
{
    pll_init();
    lcd_init();
    while(1)
    {
        keyscan();
    }
return 0;
}
void keyscan()
{
  IO0DIR=IO0DIR|(0x0f<<4);  
    //IO0DIR=IO0DIR&~(0x0f);
    IO0CLR=IO0CLR|(1<<col0);
    IO0SET=IO0SET|(1<<col1)|(1<<col2)|(1<<col3);
    if( (IO0PIN & (1<<row0))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 7 is pressd");
    }
    else if( (IO0PIN & (1<<row1))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 4 is pressd");
    }  
    else if( (IO0PIN & (1<<row2))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 1 is pressd");
    }  
    else if( (IO0PIN & (1<<row3))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key ON is pressd");
    }
    IO0CLR=IO0CLR|(1<<col1);
    IO0SET=IO0SET|(1<<col0)|(1<<col2)|(1<<col3);
    if( (IO0PIN & (1<<row0))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 8 is pressd");
    }
    else if( (IO0PIN & (1<<row1))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 5 is pressd");
    }  
    else if( (IO0PIN & (1<<row2))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 2 is pressd");
    }  
    else if( (IO0PIN & (1<<row3))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 0 is pressd");
    }
   
    IO0CLR=IO0CLR|(1<<col2);
    IO0SET=IO0SET|(1<<col0)|(1<<col1)|(1<<col3);
    if( (IO0PIN & (1<<row0))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 9 is pressd");
    }
    else if( (IO0PIN & (1<<row1))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 6 is pressd");
    }  
    else if( (IO0PIN & (1<<row2))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key 3 is pressd");
    }  
    else if( (IO0PIN & (1<<row3))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key = is pressd");
    }
    IO0CLR=IO0CLR|(1<<col3);
    IO0SET=IO0SET|(1<<col0)|(1<<col2)|(1<<col1);
    if( (IO0PIN & (1<<row0))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key / is pressd");
    }
    else if( (IO0PIN & (1<<row1))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key * is pressd");
    }  
    else if( (IO0PIN & (1<<row2))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key - is pressd");
    }  
    else if( (IO0PIN & (1<<row3))==0)
    {
        lcdcmd(0x01);lcdcmd(0x80);
        lcdstring("key + is pressd");
    }
}

void delay(int ms)
{
    T1CTCR=0x00;
    T1TC=0;
    T1PC=0;
    T1PR=59999;
    T1TCR=0x02;
    T1TCR=0x01;
    while( T1TC != ms);
    T1TCR=0;
}
void pll_init()
{
 PLL0CFG=0x24;
 PLL0CON=PLL0CON|(1<<PLLE);
 PLL0FEED=0xAA;
 PLL0FEED=0x55;
 //PLL0CON=PLL0CON|(1<<PLLC);
 while( (PLL0STAT & (1<<PLOCK))==0);
 PLL0CON=PLL0CON|3;
 PLL0FEED=0xAA;
 PLL0FEED=0x55;  
 VPBDIV=0x00;
}

void lcd_init()
{
    IO0DIR=IO0DIR|(0x7f<<16);
    lcdcmd(0x02);
    lcdcmd(0x28);
    lcdcmd(0x01);
    lcdcmd(0x0e);
    lcdcmd(0x80);
}
void lcdcmd(char ch)
{
    lcdport = ((ch&0xf0)<<15);
    cmnd();
    lcdportclr = ((ch&0xf0)<<15);
   
    lcdport = (((ch<<4)&0xf0)<<15);
    cmnd();
    lcdportclr = (((ch<<4)&0xf0)<<15);
}

void lcddata(char ch)
{
    lcdport = ((ch&0xf0)<<15);
    daten();
    lcdportclr = ((ch&0xf0)<<15);
   
    lcdport = (((ch<<4)&0xf0)<<15);
    daten();
    lcdportclr = (((ch<<4)&0xf0)<<15);
}
void cmnd()
{
    lcdportclr = (1<<rs);
    lcdportclr = (1<<rw);
    lcdport = (1<<en);
    delay(1);
    lcdportclr = (1<<en);
    delay(2);
}
void daten()
{
    lcdport = (1<<rs);
    lcdportclr = (1<<rw);
    lcdport = (1<<en);
    delay(1);
    lcdportclr = (1<<en);
    delay(2);
}

void lcdstring(char  *x)
{
    while( *x != '\0')
    {
        lcddata(*x);
        x++;
    }
}



Simulation:




0 comments:

Post a Comment

if you have any doubt please let me know