working, need to fix input bouncing

Dependencies:   mbed

Committer:
psahay
Date:
Sun Feb 22 20:31:08 2015 +0000
Revision:
3:3e18de5a9cce
Parent:
2:54ae225e6203
minor changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaredwil 0:b44cdf7cb6a5 1 #include "mbed.h"
jaredwil 0:b44cdf7cb6a5 2 #include <map>
jaredwil 0:b44cdf7cb6a5 3
jaredwil 0:b44cdf7cb6a5 4 //Initialize serial comms
jaredwil 0:b44cdf7cb6a5 5 Serial pc(USBTX, USBRX);
jaredwil 0:b44cdf7cb6a5 6
jaredwil 2:54ae225e6203 7 //Initialize PMW
jaredwil 0:b44cdf7cb6a5 8 PwmOut PWM1(p21);
jaredwil 0:b44cdf7cb6a5 9
jaredwil 2:54ae225e6203 10
jaredwil 2:54ae225e6203 11 //Initialize Keypad I/O
jaredwil 2:54ae225e6203 12 DigitalOut row1(p26);
jaredwil 2:54ae225e6203 13 DigitalOut row2(p23);
jaredwil 2:54ae225e6203 14 DigitalOut row3(p24);
jaredwil 2:54ae225e6203 15 DigitalOut row4(p25);
jaredwil 0:b44cdf7cb6a5 16
jaredwil 0:b44cdf7cb6a5 17 DigitalIn col1(p20);
jaredwil 0:b44cdf7cb6a5 18 DigitalIn col2(p19);
jaredwil 0:b44cdf7cb6a5 19 DigitalIn col3(p18);
jaredwil 0:b44cdf7cb6a5 20 DigitalIn col4(p17);
jaredwil 0:b44cdf7cb6a5 21 Timer t1;
jaredwil 0:b44cdf7cb6a5 22
jaredwil 2:54ae225e6203 23 //map used for keypad
jaredwil 0:b44cdf7cb6a5 24 map<int, char> keypadMap;
jaredwil 0:b44cdf7cb6a5 25
jaredwil 2:54ae225e6203 26
jaredwil 2:54ae225e6203 27 //Map for keypad
jaredwil 2:54ae225e6203 28 //First 4 bits corrospond to active column
jaredwil 2:54ae225e6203 29 //Last 4 bits corrospond to active row
jaredwil 0:b44cdf7cb6a5 30 void init(){
jaredwil 0:b44cdf7cb6a5 31 keypadMap[0x11]='1';
jaredwil 0:b44cdf7cb6a5 32 keypadMap[0x12]='2';
jaredwil 0:b44cdf7cb6a5 33 keypadMap[0x14]='3';
jaredwil 0:b44cdf7cb6a5 34 keypadMap[0x18]='A';
jaredwil 0:b44cdf7cb6a5 35 keypadMap[0x21]='4';
jaredwil 0:b44cdf7cb6a5 36 keypadMap[0x22]='5';
jaredwil 0:b44cdf7cb6a5 37 keypadMap[0x24]='6';
jaredwil 0:b44cdf7cb6a5 38 keypadMap[0x28]='B';
jaredwil 0:b44cdf7cb6a5 39 keypadMap[0x41]='7';
jaredwil 0:b44cdf7cb6a5 40 keypadMap[0x42]='8';
jaredwil 0:b44cdf7cb6a5 41 keypadMap[0x44]='9';
jaredwil 0:b44cdf7cb6a5 42 keypadMap[0x48]='C';
jaredwil 0:b44cdf7cb6a5 43 keypadMap[0x81]='*';
jaredwil 0:b44cdf7cb6a5 44 keypadMap[0x82]='0';
jaredwil 0:b44cdf7cb6a5 45 keypadMap[0x84]='#';
jaredwil 0:b44cdf7cb6a5 46 keypadMap[0x88]='D';
jaredwil 0:b44cdf7cb6a5 47 }
jaredwil 0:b44cdf7cb6a5 48
jaredwil 0:b44cdf7cb6a5 49 int main() {
jaredwil 0:b44cdf7cb6a5 50 init();
jaredwil 0:b44cdf7cb6a5 51 //Initialize rows to 0
jaredwil 0:b44cdf7cb6a5 52 row1 = 0;
jaredwil 0:b44cdf7cb6a5 53 row2 = 0;
jaredwil 0:b44cdf7cb6a5 54 row3 = 0;
jaredwil 0:b44cdf7cb6a5 55 row4 = 0;
jaredwil 0:b44cdf7cb6a5 56 t1.start();
jaredwil 0:b44cdf7cb6a5 57 int input = 0x00;
jaredwil 0:b44cdf7cb6a5 58 while(1) {
jaredwil 1:ab859ed60719 59 //make all rows 0 to add stability
jaredwil 1:ab859ed60719 60 row1 = 0;
jaredwil 1:ab859ed60719 61 row2 = 0;
jaredwil 1:ab859ed60719 62 row3 = 0;
jaredwil 1:ab859ed60719 63 row4 = 0;
jaredwil 0:b44cdf7cb6a5 64 //Keep each led lit for 4ms
jaredwil 0:b44cdf7cb6a5 65 switch(t1.read_ms()%4){
psahay 3:3e18de5a9cce 66 case 0: row1 = 1;
psahay 3:3e18de5a9cce 67 input |= 0x10;
psahay 3:3e18de5a9cce 68 break;
psahay 3:3e18de5a9cce 69 case 1: row2 = 1;
psahay 3:3e18de5a9cce 70 input |= 0x20;
psahay 3:3e18de5a9cce 71 break;
psahay 3:3e18de5a9cce 72 case 2: row3 = 1;
psahay 3:3e18de5a9cce 73 input |= 0x40;
psahay 3:3e18de5a9cce 74 break;
psahay 3:3e18de5a9cce 75 case 3: row4 = 1;
psahay 3:3e18de5a9cce 76 input |= 0x80;
psahay 3:3e18de5a9cce 77 break; }
jaredwil 2:54ae225e6203 78 //Pull the value from each column and check to see if 1
jaredwil 2:54ae225e6203 79 if(col1 == 1)
psahay 3:3e18de5a9cce 80 input |= 0x01;
jaredwil 2:54ae225e6203 81 else if(col2 == 1)
psahay 3:3e18de5a9cce 82 input |= 0x02;
jaredwil 2:54ae225e6203 83 else if(col3 == 1)
psahay 3:3e18de5a9cce 84 input |= 0x04;
jaredwil 2:54ae225e6203 85 else if(col4 == 1)
psahay 3:3e18de5a9cce 86 input |= 0x08;
jaredwil 2:54ae225e6203 87
jaredwil 2:54ae225e6203 88 char c = keypadMap[input]; //assign charcter based on input
jaredwil 2:54ae225e6203 89 //Print to the terminal the character if available
jaredwil 2:54ae225e6203 90 if(c != 0){
psahay 3:3e18de5a9cce 91 pc.printf("%c\r",c); //print input and char
jaredwil 2:54ae225e6203 92 }
jaredwil 2:54ae225e6203 93 //reset input
jaredwil 2:54ae225e6203 94 input = 0x00;
jaredwil 0:b44cdf7cb6a5 95 }
jaredwil 0:b44cdf7cb6a5 96 }