![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ese519 hw2 part 4
main.cpp@0:cfa060762ad7, 2015-02-17 (annotated)
- Committer:
- wenrui
- Date:
- Tue Feb 17 20:35:43 2015 +0000
- Revision:
- 0:cfa060762ad7
- Child:
- 1:e004fff01d0d
part4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wenrui | 0:cfa060762ad7 | 1 | #include "mbed.h" |
wenrui | 0:cfa060762ad7 | 2 | |
wenrui | 0:cfa060762ad7 | 3 | PwmOut buzzer(p21); |
wenrui | 0:cfa060762ad7 | 4 | PwmOut speaker(p26); |
wenrui | 0:cfa060762ad7 | 5 | |
wenrui | 0:cfa060762ad7 | 6 | Serial pc(USBTX, USBRX); |
wenrui | 0:cfa060762ad7 | 7 | DigitalOut cols[4]= {p20,p19,p18,p17}; |
wenrui | 0:cfa060762ad7 | 8 | InterruptIn rows[4]={p25,p22,p23,p24}; |
wenrui | 0:cfa060762ad7 | 9 | |
wenrui | 0:cfa060762ad7 | 10 | double HZc[] = { 1209.0, 1336.0, 1477.0, 1633.0}; // Colum |
wenrui | 0:cfa060762ad7 | 11 | double HZr[] = {697.0, 770.0, 852.0, 941.0}; //Row |
wenrui | 0:cfa060762ad7 | 12 | char Keytable[] = { '1', '2', '3', 'A', |
wenrui | 0:cfa060762ad7 | 13 | '4', '5', '6', 'B', |
wenrui | 0:cfa060762ad7 | 14 | '7', '8', '9', 'C', |
wenrui | 0:cfa060762ad7 | 15 | '*', '0', '#', 'D' |
wenrui | 0:cfa060762ad7 | 16 | }; |
wenrui | 0:cfa060762ad7 | 17 | |
wenrui | 0:cfa060762ad7 | 18 | void start(int row){ |
wenrui | 0:cfa060762ad7 | 19 | double high = 0, low = 0; |
wenrui | 0:cfa060762ad7 | 20 | if(rows[row].read() == 0) return; |
wenrui | 0:cfa060762ad7 | 21 | for(int i = 0; i < 4; ++i){ |
wenrui | 0:cfa060762ad7 | 22 | cols[i].write(0); |
wenrui | 0:cfa060762ad7 | 23 | if(rows[row].read() == 0){ |
wenrui | 0:cfa060762ad7 | 24 | high = HZc[i]; |
wenrui | 0:cfa060762ad7 | 25 | low = HZr[row]; |
wenrui | 0:cfa060762ad7 | 26 | pc.printf("input row %d col %d %c \n", row, i, Keytable[row * 4 + i]); |
wenrui | 0:cfa060762ad7 | 27 | break; |
wenrui | 0:cfa060762ad7 | 28 | } |
wenrui | 0:cfa060762ad7 | 29 | } |
wenrui | 0:cfa060762ad7 | 30 | buzzer.period(low); |
wenrui | 0:cfa060762ad7 | 31 | speaker.period(high); |
wenrui | 0:cfa060762ad7 | 32 | buzzer = 0.5; |
wenrui | 0:cfa060762ad7 | 33 | speaker = 0.5; |
wenrui | 0:cfa060762ad7 | 34 | } |
wenrui | 0:cfa060762ad7 | 35 | |
wenrui | 0:cfa060762ad7 | 36 | void stop(){ |
wenrui | 0:cfa060762ad7 | 37 | buzzer.period(0); |
wenrui | 0:cfa060762ad7 | 38 | speaker.period(0); |
wenrui | 0:cfa060762ad7 | 39 | buzzer = 0; |
wenrui | 0:cfa060762ad7 | 40 | speaker = 0; |
wenrui | 0:cfa060762ad7 | 41 | for(int i = 0; i < 4; ++i) cols[i] = 1; |
wenrui | 0:cfa060762ad7 | 42 | } |
wenrui | 0:cfa060762ad7 | 43 | |
wenrui | 0:cfa060762ad7 | 44 | |
wenrui | 0:cfa060762ad7 | 45 | |
wenrui | 0:cfa060762ad7 | 46 | void row0Rise(){ |
wenrui | 0:cfa060762ad7 | 47 | start(0); |
wenrui | 0:cfa060762ad7 | 48 | } |
wenrui | 0:cfa060762ad7 | 49 | void row1Rise(){ |
wenrui | 0:cfa060762ad7 | 50 | start(1); |
wenrui | 0:cfa060762ad7 | 51 | } |
wenrui | 0:cfa060762ad7 | 52 | void row2Rise(){ |
wenrui | 0:cfa060762ad7 | 53 | start(2); |
wenrui | 0:cfa060762ad7 | 54 | } |
wenrui | 0:cfa060762ad7 | 55 | void row3Rise(){ |
wenrui | 0:cfa060762ad7 | 56 | start(3); |
wenrui | 0:cfa060762ad7 | 57 | } |
wenrui | 0:cfa060762ad7 | 58 | |
wenrui | 0:cfa060762ad7 | 59 | void rowFall(){ |
wenrui | 0:cfa060762ad7 | 60 | stop(); |
wenrui | 0:cfa060762ad7 | 61 | } |
wenrui | 0:cfa060762ad7 | 62 | |
wenrui | 0:cfa060762ad7 | 63 | int main() { |
wenrui | 0:cfa060762ad7 | 64 | rows[0].rise(&row0Rise); |
wenrui | 0:cfa060762ad7 | 65 | rows[1].rise(&row1Rise); |
wenrui | 0:cfa060762ad7 | 66 | rows[2].rise(&row2Rise); |
wenrui | 0:cfa060762ad7 | 67 | rows[3].rise(&row3Rise); |
wenrui | 0:cfa060762ad7 | 68 | for(int i = 0; i < 4; ++i) rows[i].fall(&rowFall); |
wenrui | 0:cfa060762ad7 | 69 | while(1) { |
wenrui | 0:cfa060762ad7 | 70 | ; |
wenrui | 0:cfa060762ad7 | 71 | } |
wenrui | 0:cfa060762ad7 | 72 | } |