first version of a keypad (4x3) implementation working with interrupts instead of polling.

Dependencies:   mbed

Committer:
Renegr
Date:
Sun Dec 11 08:50:58 2011 +0000
Revision:
0:510bbea7c78e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Renegr 0:510bbea7c78e 1 #include "mbed.h"
Renegr 0:510bbea7c78e 2
Renegr 0:510bbea7c78e 3 BusInOut colBus( p26, p25, p24);
Renegr 0:510bbea7c78e 4 const PinName rowPins[] = { p30, p29, p28, p27 };
Renegr 0:510bbea7c78e 5
Renegr 0:510bbea7c78e 6 volatile bool irqFinished = false;
Renegr 0:510bbea7c78e 7
Renegr 0:510bbea7c78e 8 inline char toChar( unsigned char r, unsigned char c) {
Renegr 0:510bbea7c78e 9 static const char keytable[] = { '1', '2', '3', 'A',
Renegr 0:510bbea7c78e 10 '4', '5', '6', 'B',
Renegr 0:510bbea7c78e 11 '7', '8', '9', 'C',
Renegr 0:510bbea7c78e 12 '*', '0', '#', 'D'
Renegr 0:510bbea7c78e 13 };
Renegr 0:510bbea7c78e 14 return keytable[r*4+c];
Renegr 0:510bbea7c78e 15 }
Renegr 0:510bbea7c78e 16
Renegr 0:510bbea7c78e 17 inline char toBinary( unsigned char r, unsigned char c) {
Renegr 0:510bbea7c78e 18 static const char keytable[] = { 0x1, 0x2, 0x3, 0xC,
Renegr 0:510bbea7c78e 19 0x4, 0x5, 0x6, 0xD,
Renegr 0:510bbea7c78e 20 0x7, 0x8, 0x9, 0xE,
Renegr 0:510bbea7c78e 21 0xA, 0x0, 0xB, 0xF
Renegr 0:510bbea7c78e 22 };
Renegr 0:510bbea7c78e 23 return keytable[r*4+c];
Renegr 0:510bbea7c78e 24 }
Renegr 0:510bbea7c78e 25
Renegr 0:510bbea7c78e 26 inline void start() {
Renegr 0:510bbea7c78e 27 colBus.output();
Renegr 0:510bbea7c78e 28 colBus = 0xF;
Renegr 0:510bbea7c78e 29 }
Renegr 0:510bbea7c78e 30
Renegr 0:510bbea7c78e 31 // simple log2, only 4bits needed
Renegr 0:510bbea7c78e 32 inline unsigned char _lg2( unsigned char c) {
Renegr 0:510bbea7c78e 33 return c&1 ? 0 : c&2 ? 1 : c&4 ? 2 : 3;
Renegr 0:510bbea7c78e 34 }
Renegr 0:510bbea7c78e 35
Renegr 0:510bbea7c78e 36 inline char pin2Row(PinName pin) {
Renegr 0:510bbea7c78e 37 return rowPins[0] == pin ? 0 : rowPins[1] == pin ? 1 : rowPins[2] == pin ? 2 : 3;
Renegr 0:510bbea7c78e 38 }
Renegr 0:510bbea7c78e 39
Renegr 0:510bbea7c78e 40 BusOut code(LED4, LED3, LED2, LED1); //show binary code of key
Renegr 0:510bbea7c78e 41
Renegr 0:510bbea7c78e 42 void checkColumn(PinName pinPressed) {
Renegr 0:510bbea7c78e 43 { //debounce
Renegr 0:510bbea7c78e 44 DigitalIn in(pinPressed);
Renegr 0:510bbea7c78e 45 for (char c=0; c<100; ++c) {
Renegr 0:510bbea7c78e 46 if ( !in.read()) {
Renegr 0:510bbea7c78e 47 irqFinished=true;
Renegr 0:510bbea7c78e 48 return; // key not long enough pressed
Renegr 0:510bbea7c78e 49 }
Renegr 0:510bbea7c78e 50 wait_us(100);
Renegr 0:510bbea7c78e 51 }
Renegr 0:510bbea7c78e 52 }
Renegr 0:510bbea7c78e 53 // check column
Renegr 0:510bbea7c78e 54 DigitalOut p(pinPressed);
Renegr 0:510bbea7c78e 55 p = 0x1;
Renegr 0:510bbea7c78e 56 colBus.input();
Renegr 0:510bbea7c78e 57 unsigned char col = colBus.read();
Renegr 0:510bbea7c78e 58 if (col) {
Renegr 0:510bbea7c78e 59 col = _lg2(col);
Renegr 0:510bbea7c78e 60 { // blink to show key pressed
Renegr 0:510bbea7c78e 61 code = 0;
Renegr 0:510bbea7c78e 62 wait_ms(150);
Renegr 0:510bbea7c78e 63 code = 0xF;
Renegr 0:510bbea7c78e 64 wait_ms(150);
Renegr 0:510bbea7c78e 65 }
Renegr 0:510bbea7c78e 66 char row = pin2Row(pinPressed);
Renegr 0:510bbea7c78e 67 code = toBinary(row,col);
Renegr 0:510bbea7c78e 68 // printf("%d, %d, %c\r\n", row, col, toChar(row,col));
Renegr 0:510bbea7c78e 69 }
Renegr 0:510bbea7c78e 70 irqFinished = true;
Renegr 0:510bbea7c78e 71 }
Renegr 0:510bbea7c78e 72
Renegr 0:510bbea7c78e 73 void trigger27() {
Renegr 0:510bbea7c78e 74 checkColumn(p27);
Renegr 0:510bbea7c78e 75 }
Renegr 0:510bbea7c78e 76 void trigger28() {
Renegr 0:510bbea7c78e 77 checkColumn(p28);
Renegr 0:510bbea7c78e 78 }
Renegr 0:510bbea7c78e 79 void trigger29() {
Renegr 0:510bbea7c78e 80 checkColumn(p29);
Renegr 0:510bbea7c78e 81 }
Renegr 0:510bbea7c78e 82 void trigger30() {
Renegr 0:510bbea7c78e 83 checkColumn(p30);
Renegr 0:510bbea7c78e 84 }
Renegr 0:510bbea7c78e 85
Renegr 0:510bbea7c78e 86 void checkRow() {
Renegr 0:510bbea7c78e 87 InterruptIn irqIn27(p27), irqIn28(p28), irqIn29(p29), irqIn30(p30);
Renegr 0:510bbea7c78e 88 irqIn27.rise( &trigger27);
Renegr 0:510bbea7c78e 89 irqIn28.rise( &trigger28);
Renegr 0:510bbea7c78e 90 irqIn29.rise( &trigger29);
Renegr 0:510bbea7c78e 91 irqIn30.rise( &trigger30);
Renegr 0:510bbea7c78e 92 }
Renegr 0:510bbea7c78e 93
Renegr 0:510bbea7c78e 94 int main() {
Renegr 0:510bbea7c78e 95 while (1) {
Renegr 0:510bbea7c78e 96 start();
Renegr 0:510bbea7c78e 97 irqFinished = false;
Renegr 0:510bbea7c78e 98 checkRow();
Renegr 0:510bbea7c78e 99 while (!irqFinished);
Renegr 0:510bbea7c78e 100 }
Renegr 0:510bbea7c78e 101 }