Thursday 9 June 2016

Interfacing of matrix keypad with ARM Cortex



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.





Program1:
 Smpl_GPIO_Keypad : scan keypad 3x3 (1~9) to control GPB0~8
/
// 4-port Relay
// VCC : to 3.3V
// IN1 : to GPB0 (press 1 will output 3.3V, else output 0V)
// IN2 : to GPB1 (press 2 will output 3.3V, else output 0V)
// IN3 : to GPB2 (press 3 will output 3.3V, else output 0V)
// IN4 : to GPB3 (press 4 will output 3.3V, else output 0V)
// GND : to GND

#include "stdio.h"                                                                                                           
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
//#include "Seven_Segment.h"
//#include "Scankey.h"
//#include "LCD.h"
#define LCD_DATA   GPIOE->DOUT                 // D0~7 pin = GPE0~7
#define LCD_RS_SET DrvGPIO_SetBit(E_GPB,0) // RS   pin = GPA0
#define LCD_RS_CLR DrvGPIO_ClrBit(E_GPB,0)
#define LCD_RW_SET DrvGPIO_SetBit(E_GPB,1) // RW   pin = GPA1      
#define LCD_RW_CLR DrvGPIO_ClrBit(E_GPB,1)
#define LCD_E_SET  DrvGPIO_SetBit(E_GPB,2) // E    pin = GPA2      
#define LCD_E_CLR  DrvGPIO_ClrBit(E_GPB,2)


void init_GPIO(void)
{
  DrvGPIO_Open(E_GPB, 0, E_IO_OUTPUT);
  DrvGPIO_Open(E_GPB, 1, E_IO_OUTPUT);
  DrvGPIO_Open(E_GPB, 2, E_IO_OUTPUT);
  DrvGPIO_ClrBit(E_GPB, 0);
  DrvGPIO_ClrBit(E_GPB, 1);
  DrvGPIO_ClrBit(E_GPB, 2);
}

void  lcdWriteData(unsigned  char wdata)
{
   LCD_DATA=wdata | 0xFF00;
   LCD_RS_SET;
   LCD_RW_CLR;
  
    LCD_E_SET;
   DrvSYS_Delay(50000);
   LCD_E_CLR;
     //DrvSYS_Delay(1000000);
}

void  lcdWriteCommand(unsigned  char wdata)
{
   LCD_DATA=wdata;
   LCD_RS_CLR;
   LCD_RW_CLR;
  
    LCD_E_SET;
   DrvSYS_Delay(50000);
   LCD_E_CLR;
    //DrvSYS_Delay(1000000);
}

void delay(void)
{
    unsigned int j;
    for(j=0;j<60000;j++);
}

void OpenKeyPad(void)
{
    uint8_t i;
    /* Initial key pad */
    for(i=0;i<6;i++)
    DrvGPIO_Open(E_GPA, i, E_IO_QUASI);
}

void CloseKeyPad(void)
{
    uint8_t i;

    for(i=0;i<6;i++)
    DrvGPIO_Close(E_GPA, i);
}

uint8_t ScanKey(void)
{
    uint8_t act[4]={0x3b, 0x3d, 0x3e};   
    uint8_t i,temp,pin;

    for(i=0;i<3;i++)
    {
        temp=act[i];
        for(pin=0;pin<6;pin++)
        {
            if((temp&0x01)==0x01)
                DrvGPIO_SetBit(E_GPA,pin);
            else
                DrvGPIO_ClrBit(E_GPA,pin);
            temp>>=1;
        }
        delay();
        if(DrvGPIO_GetBit(E_GPA,3)==0)
            return(i+1);
        if(DrvGPIO_GetBit(E_GPA,4)==0)
            return(i+4);
        if(DrvGPIO_GetBit(E_GPA,5)==0)
            return(i+7);
    }
        return 0;
}

/*----------------------------------------------------------------------------
  MAIN function
  ----------------------------------------------------------------------------*/
int32_t main (void)
{
   
    int8_t i, number;
    unsigned char arr[]={0x38,0x0e,0x01,0x80,0x06,'\0'};
    int j;

 
  //for (i=0; i<9; i++)    {
    //DrvGPIO_Open(E_GPB, i, E_IO_OUTPUT);
    //DrvGPIO_ClrBit(E_GPB, i);
    //}
    OpenKeyPad();

    //print_Line(0,"Smpl_GPIO_Keypad");

   
   
    init_GPIO();
    while(1)
{
    for(j=0;arr[j]!='\0';j++)
    {
    lcdWriteCommand(arr[j]);
        DrvSYS_Delay(37);
    }

   
     number = ScanKey();
        lcdWriteData(number+'0');   

}
}



Program2: Small Method
//
// Smpl_GPIO_Keypad : scan keypad 3x3 (1~9) to control GPB0~8
//
// 4-port Relay
// VCC : to 3.3V
// IN1 : to GPB0 (press 1 will output 3.3V, else output 0V)
// IN2 : to GPB1 (press 2 will output 3.3V, else output 0V)
// IN3 : to GPB2 (press 3 will output 3.3V, else output 0V)
// IN4 : to GPB3 (press 4 will output 3.3V, else output 0V)
// GND : to GND

#include "stdio.h"                                                                                                           
#include "NUC1xx.h"
#include "GPIO.h"
#include "SYS.h"
#define seven   GPIOE->DOUT
#define key  GPIOA->DOUT
unsigned char SEG_BUF[16]={0x82,0xee,0x07,0x46,0x6a,0x52,0x12,0xe6,0x02,0x42};

void delay(void)
{
    unsigned int j;
    for(j=0;j<60000;j++);
}

uint8_t ScanKey(void)
{
   
key=0x3b;
       
         //delay();
       
        if(DrvGPIO_GetBit(E_GPA,3)==0)
            return(1);
        if(DrvGPIO_GetBit(E_GPA,4)==0)
            return(4);
        if(DrvGPIO_GetBit(E_GPA,5)==0)
            return(7);
       
        key=0x3d;
        //delay();
        if(DrvGPIO_GetBit(E_GPA,3)==0)
            return(2);
        if(DrvGPIO_GetBit(E_GPA,4)==0)
            return(5);
        if(DrvGPIO_GetBit(E_GPA,5)==0)
            return(8);
        key=0x3e;
        //delay();
        if(DrvGPIO_GetBit(E_GPA,3)==0)
            return(3);
        if(DrvGPIO_GetBit(E_GPA,4)==0)
            return(6);
        if(DrvGPIO_GetBit(E_GPA,5)==0)
            return(9);
return(0);
}

/*----------------------------------------------------------------------------
  MAIN function
  ----------------------------------------------------------------------------*/
int32_t main (void)
{

    int8_t i, number,m;

    for(m=0;m<6;m++)
    DrvGPIO_Open(E_GPA, m, E_IO_QUASI);
   
    for(i=0;i<8;i++)
    DrvGPIO_Open(E_GPE, i, E_IO_OUTPUT);
    DrvGPIO_Open(E_GPC,12,E_IO_OUTPUT);   
   
 while(1)
    { number = ScanKey();
            seven=SEG_BUF[number];
                    
    }
}


0 comments:

Post a Comment

if you have any doubt please let me know