new

Dependencies:   mbed

Committer:
psahay
Date:
Sun Feb 22 20:31:19 2015 +0000
Revision:
3:b5c5c73c5bbf
Parent:
2:94a34bcf8f09
minor changes

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 1:461a4f04db9b 8 PwmOut PWMh(p22);
jaredwil 1:461a4f04db9b 9 //because both use same ticker make a new manual pmw
jaredwil 1:461a4f04db9b 10 DigitalOut PWMl(p21);
jaredwil 1:461a4f04db9b 11 Ticker PWMcont;
jaredwil 1:461a4f04db9b 12 //PwmOut PWMl(p21);
jaredwil 0:89cc1300ce7e 13
jaredwil 1:461a4f04db9b 14 DigitalOut row1(p26);
jaredwil 1:461a4f04db9b 15 DigitalOut row2(p23);
jaredwil 1:461a4f04db9b 16 DigitalOut row3(p24);
jaredwil 1:461a4f04db9b 17 DigitalOut row4(p25);
jaredwil 0:89cc1300ce7e 18
jaredwil 0:89cc1300ce7e 19 DigitalIn col1(p20);
jaredwil 0:89cc1300ce7e 20 DigitalIn col2(p19);
jaredwil 0:89cc1300ce7e 21 DigitalIn col3(p18);
jaredwil 0:89cc1300ce7e 22 DigitalIn col4(p17);
jaredwil 0:89cc1300ce7e 23 Timer t1;
jaredwil 0:89cc1300ce7e 24
jaredwil 2:94a34bcf8f09 25 //map for keypad and DTMF
jaredwil 0:89cc1300ce7e 26 map<int, char> keypadMap;
jaredwil 0:89cc1300ce7e 27 map<int, char *> dtmfMap;
jaredwil 0:89cc1300ce7e 28
jaredwil 2:94a34bcf8f09 29 //global on for PWMl (low freqency)
jaredwil 1:461a4f04db9b 30 int on = 0;
jaredwil 1:461a4f04db9b 31
jaredwil 2:94a34bcf8f09 32 //Map for keypad
jaredwil 2:94a34bcf8f09 33 //First 4 bits corrospond to active column
jaredwil 2:94a34bcf8f09 34 //Last 4 bits corrospond to active row
jaredwil 2:94a34bcf8f09 35
jaredwil 2:94a34bcf8f09 36 //DTMF
jaredwil 2:94a34bcf8f09 37 //Maps the current input to appropriate two
jaredwil 2:94a34bcf8f09 38 //frequencies
jaredwil 0:89cc1300ce7e 39 void init(){
jaredwil 0:89cc1300ce7e 40 keypadMap[0x11]='1';
jaredwil 0:89cc1300ce7e 41 keypadMap[0x12]='2';
jaredwil 0:89cc1300ce7e 42 keypadMap[0x14]='3';
jaredwil 0:89cc1300ce7e 43 keypadMap[0x18]='A';
jaredwil 0:89cc1300ce7e 44 keypadMap[0x21]='4';
jaredwil 0:89cc1300ce7e 45 keypadMap[0x22]='5';
jaredwil 0:89cc1300ce7e 46 keypadMap[0x24]='6';
jaredwil 0:89cc1300ce7e 47 keypadMap[0x28]='B';
jaredwil 0:89cc1300ce7e 48 keypadMap[0x41]='7';
jaredwil 0:89cc1300ce7e 49 keypadMap[0x42]='8';
jaredwil 0:89cc1300ce7e 50 keypadMap[0x44]='9';
jaredwil 0:89cc1300ce7e 51 keypadMap[0x48]='C';
jaredwil 0:89cc1300ce7e 52 keypadMap[0x81]='*';
jaredwil 0:89cc1300ce7e 53 keypadMap[0x82]='0';
jaredwil 0:89cc1300ce7e 54 keypadMap[0x84]='#';
jaredwil 0:89cc1300ce7e 55 keypadMap[0x88]='D';
jaredwil 0:89cc1300ce7e 56
jaredwil 0:89cc1300ce7e 57 dtmfMap[0x11]="1029,697"; //1
jaredwil 0:89cc1300ce7e 58 dtmfMap[0x12]="1336,697"; //2
jaredwil 0:89cc1300ce7e 59 dtmfMap[0x14]="1477,697"; //3
jaredwil 0:89cc1300ce7e 60 dtmfMap[0x18]="1633,697"; //A
jaredwil 0:89cc1300ce7e 61 dtmfMap[0x21]="1209,770"; //4
jaredwil 0:89cc1300ce7e 62 dtmfMap[0x22]="1336,770"; //5
jaredwil 0:89cc1300ce7e 63 dtmfMap[0x24]="1477,770"; //6
jaredwil 0:89cc1300ce7e 64 dtmfMap[0x28]="1633,770"; //B
jaredwil 0:89cc1300ce7e 65 dtmfMap[0x41]="1209,852"; //7
jaredwil 0:89cc1300ce7e 66 dtmfMap[0x42]="1336,852"; //8
jaredwil 0:89cc1300ce7e 67 dtmfMap[0x44]="1477,852"; //9
jaredwil 0:89cc1300ce7e 68 dtmfMap[0x48]="1633,852"; //C
jaredwil 0:89cc1300ce7e 69 dtmfMap[0x81]="1209,941"; //*
jaredwil 0:89cc1300ce7e 70 dtmfMap[0x82]="1336,941"; //0
jaredwil 0:89cc1300ce7e 71 dtmfMap[0x84]="1477,941"; //#
jaredwil 0:89cc1300ce7e 72 dtmfMap[0x88]="1633,941"; //D
jaredwil 0:89cc1300ce7e 73 }
jaredwil 0:89cc1300ce7e 74
jaredwil 2:94a34bcf8f09 75 //Flip function is attached to ticker to flip
jaredwil 2:94a34bcf8f09 76 //PWMl (low Frequency)
jaredwil 1:461a4f04db9b 77 void flip() {
jaredwil 1:461a4f04db9b 78 if(on == 1)
jaredwil 1:461a4f04db9b 79 PWMl = !PWMl;
jaredwil 1:461a4f04db9b 80 else
jaredwil 1:461a4f04db9b 81 PWMl = 0;
jaredwil 1:461a4f04db9b 82 }
jaredwil 1:461a4f04db9b 83
jaredwil 0:89cc1300ce7e 84 int main() {
jaredwil 0:89cc1300ce7e 85 init();
jaredwil 0:89cc1300ce7e 86 //Initialize rows to 0
jaredwil 0:89cc1300ce7e 87 row1 = 0;
jaredwil 0:89cc1300ce7e 88 row2 = 0;
jaredwil 0:89cc1300ce7e 89 row3 = 0;
jaredwil 0:89cc1300ce7e 90 row4 = 0;
jaredwil 1:461a4f04db9b 91 PWMl = 0;
jaredwil 0:89cc1300ce7e 92 t1.start();
jaredwil 0:89cc1300ce7e 93 int cur_input = 0x00;
jaredwil 0:89cc1300ce7e 94 int prev_input = 0x00;
jaredwil 1:461a4f04db9b 95
jaredwil 1:461a4f04db9b 96 //main loop
jaredwil 0:89cc1300ce7e 97 while(1) {
jaredwil 1:461a4f04db9b 98
jaredwil 1:461a4f04db9b 99
jaredwil 1:461a4f04db9b 100 //If there was no previous input than switch rows
jaredwil 1:461a4f04db9b 101 if(prev_input == 0){
jaredwil 1:461a4f04db9b 102 //make all rows 0 to add stability
jaredwil 1:461a4f04db9b 103 row1 = 0;
jaredwil 1:461a4f04db9b 104 row2 = 0;
jaredwil 1:461a4f04db9b 105 row3 = 0;
jaredwil 1:461a4f04db9b 106 row4 = 0;
jaredwil 1:461a4f04db9b 107 //Keep each pin on for 4ms
jaredwil 1:461a4f04db9b 108 //"turn on" appropriat bit in map when on.
jaredwil 1:461a4f04db9b 109 switch(t1.read_ms()%4){
jaredwil 1:461a4f04db9b 110 case 0:
jaredwil 1:461a4f04db9b 111 row1 = 1;
jaredwil 1:461a4f04db9b 112 cur_input |= 0x10;
jaredwil 1:461a4f04db9b 113 break;
jaredwil 1:461a4f04db9b 114 case 1:
jaredwil 1:461a4f04db9b 115 row2 = 1;
jaredwil 1:461a4f04db9b 116 cur_input |= 0x20;
jaredwil 1:461a4f04db9b 117 break;
jaredwil 1:461a4f04db9b 118 case 2:
jaredwil 1:461a4f04db9b 119 row3 = 1;
jaredwil 1:461a4f04db9b 120 cur_input |= 0x40;
jaredwil 1:461a4f04db9b 121 break;
jaredwil 1:461a4f04db9b 122 case 3:
jaredwil 1:461a4f04db9b 123 row4 = 1;
jaredwil 1:461a4f04db9b 124 cur_input |= 0x80;
jaredwil 1:461a4f04db9b 125 break;
jaredwil 1:461a4f04db9b 126 }
jaredwil 2:94a34bcf8f09 127 }
jaredwil 1:461a4f04db9b 128 //Check each colum to see if it is high
jaredwil 1:461a4f04db9b 129 //if it is "turn on" that respective bit
jaredwil 1:461a4f04db9b 130 if(col1 == 1)
jaredwil 1:461a4f04db9b 131 cur_input |= 0x01;
jaredwil 1:461a4f04db9b 132 else if(col2 == 1)
jaredwil 1:461a4f04db9b 133 cur_input |= 0x02;
jaredwil 1:461a4f04db9b 134 else if(col3 == 1)
jaredwil 1:461a4f04db9b 135 cur_input |= 0x04;
jaredwil 1:461a4f04db9b 136 else if(col4 == 1)
jaredwil 1:461a4f04db9b 137 cur_input |= 0x08;
jaredwil 1:461a4f04db9b 138 else cur_input = 0; //Detect button release or no input set input to 0
jaredwil 1:461a4f04db9b 139
jaredwil 1:461a4f04db9b 140 //If curr input is non zero continue
jaredwil 1:461a4f04db9b 141 if(cur_input) {
jaredwil 1:461a4f04db9b 142 if(cur_input != prev_input) {
jaredwil 1:461a4f04db9b 143 char c = keypadMap[cur_input]; //assign charcter based on input
jaredwil 1:461a4f04db9b 144 if(c != 0){
jaredwil 1:461a4f04db9b 145 pc.printf("%c\r",c); //print input and char
jaredwil 1:461a4f04db9b 146 }
jaredwil 2:94a34bcf8f09 147 //Assign freql and freqh based on current input
jaredwil 1:461a4f04db9b 148 float freqh,freql;
jaredwil 1:461a4f04db9b 149 char *freq = dtmfMap[cur_input];
jaredwil 1:461a4f04db9b 150 sscanf(freq,"%f,%f",&freqh,&freql);
jaredwil 1:461a4f04db9b 151 pc.printf("High:%f, Low:%f\r",freqh,freql);
jaredwil 2:94a34bcf8f09 152 //Set the low frequency on ticker
jaredwil 1:461a4f04db9b 153 on = 1;
psahay 3:b5c5c73c5bbf 154 // the address of the function to be attached (flip) and the interval (1/freql)
psahay 3:b5c5c73c5bbf 155 PWMcont.attach(&flip, (1/(2*freql)));
jaredwil 2:94a34bcf8f09 156 //Set the high frequency on PWMout
psahay 3:b5c5c73c5bbf 157 PWMh.period(1/freqh); // set PWM period to user specified for high tone
psahay 3:b5c5c73c5bbf 158 PWMh=0.5; // set duty cycle to 50%
jaredwil 1:461a4f04db9b 159 }
jaredwil 0:89cc1300ce7e 160 }
jaredwil 1:461a4f04db9b 161 //Turn off pulse width modulation if curr input is 0
jaredwil 1:461a4f04db9b 162 else {
jaredwil 1:461a4f04db9b 163 PWMh = 0.0;
jaredwil 1:461a4f04db9b 164 on = 0;
jaredwil 1:461a4f04db9b 165 }
jaredwil 1:461a4f04db9b 166
jaredwil 1:461a4f04db9b 167 //Maintain the past input
jaredwil 1:461a4f04db9b 168 prev_input = cur_input;
jaredwil 1:461a4f04db9b 169
jaredwil 1:461a4f04db9b 170
jaredwil 0:89cc1300ce7e 171 }
jaredwil 1:461a4f04db9b 172
jaredwil 0:89cc1300ce7e 173 }