Friday, 10 March 2017

Matrix Keypad (4x3) Interface With Microcontroller

Matrix Keypad Interface With PIC Microntroller



Metric keypads use in various types of embedded applications like door lock system, phones, as input devices etc. We can reduce numbers of pins by using metric keypad instead to individual keys. Matrix keypad (4x3) have 12 buttons and we can scan all of button with 7 microcontroller pins. 


Circuit Diagram

In the example circuit, we are using a keypad and a LCD (16x2) for display the value when anyone keypad key pressed. Only single key can be scan at a time.





Matrix Keypad scanning algorithm

In the normal condition when  no key is pressed, all columns (c-0 to c-2) read low. When any key is pressed the specific column goes high.  When microcontroller find any column pin high, then scanning algorithm works. For example column-3 is high, First controller low first row pin and scan that column-3 is still high or not. Controller turn low row pins one by one until column-3 pin does not go low. Once the row has been identified, the specific column of the pressed key can be established by locating the position of the single low bit on the input port.




Codes

#include<p18f2520.h>

#define RS_PIN PORTAbits.RA1  /* Pin for RS */
#define E_PIN PORTAbits.RA0   /* Pin for E  */

//--------------------------------------------------------------------------
void LCD_Init(void);
void Str_LCD( const rom char *buffer);
void Chr_LCD(char data);
void Clear_LCD(void);
void Move_Corsor( char Line, char Pos);
//--------------------------------------------------------------------------

void Time_10ms(void);
void scan_keypad(void);

void main()
{
                TRISA=0x00;
                TRISB=0x07;                       // Set row pins as output and Column pins as input
                TRISC=0x00;                       // Output port for LCD
                PORTA=0X00;
                PORTB=0X78;                     // High to Row pins
                PORTC=0x00;
               
                ADCON1 = 0x0F;               // Set all AN pins as digital
               
                T0CON = 0x06;   // Timer-0 for LCD
                LCD_Init();
                Str_LCD("Waiting for key");
                while(1)
                {
                                scan_keypad();
                }
}

void Time_10ms()
{
                TMR0H=0xFE;
                TMR0L=0x7A;
                T0CONbits.TMR0ON=1;
                while(INTCONbits.TMR0IF==0);
                T0CONbits.TMR0ON=0;
                INTCONbits.TMR0IF=0;
}

void LCD_Init()
{
                RS_PIN=0;              //LCD instructions              
                Chr_LCD(0x38);     //LCD 8 Bit Data
                Chr_LCD(0x01);     //LCD clear
                Chr_LCD(0x0C);    //LCD diplay on cursor not blinking
                Chr_LCD(0x80);     //LCD cursor Pos. begain
                RS_PIN=1;              //LCD DATA
}
void Str_LCD( const rom char *buffer)
{
     while(*buffer)                  // Write data to LCD up to null
        {
             Chr_LCD(*buffer); // Write character to LCD
             buffer++;                 // Increment buffer
        }
}
void Chr_LCD(char data)
{
                PORTC=data;
                E_PIN=1;
                Time_10ms();
                Time_10ms();
                E_PIN=0;
                Time_10ms();
                Time_10ms();
}
void Clear_LCD(void)
{
                RS_PIN=0;
                Chr_LCD(0x01);
                Chr_LCD(0x80);
                RS_PIN=1;
}
void Move_Corsor( char Line, char Pos)
{
                RS_PIN=0;
                if(Line==1)Chr_LCD(0x80);
                if(Line==2)Chr_LCD(0xC0);
                while(Pos!=0)
                {
                                Chr_LCD(0x14);
                                Pos--;
                }
                RS_PIN=1;
}

void scan_keypad()
{
                if(PORTBbits.RB0==1)    // Scan column-0
                {
                                Clear_LCD();
                                PORTBbits.RB3 = 0;
                                if(PORTBbits.RB0==0) Str_LCD("You pressed \"1\"");
                                else
                                {
                                                PORTBbits.RB4=0;
                                                if(PORTBbits.RB0==0) Str_LCD("You pressed \"4\"");
                                                else
                                                {
                                                                PORTBbits.RB5=0;
                                                                if(PORTBbits.RB0==0) Str_LCD("You pressed \"7\"");
                                                                else
                                                                {
                                                                                PORTBbits.RB6=0;
                                                                                if(PORTBbits.RB0==0) Str_LCD("You pressed \"*\"");
                                                                }
                                                }
                                }
                }
               
                if(PORTBbits.RB1==1)    // Scan column-1
                {
                                Clear_LCD();
                                PORTBbits.RB3 = 0;
                                if(PORTBbits.RB1==0) Str_LCD("You pressed \"2\"");
                                else
                                {
                                                PORTBbits.RB4=0;
                                                if(PORTBbits.RB1==0) Str_LCD("You pressed \"5\"");
                                                else
                                                {
                                                                PORTBbits.RB5=0;
                                                                if(PORTBbits.RB1==0) Str_LCD("You pressed \"8\"");
                                                                else
                                                                {
                                                                                PORTBbits.RB6=0;
                                                                                if(PORTBbits.RB1==0) Str_LCD("You pressed \"0\"");
                                                                }
                                                }
                                }
                }
               
                if(PORTBbits.RB2==1)    // Scan column-2
                {
                                Clear_LCD();
                                PORTBbits.RB3 = 0;
                                if(PORTBbits.RB2==0) Str_LCD("You pressed \"3\"");
                                else
                                {
                                                PORTBbits.RB4=0;
                                                if(PORTBbits.RB2==0) Str_LCD("You pressed \"6\"");
                                                else
                                                {
                                                                PORTBbits.RB5=0;
                                                                if(PORTBbits.RB2==0) Str_LCD("You pressed \"9\"");
                                                                else
                                                                {
                                                                                PORTBbits.RB6=0;
                                                                                if(PORTBbits.RB2==0) Str_LCD("You pressed \"#\"");
                                                                }
                                                }             
                                }
                }
                PORTB=0X78;                     // Set again to Row pins               

}









 If you have any question, ask feel free. I  will totally devoted for your help.

No comments:

Post a Comment