new

Dependencies:   mbed

Committer:
jaredwil
Date:
Tue Feb 17 21:31:58 2015 +0000
Revision:
0:89cc1300ce7e
Child:
1:461a4f04db9b
part4_trial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaredwil 0:89cc1300ce7e 1 #include "mbed.h"
jaredwil 0:89cc1300ce7e 2 #include <map>
jaredwil 0:89cc1300ce7e 3 #include <string>
jaredwil 0:89cc1300ce7e 4
jaredwil 0:89cc1300ce7e 5 //Initialize serial comms
jaredwil 0:89cc1300ce7e 6 Serial pc(USBTX, USBRX);
jaredwil 0:89cc1300ce7e 7
jaredwil 0:89cc1300ce7e 8 PwmOut PWMh(p26);
jaredwil 0:89cc1300ce7e 9 PwmOut PWMl(p21);
jaredwil 0:89cc1300ce7e 10
jaredwil 0:89cc1300ce7e 11 DigitalOut row1(p25);
jaredwil 0:89cc1300ce7e 12 DigitalOut row2(p22);
jaredwil 0:89cc1300ce7e 13 DigitalOut row3(p23);
jaredwil 0:89cc1300ce7e 14 DigitalOut row4(p24);
jaredwil 0:89cc1300ce7e 15
jaredwil 0:89cc1300ce7e 16 DigitalIn col1(p20);
jaredwil 0:89cc1300ce7e 17 DigitalIn col2(p19);
jaredwil 0:89cc1300ce7e 18 DigitalIn col3(p18);
jaredwil 0:89cc1300ce7e 19 DigitalIn col4(p17);
jaredwil 0:89cc1300ce7e 20 Timer t1;
jaredwil 0:89cc1300ce7e 21
jaredwil 0:89cc1300ce7e 22 //map
jaredwil 0:89cc1300ce7e 23 map<int, char> keypadMap;
jaredwil 0:89cc1300ce7e 24 map<int, char *> dtmfMap;
jaredwil 0:89cc1300ce7e 25
jaredwil 0:89cc1300ce7e 26 //Mores Code -> Character Map
jaredwil 0:89cc1300ce7e 27 void init(){
jaredwil 0:89cc1300ce7e 28 keypadMap[0x11]='1';
jaredwil 0:89cc1300ce7e 29 keypadMap[0x12]='2';
jaredwil 0:89cc1300ce7e 30 keypadMap[0x14]='3';
jaredwil 0:89cc1300ce7e 31 keypadMap[0x18]='A';
jaredwil 0:89cc1300ce7e 32 keypadMap[0x21]='4';
jaredwil 0:89cc1300ce7e 33 keypadMap[0x22]='5';
jaredwil 0:89cc1300ce7e 34 keypadMap[0x24]='6';
jaredwil 0:89cc1300ce7e 35 keypadMap[0x28]='B';
jaredwil 0:89cc1300ce7e 36 keypadMap[0x41]='7';
jaredwil 0:89cc1300ce7e 37 keypadMap[0x42]='8';
jaredwil 0:89cc1300ce7e 38 keypadMap[0x44]='9';
jaredwil 0:89cc1300ce7e 39 keypadMap[0x48]='C';
jaredwil 0:89cc1300ce7e 40 keypadMap[0x81]='*';
jaredwil 0:89cc1300ce7e 41 keypadMap[0x82]='0';
jaredwil 0:89cc1300ce7e 42 keypadMap[0x84]='#';
jaredwil 0:89cc1300ce7e 43 keypadMap[0x88]='D';
jaredwil 0:89cc1300ce7e 44
jaredwil 0:89cc1300ce7e 45 dtmfMap[0x11]="1029,697"; //1
jaredwil 0:89cc1300ce7e 46 dtmfMap[0x12]="1336,697"; //2
jaredwil 0:89cc1300ce7e 47 dtmfMap[0x14]="1477,697"; //3
jaredwil 0:89cc1300ce7e 48 dtmfMap[0x18]="1633,697"; //A
jaredwil 0:89cc1300ce7e 49 dtmfMap[0x21]="1209,770"; //4
jaredwil 0:89cc1300ce7e 50 dtmfMap[0x22]="1336,770"; //5
jaredwil 0:89cc1300ce7e 51 dtmfMap[0x24]="1477,770"; //6
jaredwil 0:89cc1300ce7e 52 dtmfMap[0x28]="1633,770"; //B
jaredwil 0:89cc1300ce7e 53 dtmfMap[0x41]="1209,852"; //7
jaredwil 0:89cc1300ce7e 54 dtmfMap[0x42]="1336,852"; //8
jaredwil 0:89cc1300ce7e 55 dtmfMap[0x44]="1477,852"; //9
jaredwil 0:89cc1300ce7e 56 dtmfMap[0x48]="1633,852"; //C
jaredwil 0:89cc1300ce7e 57 dtmfMap[0x81]="1209,941"; //*
jaredwil 0:89cc1300ce7e 58 dtmfMap[0x82]="1336,941"; //0
jaredwil 0:89cc1300ce7e 59 dtmfMap[0x84]="1477,941"; //#
jaredwil 0:89cc1300ce7e 60 dtmfMap[0x88]="1633,941"; //D
jaredwil 0:89cc1300ce7e 61 }
jaredwil 0:89cc1300ce7e 62
jaredwil 0:89cc1300ce7e 63 int main() {
jaredwil 0:89cc1300ce7e 64 init();
jaredwil 0:89cc1300ce7e 65 //Initialize rows to 0
jaredwil 0:89cc1300ce7e 66 row1 = 0;
jaredwil 0:89cc1300ce7e 67 row2 = 0;
jaredwil 0:89cc1300ce7e 68 row3 = 0;
jaredwil 0:89cc1300ce7e 69 row4 = 0;
jaredwil 0:89cc1300ce7e 70 t1.start();
jaredwil 0:89cc1300ce7e 71 int cur_input = 0x00;
jaredwil 0:89cc1300ce7e 72 int prev_input = 0x00;
jaredwil 0:89cc1300ce7e 73 while(1) {
jaredwil 0:89cc1300ce7e 74 //make all rows 0 to add stability
jaredwil 0:89cc1300ce7e 75 row1 = 0;
jaredwil 0:89cc1300ce7e 76 row2 = 0;
jaredwil 0:89cc1300ce7e 77 row3 = 0;
jaredwil 0:89cc1300ce7e 78 row4 = 0;
jaredwil 0:89cc1300ce7e 79 //Keep each led lit for 4ms
jaredwil 0:89cc1300ce7e 80 switch(t1.read_ms()%4){
jaredwil 0:89cc1300ce7e 81 case 0:
jaredwil 0:89cc1300ce7e 82 row1 = 1;
jaredwil 0:89cc1300ce7e 83 cur_input |= 0x10;
jaredwil 0:89cc1300ce7e 84 break;
jaredwil 0:89cc1300ce7e 85 case 1:
jaredwil 0:89cc1300ce7e 86 row2 = 1;
jaredwil 0:89cc1300ce7e 87 cur_input |= 0x20;
jaredwil 0:89cc1300ce7e 88 break;
jaredwil 0:89cc1300ce7e 89 case 2:
jaredwil 0:89cc1300ce7e 90 row3 = 1;
jaredwil 0:89cc1300ce7e 91 cur_input |= 0x40;
jaredwil 0:89cc1300ce7e 92 break;
jaredwil 0:89cc1300ce7e 93 case 3:
jaredwil 0:89cc1300ce7e 94 row4 = 1;
jaredwil 0:89cc1300ce7e 95 cur_input |= 0x80;
jaredwil 0:89cc1300ce7e 96 break;
jaredwil 0:89cc1300ce7e 97 }
jaredwil 0:89cc1300ce7e 98 if(col1 == 1)
jaredwil 0:89cc1300ce7e 99 cur_input |= 0x01;
jaredwil 0:89cc1300ce7e 100 else if(col2 == 1)
jaredwil 0:89cc1300ce7e 101 cur_input |= 0x02;
jaredwil 0:89cc1300ce7e 102 else if(col3 == 1)
jaredwil 0:89cc1300ce7e 103 cur_input |= 0x04;
jaredwil 0:89cc1300ce7e 104 else if(col4 == 1)
jaredwil 0:89cc1300ce7e 105 cur_input |= 0x08;
jaredwil 0:89cc1300ce7e 106 else cur_input = 0;
jaredwil 0:89cc1300ce7e 107
jaredwil 0:89cc1300ce7e 108
jaredwil 0:89cc1300ce7e 109 if(cur_input) {
jaredwil 0:89cc1300ce7e 110 if(cur_input != prev_input) {
jaredwil 0:89cc1300ce7e 111 char c = keypadMap[cur_input]; //assign charcter based on input
jaredwil 0:89cc1300ce7e 112 if(c != 0){
jaredwil 0:89cc1300ce7e 113 pc.printf("%c\r",c); //print input and char
jaredwil 0:89cc1300ce7e 114 }
jaredwil 0:89cc1300ce7e 115 float freqh,freql;
jaredwil 0:89cc1300ce7e 116 char *freq = dtmfMap[cur_input];
jaredwil 0:89cc1300ce7e 117 sscanf(freq,"%f,%f",&freqh,&freql);
jaredwil 0:89cc1300ce7e 118 pc.printf("High:%f, Low:%f\r",freqh,freql);
jaredwil 0:89cc1300ce7e 119 PWMh.period(1/freqh); // set PWM period to user specified
jaredwil 0:89cc1300ce7e 120 PWMh=0.5; // set duty cycle to 50%
jaredwil 0:89cc1300ce7e 121 PWMl.period(1/freql); // set PWM period to user specified
jaredwil 0:89cc1300ce7e 122 PWMl=0.5; // set duty cycle to 50%
jaredwil 0:89cc1300ce7e 123 prev_input = cur_input;
jaredwil 0:89cc1300ce7e 124 }
jaredwil 0:89cc1300ce7e 125
jaredwil 0:89cc1300ce7e 126 }
jaredwil 0:89cc1300ce7e 127 else {
jaredwil 0:89cc1300ce7e 128 PWMh = 0.0;
jaredwil 0:89cc1300ce7e 129 PWMl = 0.0;
jaredwil 0:89cc1300ce7e 130 }
jaredwil 0:89cc1300ce7e 131 }
jaredwil 0:89cc1300ce7e 132 }