Search code examples
microcontrollerpicmikroc

PIC MikroC - Making a 2x2 Keypad Scanner


I am creating a 2x2 Keypad Scanner where RC0 and RC1 are input lines, and RC2 and RC3 are output lines. I have set TRISC as follows

TRISC = 0b00000011;    

I have then created the keypad scanner method

char keyPadScanner(){
    PORTC.RC2 = HIGH; PORTC.RC3 = LOW;
        if (PORTC.RC0 != 0) return '1';
        if (PORTC.RC1 != 0) return '2';
    PORTC.RC2 = LOW; PORTC.RC3 = HIGH;
        if (PORTC.RC0 != 0) return '4';
        if (PORTC.RC1 != 0) return '5';}

and in the while(1) loop, I have this to display it onto the GLCD Display

test = keyPadScanner();
Delay_Ms(50);
Sm_Glcd_char2(30, 90, test);    

I would just like to know where I am going wrong, I have spent way too long trying to figure this out, if you could just push me in the right direction rather than giving me an answer, that would be great :)

EDIT: HIGH is defined as 1, LOW is defined at 0, and test is just a 'char'

and this is the pseudocode I am following

Set RC2 high, RC3 low
Read RC0. If high, 1 is being pressed
Read RC1. If high, 2 is being pressed
Set RC2 low, RC3 high
Read RC0. If high, 4 is being pressed
Read RC1. If high, 5 is being pressed


Solution

  • For your design similar to yours, I have seen where the pull down resistors are left out thereby leaving PORTC.RC1 and PORTC.RC0 in an unknown state when no buttons are pressed.