new

Dependencies:   mbed

Revision:
0:89cc1300ce7e
Child:
1:461a4f04db9b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Feb 17 21:31:58 2015 +0000
@@ -0,0 +1,132 @@
+#include "mbed.h"
+#include <map>
+#include <string>
+
+//Initialize serial comms
+Serial pc(USBTX, USBRX);
+
+PwmOut PWMh(p26);
+PwmOut PWMl(p21);
+
+DigitalOut row1(p25);
+DigitalOut row2(p22);
+DigitalOut row3(p23);
+DigitalOut row4(p24);
+
+DigitalIn col1(p20);
+DigitalIn col2(p19);
+DigitalIn col3(p18);
+DigitalIn col4(p17);
+Timer t1;
+
+//map
+map<int, char> keypadMap;
+map<int, char *> dtmfMap; 
+
+//Mores Code -> Character Map
+void init(){
+        keypadMap[0x11]='1';
+        keypadMap[0x12]='2';
+        keypadMap[0x14]='3';
+        keypadMap[0x18]='A';
+        keypadMap[0x21]='4';
+        keypadMap[0x22]='5';
+        keypadMap[0x24]='6';
+        keypadMap[0x28]='B';
+        keypadMap[0x41]='7';
+        keypadMap[0x42]='8';
+        keypadMap[0x44]='9';
+        keypadMap[0x48]='C';
+        keypadMap[0x81]='*';
+        keypadMap[0x82]='0';
+        keypadMap[0x84]='#';
+        keypadMap[0x88]='D';
+        
+        dtmfMap[0x11]="1029,697"; //1
+        dtmfMap[0x12]="1336,697"; //2
+        dtmfMap[0x14]="1477,697"; //3
+        dtmfMap[0x18]="1633,697"; //A
+        dtmfMap[0x21]="1209,770"; //4
+        dtmfMap[0x22]="1336,770"; //5
+        dtmfMap[0x24]="1477,770"; //6
+        dtmfMap[0x28]="1633,770"; //B
+        dtmfMap[0x41]="1209,852"; //7
+        dtmfMap[0x42]="1336,852"; //8
+        dtmfMap[0x44]="1477,852"; //9
+        dtmfMap[0x48]="1633,852"; //C
+        dtmfMap[0x81]="1209,941"; //*
+        dtmfMap[0x82]="1336,941"; //0
+        dtmfMap[0x84]="1477,941"; //#
+        dtmfMap[0x88]="1633,941"; //D
+}
+
+int main() {
+    init();
+    //Initialize rows to 0
+    row1 = 0;
+    row2 = 0;
+    row3 = 0;
+    row4 = 0;
+    t1.start();
+    int cur_input = 0x00;
+    int prev_input = 0x00;
+    while(1) {
+        //make all rows 0 to add stability
+        row1 = 0;
+        row2 = 0;
+        row3 = 0;
+        row4 = 0;
+       //Keep each led lit for 4ms
+       switch(t1.read_ms()%4){
+                        case 0:
+                        row1 = 1;
+                        cur_input |= 0x10;   
+                        break;
+                        case 1:
+                        row2 = 1;
+                        cur_input |= 0x20;
+                        break;
+                        case 2:
+                        row3 = 1;
+                        cur_input |= 0x40;
+                        break;
+                        case 3:
+                        row4 = 1; 
+                        cur_input |= 0x80;
+                        break; 
+                        }
+     if(col1 == 1)
+     cur_input |= 0x01;
+     else if(col2 == 1)
+     cur_input |= 0x02;
+     else if(col3 == 1)
+     cur_input |= 0x04;
+     else if(col4 == 1)
+     cur_input |= 0x08;
+     else cur_input = 0;
+     
+     
+     if(cur_input) {
+        if(cur_input != prev_input) {
+            char c = keypadMap[cur_input]; //assign charcter based on input
+            if(c != 0){
+                pc.printf("%c\r",c);  //print input and char
+            }
+            float freqh,freql;
+            char *freq = dtmfMap[cur_input];
+            sscanf(freq,"%f,%f",&freqh,&freql);
+            pc.printf("High:%f, Low:%f\r",freqh,freql);
+            PWMh.period(1/freqh);               // set PWM period to user specified   
+            PWMh=0.5;                         // set duty cycle to 50% 
+            PWMl.period(1/freql);               // set PWM period to user specified   
+            PWMl=0.5;                         // set duty cycle to 50%
+            prev_input = cur_input;
+        }
+         
+    }
+    else {
+        PWMh = 0.0;
+        PWMl = 0.0;
+     } 
+    }
+}
\ No newline at end of file