4 x 3 keypad

Dependencies:   mbed

Fork of Prog61006_keypad_kl25z by David Lenihan

main.cpp

Committer:
davidlenihan
Date:
2017-07-14
Revision:
1:ad3491c7b118
Parent:
0:c19d2c6de515

File content as of revision 1:ad3491c7b118:

/*Keypad file for prog 61006 lab*/

#include "mbed.h"
void setcolhigh(void);
void setcol1(void);
void setcol2(void);
void setcol3(void);
char key_pressed(void);

//Set up the pins for the keypad
DigitalOut col1     (PTA16);    // pin 3 on keypad
DigitalOut col2     (PTE31);    // pin 1 on keypad
DigitalOut col3     (PTC16);    // pin 5 on keypad

DigitalIn row1      (PTA17);    // pin 2 on keypad
DigitalIn row2      (PTC12);    // pin 7 on keypad
DigitalIn row3      (PTC13);    // pin 6 on keypad
DigitalIn row4      (PTC17);    // pin 4 on keypard
//Set up the serial communication link
Serial pc(USBTX, USBRX); // tx, rx



int main()
{
    char my_char;
    while(1) //infinate loop
    {
       my_char = key_pressed(); //Read the value of the key pressed
       if (my_char !='N')
       pc.printf("key pressed is %c\n\r",my_char);//print out the key pressed over the serial link
        wait(0.2);
    }
}

char key_pressed(void)
{
    char result ='N'; 
    setcolhigh();
    wait(0.2);
    //check col 0
    setcol1();
    wait(0.2);
    if (row1==0)
        result = '1';
    if (row2 ==0)
        result = '4';
    if (row3 ==0)
        result ='7';
    if (row4 ==0)
        result = '*';
        
    //check col 1
    setcol2();
    wait(0.2);
    if (row1==0)
        result = '2';
    if (row2 ==0)
        result = '5';
    if (row3 ==0)
        result ='8';
    if (row4 ==0)
        result = '0';
    
    //check col 2
    setcol3();
    wait(0.2);
    if (row1==0)
        result = '3';
    if (row2 ==0)
        result = '6';
    if (row3 ==0)
        result ='9';
    if (row4 ==0)
        result = '?';
    return result;
}

void setcolhigh()
{
    //Set all the columns high

    col1 = 1;
    col2 = 1;
    col3 = 1;

}
void setcol1()
{
    //Set column 1 low
    col1 = 0;
    col2 = 1;
    col3 = 1;
}
void setcol2()
{
    //Set column 2 low
    col1 = 1;
    col2 = 0;
    col3 = 1;
}
void setcol3()
{
    //set column 3 low
    col1 = 1;
    col2 = 1;
    col3 = 0;
}