Class library for a 4x4 keypad connected to a MCP23008 I2C IO Expander.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP23008_Keypad.cpp Source File

MCP23008_Keypad.cpp

00001 #include "MCP23008_Keypad.h"
00002 #include "mbed.h"
00003  
00004 
00005 
00006 // Keypad layout:
00007 //                [row][col]   Col0 Col1 Col2 Col3 
00008 char const kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'A'},  //row0
00009                               {'4' ,'5' ,'6' ,'B'},  //row1
00010                               {'7' ,'8' ,'9' ,'C'},  //row2
00011                               {'*' ,'0' ,'#' ,'D'}}; //row3
00012 
00013 //NIBBLE LOW=0000,  HIGH= 0111 1011 1101 1110    Col  (x)
00014 const char KpdInMask[4] ={0xe0,0xd0,0xb0,0x70};
00015 
00016 //NIBBLE HIGH=1111,  LOW= 0111 1011 1101 1110    Rows (y)
00017 const char KpdOutMask[4]={0xfe,0xfd,0xfb,0xf7};  
00018     
00019 
00020 
00021 MCP23008_Keypad::MCP23008_Keypad(PinName SDApin, PinName SCLpin, int MCP23008Address) : i2c(SDApin, SCLpin) {
00022     char cmd[2];
00023     
00024     _addr = MCP23008Address;
00025     i2c.frequency(100000);      //set I2C frequency to 100Khz
00026     cmd[0] = 0x00;              //IODIR register
00027     cmd[1] = 0xf0;              //Bit7 - 4 are inputs and rest outputs
00028     i2c.write(_addr, cmd, 2);   //write IODIR setting to MCP23008
00029 }
00030 
00031 char MCP23008_Keypad::ReadKey() {
00032     char KeyValue, Done=0;
00033     uint16_t y, x;
00034     char cmd[2];
00035     
00036     //delay_ms(ContactBounceTime);  //warning no contact bounce protection
00037                                     //call read_key more than once with delay
00038                                     //between if key stay constant then key is pressed
00039     y = 0;
00040     while((y < 4) && (!Done))
00041     {
00042         cmd[0] = 0x09;
00043         cmd[1] = KpdOutMask[y];
00044         i2c.write(_addr, cmd, 2);           //write mask value to MCP23008 GPIO register
00045       
00046         wait(0.01);  
00047 
00048         cmd[0] = 0x09;
00049         i2c.write(_addr, cmd, 1);
00050         i2c.read(_addr, cmd, 1);
00051         KeyValue = cmd[0] & 0xf0;           //read 4 MSB bits from MCP23008 GPIO register
00052         
00053         if(KeyValue == KpdInMask[0])
00054             x = 0;
00055         else if(KeyValue == KpdInMask[1])
00056             x = 1;
00057         else if(KeyValue == KpdInMask[2])
00058             x = 2;
00059         else if(KeyValue == KpdInMask[3])
00060             x = 3;
00061         else
00062         {
00063             KeyValue='\0';                  //more than one key was pressed or no key in this row.
00064             x=9;          
00065         }
00066         if(x != 9)
00067         {
00068             Done = 1;                       //valid key found
00069             KeyValue = kpdLayout[x][y];     //convert to a character eg. '1','2','3','#','*'
00070         }
00071         y++;
00072     }
00073     return(KeyValue);
00074 }