Yifeng Yuan
/
ese519_lab2_part3
part3 update
Revision 2:75fb721a5b86, committed 2015-02-17
- Comitter:
- yifeng021
- Date:
- Tue Feb 17 18:48:53 2015 +0000
- Parent:
- 1:1260dba917b0
- Commit message:
- part3 ticker 2-17-2015 1:50pm uploaded by Yifeng
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 1260dba917b0 -r 75fb721a5b86 main.cpp --- a/main.cpp Fri Feb 13 19:53:09 2015 +0000 +++ b/main.cpp Tue Feb 17 18:48:53 2015 +0000 @@ -1,7 +1,13 @@ +// This version does not work. +// The issue is: if I only scan first row, it works. However, when I essentially copy +// the first case to the rest of the cases(or rows), program does not recognize any +// input anymore. + #include "mbed.h" #include "string.h" // speaker sound effect demo using PWM hardware output -PwmOut speaker(p21);//pwm output +PwmOut keytone_row(p21);//pwm output for key tone table row freq +PwmOut keytone_col(p22);//pwm output for key tone table column freq Serial usb(USBTX, USBRX);//serial comm DigitalOut row8(p8);//pin8 on keypad board->row1(top) DigitalOut row1(p5);//pin1 ...->row2 @@ -11,14 +17,12 @@ InterruptIn col6(p28);//pin6->column 2 InterruptIn col5(p29);//pin5->column 3 InterruptIn col3(p30);//pin3->column 4 - -unsigned short global_timer;//global variable -unsigned short global_key_index;//global variable, value range:0 - 15, represents for 16 keys +Ticker t; -extern "C" void TIMER0_IRQHandler (void); -void timer0_init(void);//initialize timer0 -void play_tone(int freq, int dur);//takes in frequency & duration and plays the tone accodingly -bool check_buffer_format(char *buffer);//check input string format +int global_key_index;//global variable, value range:0 - 15, represents for 16 keys +unsigned short global_flag_row; +int global_count; + // The following functions determine which key is pressed by changing global variable 'global_key_index' void key_1(); void key_2(); @@ -37,192 +41,187 @@ void key_pd(); void key_D(); +void scan_row(); +void print_key(); +void key_release(); + int main() { - timer0_init(); - global_timer = 0; - row8 = 0; - row1 = 0; - row2 = 0; - row4 = 0; + //usb.printf("hello world!"); + float dur_row = 4000.0; //time to scan each row in us + global_count = 0; + global_flag_row = 0; + global_key_index = 16; + + row8 = 1; + row1 = 1; + row2 = 1; + row4 = 1; + + t.attach_us(&scan_row, dur_row); while (1) { - if (global_timer < 4) { - //set row1 to high for 4ms - row8 = 1; - //scan 4 columns - col3.fall(&key_1); - col5.fall(&key_2); - col6.fall(&key_3); - col7.fall(&key_A); - } - if (global_timer >= 4 && global_timer < 8){ - //set row2 to high for 4ms - row8 = 0; - row2 = 0; - row4 = 0; - row1 = 1; - //scan 4 columns - col3.fall(&key_4); - col5.fall(&key_5); - col6.fall(&key_6); - col7.fall(&key_B); - } - if (global_timer >= 8 && global_timer < 12) { - //set row3 high for 4ms - row8 = 0; - row1 = 0; - row4 = 0; - row2 = 1; - //scan 4 columns - col3.fall(&key_7); - col5.fall(&key_8); - col6.fall(&key_9); - col7.fall(&key_C); - } - if (global_timer >= 12 && global_timer < 16) { - //set row4 high for 4ms - row8 = 0; - row1 = 0; - row2 = 0; - row4 = 1; - //scan 4 columns - col3.fall(&key_str); - col5.fall(&key_0); - col6.fall(&key_pd); - col7.fall(&key_D); - } - if (global_timer >= 16) { - row8 = 0; - row1 = 0; - row2 = 0; - row4 = 0; - global_timer = 0; - } + usb.printf("%d",global_key_index); + //print_key(); + //wait(0.2); } } -//source: https://developer.mbed.org/users/microguy/notebook/timer0-example-code/ -extern "C" void TIMER0_IRQHandler (void) -{ -if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed - { - LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag - global_timer++; //increment timer_count - } -} - -//source: https://developer.mbed.org/users/microguy/notebook/timer0-example-code/ -// This function initializes timer0 -void timer0_init(void) -{ - LPC_SC->PCONP |=1<1; //timer0 power on - LPC_TIM0->MR0 = 23980; //1 msec(verified with oscilloscope) - LPC_TIM0->MCR = 3; //interrupt and reset control - //3 = Interrupt & reset timer0 on match - //1 = Interrupt only, no reset of timer0 - NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt - LPC_TIM0->TCR = 1; //enable Timer0 - printf("Done timer_init\n\r"); -} - -// This function plays a tone for a certain duration -void play_tone(int freq, int dur) -{ - speaker.period(1.0/freq); // 500hz period - speaker = 0.2; //50% duty cycle - max volume - wait(dur/1000.0); - speaker=0.0; // turn off audio -} - -// This function checks the user input format of frequency & duration -bool check_buffer_format(char *buffer) -{ - bool flag = true;//true - valid input; false - invalid input - int buffer_length = strlen(buffer); - int i = 0; - for (i=0; i<buffer_length; i++) { - //check if buffer[0:2] & buffer[4:6] are #s - if (i==0 || i==1 || i==2 || i==4 || i==5 || i==6){ - if (buffer[i] < '0' || buffer[i] > '9'){ - flag = false; - } - } - //check if buffer[3] is a 'space' - else if (i==3){ - if (buffer[i] != ' '){ - flag = false; - } - } - } - return flag; -} - // The following functions determine which key is pressed by changing global variable 'global_key_index' //indice: //key-[index] //1-[0],2-[1],3-[2],A-[3],4-[4],5-[5],6-[6],B-[7],7-[8],8-[9],9-[10],C-[11],*-[12],0-[13],#-[14],D-[15] -void key_1(){ - global_key_index = 0; - printf("1 is pressed!\n"); -} -void key_2(){ - global_key_index = 1; - printf("2 is pressed!\n"); -} -void key_3(){ - global_key_index = 2; - printf("3 is pressed!\n"); -} -void key_A(){ - global_key_index = 3; - printf("A is pressed!\n"); -} -void key_4(){ - global_key_index = 4; - printf("4 is pressed!\n"); -} -void key_5(){ - global_key_index = 5; - printf("5 is pressed!\n"); -} -void key_6(){ - global_key_index = 6; - printf("6 is pressed!\n"); -} -void key_B(){ - global_key_index = 7; - printf("B is pressed!\n"); +void key_1(){global_key_index = 0;} +void key_2(){global_key_index = 1;} +void key_3(){global_key_index = 2;} +void key_A(){global_key_index = 3;} +void key_4(){global_key_index = 4;} +void key_5(){global_key_index = 5;} +void key_6(){global_key_index = 6;} +void key_B(){global_key_index = 7;} +void key_7(){global_key_index = 8;} +void key_8(){global_key_index = 9;} +void key_9(){global_key_index = 10;} +void key_C(){global_key_index = 11;} +void key_str(){global_key_index = 12;} +void key_0(){global_key_index = 13;} +void key_pd(){global_key_index = 14;} +void key_D(){global_key_index = 15;} + +void scan_row() +{ + global_flag_row = global_count % 4; + switch(global_flag_row) { + case 0: + //printf("case0"); + row8 = 0; + row1 = 1; + row2 = 1; + row4 = 1; + col3.rise(&key_1); + col5.rise(&key_2); + col6.rise(&key_3); + col7.rise(&key_A); + col3.fall(&key_release); + col5.fall(&key_release); + col6.fall(&key_release); + col7.fall(&key_release); + break; + + case 1: + //printf("case1"); + row8 = 1; + row1 = 0; + row2 = 1; + row4 = 1; + /* + col3.rise(&key_4); + col5.rise(&key_5); + col6.rise(&key_6); + col7.rise(&key_B); + col3.fall(&key_release); + col5.fall(&key_release); + col6.fall(&key_release); + col7.fall(&key_release); + */ + break; + case 2: + //printf("case2"); + row8 = 1; + row1 = 1; + row2 = 0; + row4 = 1; + /* + col3.rise(&key_7); + col5.rise(&key_8); + col6.rise(&key_9); + col7.rise(&key_C); + col3.fall(&key_release); + col5.fall(&key_release); + col6.fall(&key_release); + col7.fall(&key_release); + */ + break; + case 3: + //printf("case3"); + row8 = 1; + row1 = 1; + row2 = 1; + row4 = 0; + /* + col3.rise(&key_str); + col5.rise(&key_0); + col6.rise(&key_pd); + col7.rise(&key_D); + col3.fall(&key_release); + col5.fall(&key_release); + col6.fall(&key_release); + col7.fall(&key_release); + */ + break; + + } + global_count += 1; } -void key_7(){ - global_key_index = 8; - printf("7 is pressed!\n"); -} -void key_8(){ - global_key_index = 9; - printf("8 is pressed!\n"); -} -void key_9(){ - global_key_index = 10; - printf("9 is pressed!\n"); -} -void key_C(){ - global_key_index = 11; - printf("C is pressed!\n"); + +void print_key() +{ + switch(global_key_index) { + case 0: + usb.printf("1"); + break; + case 1: + usb.printf("2"); + break; + case 2: + usb.printf("3"); + break; + case 3: + usb.printf("A"); + break; + case 4: + usb.printf("4"); + break; + case 5: + usb.printf("5"); + break; + case 6: + usb.printf("6"); + break; + case 7: + usb.printf("B"); + break; + case 8: + usb.printf("7"); + break; + case 9: + usb.printf("8"); + break; + case 10: + usb.printf("9"); + break; + case 11: + usb.printf("C"); + break; + case 12: + usb.printf("*"); + break; + case 13: + usb.printf("0"); + break; + case 14: + usb.printf("#"); + break; + case 15: + usb.printf("D"); + break; + case 16: + //usb.printf("default!"); + break; + } } -void key_str(){ - global_key_index = 12; - printf("* is pressed!\n"); -} -void key_0(){ - global_key_index = 13; - printf("0 is pressed!\n"); + +void key_release() +{ + global_key_index = 16; } -void key_pd(){ - global_key_index = 14; - printf("# is pressed!\n"); -} -void key_D(){ - global_key_index = 15; - printf("D is pressed!\n"); -} \ No newline at end of file