part3 update

Dependencies:   mbed

Revision:
1:1260dba917b0
Parent:
0:7c6b15d40e00
Child:
2:75fb721a5b86
--- a/main.cpp	Thu Feb 12 23:52:52 2015 +0000
+++ b/main.cpp	Fri Feb 13 19:53:09 2015 +0000
@@ -1,40 +1,45 @@
 #include "mbed.h"
 #include "string.h"
 // speaker sound effect demo using PWM hardware output
-PwmOut speaker(p21);
-Serial usb(USBTX, USBRX);
-DigitalOut row8(p8);
-DigitalOut row1(p5);
-DigitalOut row2(p6);
-DigitalOut row4(p7);
-InterruptIn col7(p27);
-InterruptIn col6(p28);
-InterruptIn col5(p29);
-InterruptIn col3(p30);
+PwmOut speaker(p21);//pwm output
+Serial usb(USBTX, USBRX);//serial comm
+DigitalOut row8(p8);//pin8 on keypad board->row1(top)
+DigitalOut row1(p5);//pin1 ...->row2
+DigitalOut row2(p6);//pin2->row3
+DigitalOut row4(p7);//pin4->row4
+InterruptIn col7(p27);//pin7->column 1(right-most column)
+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;
+unsigned short global_key_index;//global variable, value range:0 - 15, represents for 16 keys
 
 extern "C" void TIMER0_IRQHandler (void);
-void timer0_init(void);
-void play_tone(int freq, int dur);
-bool check_buffer_format(char *buffer);
-//void scan_all_rows();
+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
+// The following functions determine which key is pressed by changing global variable 'global_key_index'
 void key_1();
 void key_2();
 void key_3();
 void key_A();
-
-
+void key_4();
+void key_5();
+void key_6();
+void key_B();
+void key_7();
+void key_8();
+void key_9();
+void key_C();
+void key_str();
+void key_0();
+void key_pd();
+void key_D();
 
 int main()
 {
-    //printf("Gello world!\n");
-    
-    //en8.rise(&check_row8);
-    //en1.rise(&check_row1);
-    //en2.rise(&check_row2);
-    //en4.rise(&check_row4);
+    timer0_init();
     global_timer = 0;
     row8 = 0;
     row1 = 0;
@@ -43,72 +48,61 @@
     
     while (1) {
         if (global_timer < 4) {
+            //set row1 to high for 4ms
             row8 = 1;
-            col3.rise(&key_1);
-            col5.rise(&key_2);
-            col6.rise(&key_3);
-            col7.rise(&key_A);
+            //scan 4 columns
+            col3.fall(&key_1);
+            col5.fall(&key_2);
+            col6.fall(&key_3);
+            col7.fall(&key_A);    
         }
-        else if (global_timer < 8){
+        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); 
         }
-        else if (global_timer < 12) {
+        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); 
         }
-        else if (global_timer < 16) {
+        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); 
         }
-        else {
+        if (global_timer >= 16) {
+            row8 = 0;
+            row1 = 0;
+            row2 = 0;
             row4 = 0;
             global_timer = 0;
         }
     }
-            
-
-    
-    /*
-    while (1) {   
-        speaker = 0.0;
-        int freq = 0; 
-        int dur = 0;
-        int i;
-        const int buffer_length = 7; //this is the actual buffer length, excluding the '\0' in the end
-        bool flag = true;
-        char buffer[128];
-        usb.gets(buffer, buffer_length+1);
-        //usb.puts(buffer);
-        //check if input is valid
-        if (check_buffer_format(buffer) == false){
-            printf("Invalid input format, please re-enter:\n");
-            continue;
-            }
-            
-            
-        
-        //convert input string to two numbers, freq & dur
-        for (i=0; i < buffer_length; i++) {
-            //flag indicades whether the string represents frequency or duration
-            if (buffer[i] == ' ') {
-                flag = false;
-                continue;
-            }
-            if (flag) {
-                freq *= 10;
-                freq += buffer[i] - '0';
-            } else {
-                dur *= 10;
-                dur += buffer[i] - '0';
-            }
-        }
-        //usb.printf("freq: %d; dur: %d\n", freq, dur);
-        play_tone(freq, dur);
-    }
-    */
 }
 
+//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
@@ -118,10 +112,12 @@
     }
 }
 
+//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(needs to be verified)
+    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
@@ -130,6 +126,7 @@
     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
@@ -138,6 +135,7 @@
     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
@@ -160,33 +158,10 @@
     return flag;
 }
 
-/*
-void scan_all_rows()
-{
-    timer0_init();
-    global_timer = 0;
-    while(1)
-    {
-        en8.fall(&check_row8);
-        if (global_timer > 4) {
-            en1.fall(&check_row1);
-            //scan next row
-            if (global_timer > 8) {
-                en2.fall(&check_row2);
-                //scan next row
-                if (global_timer > 12) {
-                    en4.fall(&check_row4);
-                    if (global_timer > 16) {
-                        break;
-                    }
-                }
-            }
-        }
-    }
-    usb.printf("row %d is pressed!\n",global_row_index);
-    global_timer = 0;
-}
-*/
+// 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");
@@ -202,4 +177,52 @@
 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_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 key_str(){
+    global_key_index = 12;
+    printf("* is pressed!\n");
+}
+void key_0(){
+    global_key_index = 13;
+    printf("0 is pressed!\n");
+}
+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