4 x 3 keypad

Dependencies:   mbed

Fork of Prog61006_keypad_kl25z by David Lenihan

Committer:
davidlenihan
Date:
Fri Jul 14 12:20:34 2017 +0000
Revision:
1:ad3491c7b118
Parent:
0:c19d2c6de515
comments

Who changed what in which revision?

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