4 x 3 keypad

Dependencies:   mbed

Fork of Prog61006_keypad_kl25z by David Lenihan

Committer:
davidlenihan
Date:
Thu Jul 13 20:05:26 2017 +0000
Revision:
0:c19d2c6de515
Child:
1:ad3491c7b118
Lab prog61006

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davidlenihan 0:c19d2c6de515 1 #include "mbed.h"
davidlenihan 0:c19d2c6de515 2 void setcolhigh(void);
davidlenihan 0:c19d2c6de515 3 void setcol1(void);
davidlenihan 0:c19d2c6de515 4 void setcol2(void);
davidlenihan 0:c19d2c6de515 5 void setcol3(void);
davidlenihan 0:c19d2c6de515 6 char key_pressed(void);
davidlenihan 0:c19d2c6de515 7
davidlenihan 0:c19d2c6de515 8 //Set up the pins for the keypad
davidlenihan 0:c19d2c6de515 9 DigitalOut col1 (PTA16); // pin 3 on keypad
davidlenihan 0:c19d2c6de515 10 DigitalOut col2 (PTE31); // pin 1 on keypad
davidlenihan 0:c19d2c6de515 11 DigitalOut col3 (PTC16); // pin 5 on keypad
davidlenihan 0:c19d2c6de515 12
davidlenihan 0:c19d2c6de515 13 DigitalIn row1 (PTA17); // pin 2 on keypad
davidlenihan 0:c19d2c6de515 14 DigitalIn row2 (PTC12); // pin 7 on keypad
davidlenihan 0:c19d2c6de515 15 DigitalIn row3 (PTC13); // pin 6 on keypad
davidlenihan 0:c19d2c6de515 16 DigitalIn row4 (PTC17); // pin 4 on keypard
davidlenihan 0:c19d2c6de515 17 //Set up the serial communication link
davidlenihan 0:c19d2c6de515 18 Serial pc(USBTX, USBRX); // tx, rx
davidlenihan 0:c19d2c6de515 19
davidlenihan 0:c19d2c6de515 20
davidlenihan 0:c19d2c6de515 21
davidlenihan 0:c19d2c6de515 22 int main()
davidlenihan 0:c19d2c6de515 23 {
davidlenihan 0:c19d2c6de515 24 char my_char;
davidlenihan 0:c19d2c6de515 25 while(1) //infinate loop
davidlenihan 0:c19d2c6de515 26 {
davidlenihan 0:c19d2c6de515 27 my_char = key_pressed(); //Read the value of the key pressed
davidlenihan 0:c19d2c6de515 28 if (my_char !='N')
davidlenihan 0:c19d2c6de515 29 pc.printf("key pressed is %c\n\r",my_char);//print out the key pressed over the serial link
davidlenihan 0:c19d2c6de515 30 wait(0.2);
davidlenihan 0:c19d2c6de515 31 }
davidlenihan 0:c19d2c6de515 32 }
davidlenihan 0:c19d2c6de515 33
davidlenihan 0:c19d2c6de515 34 char key_pressed(void)
davidlenihan 0:c19d2c6de515 35 {
davidlenihan 0:c19d2c6de515 36 char result ='N';
davidlenihan 0:c19d2c6de515 37 setcolhigh();
davidlenihan 0:c19d2c6de515 38 wait(0.2);
davidlenihan 0:c19d2c6de515 39 //check col 0
davidlenihan 0:c19d2c6de515 40 setcol1();
davidlenihan 0:c19d2c6de515 41 wait(0.2);
davidlenihan 0:c19d2c6de515 42 if (row1==0)
davidlenihan 0:c19d2c6de515 43 result = '1';
davidlenihan 0:c19d2c6de515 44 if (row2 ==0)
davidlenihan 0:c19d2c6de515 45 result = '4';
davidlenihan 0:c19d2c6de515 46 if (row3 ==0)
davidlenihan 0:c19d2c6de515 47 result ='7';
davidlenihan 0:c19d2c6de515 48 if (row4 ==0)
davidlenihan 0:c19d2c6de515 49 result = '*';
davidlenihan 0:c19d2c6de515 50
davidlenihan 0:c19d2c6de515 51 //check col 1
davidlenihan 0:c19d2c6de515 52 setcol2();
davidlenihan 0:c19d2c6de515 53 wait(0.2);
davidlenihan 0:c19d2c6de515 54 if (row1==0)
davidlenihan 0:c19d2c6de515 55 result = '2';
davidlenihan 0:c19d2c6de515 56 if (row2 ==0)
davidlenihan 0:c19d2c6de515 57 result = '5';
davidlenihan 0:c19d2c6de515 58 if (row3 ==0)
davidlenihan 0:c19d2c6de515 59 result ='8';
davidlenihan 0:c19d2c6de515 60 if (row4 ==0)
davidlenihan 0:c19d2c6de515 61 result = '0';
davidlenihan 0:c19d2c6de515 62
davidlenihan 0:c19d2c6de515 63 //check col 2
davidlenihan 0:c19d2c6de515 64 setcol3();
davidlenihan 0:c19d2c6de515 65 wait(0.2);
davidlenihan 0:c19d2c6de515 66 if (row1==0)
davidlenihan 0:c19d2c6de515 67 result = '3';
davidlenihan 0:c19d2c6de515 68 if (row2 ==0)
davidlenihan 0:c19d2c6de515 69 result = '6';
davidlenihan 0:c19d2c6de515 70 if (row3 ==0)
davidlenihan 0:c19d2c6de515 71 result ='9';
davidlenihan 0:c19d2c6de515 72 if (row4 ==0)
davidlenihan 0:c19d2c6de515 73 result = '?';
davidlenihan 0:c19d2c6de515 74 return result;
davidlenihan 0:c19d2c6de515 75 }
davidlenihan 0:c19d2c6de515 76
davidlenihan 0:c19d2c6de515 77 void setcolhigh()
davidlenihan 0:c19d2c6de515 78 {
davidlenihan 0:c19d2c6de515 79 //Set all the columns high
davidlenihan 0:c19d2c6de515 80
davidlenihan 0:c19d2c6de515 81 col1 = 1;
davidlenihan 0:c19d2c6de515 82 col2 = 1;
davidlenihan 0:c19d2c6de515 83 col3 = 1;
davidlenihan 0:c19d2c6de515 84
davidlenihan 0:c19d2c6de515 85 }
davidlenihan 0:c19d2c6de515 86 void setcol1()
davidlenihan 0:c19d2c6de515 87 {
davidlenihan 0:c19d2c6de515 88 //Set column 1 low
davidlenihan 0:c19d2c6de515 89 col1 = 0;
davidlenihan 0:c19d2c6de515 90 col2 = 1;
davidlenihan 0:c19d2c6de515 91 col3 = 1;
davidlenihan 0:c19d2c6de515 92 }
davidlenihan 0:c19d2c6de515 93 void setcol2()
davidlenihan 0:c19d2c6de515 94 {
davidlenihan 0:c19d2c6de515 95 //Set column 2 low
davidlenihan 0:c19d2c6de515 96 col1 = 1;
davidlenihan 0:c19d2c6de515 97 col2 = 0;
davidlenihan 0:c19d2c6de515 98 col3 = 1;
davidlenihan 0:c19d2c6de515 99 }
davidlenihan 0:c19d2c6de515 100 void setcol3()
davidlenihan 0:c19d2c6de515 101 {
davidlenihan 0:c19d2c6de515 102 //set column 3 low
davidlenihan 0:c19d2c6de515 103 col1 = 1;
davidlenihan 0:c19d2c6de515 104 col2 = 1;
davidlenihan 0:c19d2c6de515 105 col3 = 0;
davidlenihan 0:c19d2c6de515 106 }