working, need to fix input bouncing

Dependencies:   mbed

Committer:
jaredwil
Date:
Sun Feb 22 17:59:32 2015 +0000
Revision:
2:54ae225e6203
Parent:
1:ab859ed60719
Child:
3:3e18de5a9cce
commented;

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){
jaredwil 0:b44cdf7cb6a5 66 case 0:
jaredwil 0:b44cdf7cb6a5 67 row1 = 1;
jaredwil 1:ab859ed60719 68 input |= 0x10;
jaredwil 0:b44cdf7cb6a5 69 break;
jaredwil 0:b44cdf7cb6a5 70 case 1:
jaredwil 0:b44cdf7cb6a5 71 row2 = 1;
jaredwil 0:b44cdf7cb6a5 72 input |= 0x20;
jaredwil 0:b44cdf7cb6a5 73 break;
jaredwil 0:b44cdf7cb6a5 74 case 2:
jaredwil 0:b44cdf7cb6a5 75 row3 = 1;
jaredwil 0:b44cdf7cb6a5 76 input |= 0x40;
jaredwil 0:b44cdf7cb6a5 77 break;
jaredwil 0:b44cdf7cb6a5 78 case 3:
jaredwil 0:b44cdf7cb6a5 79 row4 = 1;
jaredwil 0:b44cdf7cb6a5 80 input |= 0x80;
jaredwil 0:b44cdf7cb6a5 81 break;
jaredwil 2:54ae225e6203 82
jaredwil 2:54ae225e6203 83 }
jaredwil 2:54ae225e6203 84 //Pull the value from each column and check to see if 1
jaredwil 2:54ae225e6203 85 if(col1 == 1)
jaredwil 2:54ae225e6203 86 input |= 0x01;
jaredwil 2:54ae225e6203 87 else if(col2 == 1)
jaredwil 2:54ae225e6203 88 input |= 0x02;
jaredwil 2:54ae225e6203 89 else if(col3 == 1)
jaredwil 2:54ae225e6203 90 input |= 0x04;
jaredwil 2:54ae225e6203 91 else if(col4 == 1)
jaredwil 2:54ae225e6203 92 input |= 0x08;
jaredwil 2:54ae225e6203 93
jaredwil 2:54ae225e6203 94
jaredwil 2:54ae225e6203 95 char c = keypadMap[input]; //assign charcter based on input
jaredwil 2:54ae225e6203 96 //Print to the terminal the character if available
jaredwil 2:54ae225e6203 97 if(c != 0){
jaredwil 2:54ae225e6203 98 pc.printf("%c\r",c); //print input and char
jaredwil 2:54ae225e6203 99 }
jaredwil 2:54ae225e6203 100
jaredwil 2:54ae225e6203 101 //reset input
jaredwil 2:54ae225e6203 102 input = 0x00;
jaredwil 0:b44cdf7cb6a5 103 }
jaredwil 0:b44cdf7cb6a5 104 }