Wenrui Meng
/
ese519-hw2-part4
ese519 hw2 part 4
Diff: main.cpp
- Revision:
- 0:cfa060762ad7
- Child:
- 1:e004fff01d0d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Feb 17 20:35:43 2015 +0000 @@ -0,0 +1,72 @@ +#include "mbed.h" + +PwmOut buzzer(p21); +PwmOut speaker(p26); + +Serial pc(USBTX, USBRX); +DigitalOut cols[4]= {p20,p19,p18,p17}; +InterruptIn rows[4]={p25,p22,p23,p24}; + +double HZc[] = { 1209.0, 1336.0, 1477.0, 1633.0}; // Colum +double HZr[] = {697.0, 770.0, 852.0, 941.0}; //Row +char Keytable[] = { '1', '2', '3', 'A', + '4', '5', '6', 'B', + '7', '8', '9', 'C', + '*', '0', '#', 'D' + }; + +void start(int row){ + double high = 0, low = 0; + if(rows[row].read() == 0) return; + for(int i = 0; i < 4; ++i){ + cols[i].write(0); + if(rows[row].read() == 0){ + high = HZc[i]; + low = HZr[row]; + pc.printf("input row %d col %d %c \n", row, i, Keytable[row * 4 + i]); + break; + } + } + buzzer.period(low); + speaker.period(high); + buzzer = 0.5; + speaker = 0.5; +} + +void stop(){ + buzzer.period(0); + speaker.period(0); + buzzer = 0; + speaker = 0; + for(int i = 0; i < 4; ++i) cols[i] = 1; +} + + + +void row0Rise(){ + start(0); +} +void row1Rise(){ + start(1); +} +void row2Rise(){ + start(2); +} +void row3Rise(){ + start(3); +} + +void rowFall(){ + stop(); +} + +int main() { + rows[0].rise(&row0Rise); + rows[1].rise(&row1Rise); + rows[2].rise(&row2Rise); + rows[3].rise(&row3Rise); + for(int i = 0; i < 4; ++i) rows[i].fall(&rowFall); + while(1) { + ; + } +}