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

Committer:
grantphillips
Date:
Tue Apr 05 19:04:04 2016 +0000
Revision:
3:7d6b9684595c
Parent:
0:6f83aeb11f9a
v1.00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:6f83aeb11f9a 1 #include "MCP23008_Keypad.h"
grantphillips 0:6f83aeb11f9a 2 #include "mbed.h"
grantphillips 0:6f83aeb11f9a 3
grantphillips 0:6f83aeb11f9a 4
grantphillips 0:6f83aeb11f9a 5
grantphillips 0:6f83aeb11f9a 6 // Keypad layout:
grantphillips 0:6f83aeb11f9a 7 // [row][col] Col0 Col1 Col2 Col3
grantphillips 0:6f83aeb11f9a 8 char const kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'A'}, //row0
grantphillips 0:6f83aeb11f9a 9 {'4' ,'5' ,'6' ,'B'}, //row1
grantphillips 0:6f83aeb11f9a 10 {'7' ,'8' ,'9' ,'C'}, //row2
grantphillips 0:6f83aeb11f9a 11 {'*' ,'0' ,'#' ,'D'}}; //row3
grantphillips 0:6f83aeb11f9a 12
grantphillips 0:6f83aeb11f9a 13 //NIBBLE LOW=0000, HIGH= 0111 1011 1101 1110 Col (x)
grantphillips 0:6f83aeb11f9a 14 const char KpdInMask[4] ={0xe0,0xd0,0xb0,0x70};
grantphillips 0:6f83aeb11f9a 15
grantphillips 0:6f83aeb11f9a 16 //NIBBLE HIGH=1111, LOW= 0111 1011 1101 1110 Rows (y)
grantphillips 0:6f83aeb11f9a 17 const char KpdOutMask[4]={0xfe,0xfd,0xfb,0xf7};
grantphillips 0:6f83aeb11f9a 18
grantphillips 0:6f83aeb11f9a 19
grantphillips 0:6f83aeb11f9a 20
grantphillips 0:6f83aeb11f9a 21 MCP23008_Keypad::MCP23008_Keypad(PinName SDApin, PinName SCLpin, int MCP23008Address) : i2c(SDApin, SCLpin) {
grantphillips 0:6f83aeb11f9a 22 char cmd[2];
grantphillips 0:6f83aeb11f9a 23
grantphillips 0:6f83aeb11f9a 24 _addr = MCP23008Address;
grantphillips 0:6f83aeb11f9a 25 i2c.frequency(100000); //set I2C frequency to 100Khz
grantphillips 0:6f83aeb11f9a 26 cmd[0] = 0x00; //IODIR register
grantphillips 0:6f83aeb11f9a 27 cmd[1] = 0xf0; //Bit7 - 4 are inputs and rest outputs
grantphillips 0:6f83aeb11f9a 28 i2c.write(_addr, cmd, 2); //write IODIR setting to MCP23008
grantphillips 0:6f83aeb11f9a 29 }
grantphillips 0:6f83aeb11f9a 30
grantphillips 0:6f83aeb11f9a 31 char MCP23008_Keypad::ReadKey() {
grantphillips 0:6f83aeb11f9a 32 char KeyValue, Done=0;
grantphillips 0:6f83aeb11f9a 33 uint16_t y, x;
grantphillips 0:6f83aeb11f9a 34 char cmd[2];
grantphillips 0:6f83aeb11f9a 35
grantphillips 0:6f83aeb11f9a 36 //delay_ms(ContactBounceTime); //warning no contact bounce protection
grantphillips 0:6f83aeb11f9a 37 //call read_key more than once with delay
grantphillips 0:6f83aeb11f9a 38 //between if key stay constant then key is pressed
grantphillips 0:6f83aeb11f9a 39 y = 0;
grantphillips 0:6f83aeb11f9a 40 while((y < 4) && (!Done))
grantphillips 0:6f83aeb11f9a 41 {
grantphillips 0:6f83aeb11f9a 42 cmd[0] = 0x09;
grantphillips 0:6f83aeb11f9a 43 cmd[1] = KpdOutMask[y];
grantphillips 0:6f83aeb11f9a 44 i2c.write(_addr, cmd, 2); //write mask value to MCP23008 GPIO register
grantphillips 0:6f83aeb11f9a 45
grantphillips 0:6f83aeb11f9a 46 wait(0.01);
grantphillips 0:6f83aeb11f9a 47
grantphillips 0:6f83aeb11f9a 48 cmd[0] = 0x09;
grantphillips 0:6f83aeb11f9a 49 i2c.write(_addr, cmd, 1);
grantphillips 0:6f83aeb11f9a 50 i2c.read(_addr, cmd, 1);
grantphillips 0:6f83aeb11f9a 51 KeyValue = cmd[0] & 0xf0; //read 4 MSB bits from MCP23008 GPIO register
grantphillips 0:6f83aeb11f9a 52
grantphillips 0:6f83aeb11f9a 53 if(KeyValue == KpdInMask[0])
grantphillips 0:6f83aeb11f9a 54 x = 0;
grantphillips 0:6f83aeb11f9a 55 else if(KeyValue == KpdInMask[1])
grantphillips 0:6f83aeb11f9a 56 x = 1;
grantphillips 0:6f83aeb11f9a 57 else if(KeyValue == KpdInMask[2])
grantphillips 0:6f83aeb11f9a 58 x = 2;
grantphillips 0:6f83aeb11f9a 59 else if(KeyValue == KpdInMask[3])
grantphillips 0:6f83aeb11f9a 60 x = 3;
grantphillips 0:6f83aeb11f9a 61 else
grantphillips 0:6f83aeb11f9a 62 {
grantphillips 0:6f83aeb11f9a 63 KeyValue='\0'; //more than one key was pressed or no key in this row.
grantphillips 0:6f83aeb11f9a 64 x=9;
grantphillips 0:6f83aeb11f9a 65 }
grantphillips 0:6f83aeb11f9a 66 if(x != 9)
grantphillips 0:6f83aeb11f9a 67 {
grantphillips 0:6f83aeb11f9a 68 Done = 1; //valid key found
grantphillips 0:6f83aeb11f9a 69 KeyValue = kpdLayout[x][y]; //convert to a character eg. '1','2','3','#','*'
grantphillips 0:6f83aeb11f9a 70 }
grantphillips 0:6f83aeb11f9a 71 y++;
grantphillips 0:6f83aeb11f9a 72 }
grantphillips 0:6f83aeb11f9a 73 return(KeyValue);
grantphillips 0:6f83aeb11f9a 74 }