1

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

Joeliomason
  • 159
  • 2
  • 16

1 Answers1

0

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.

st2000
  • 286
  • 1
  • 16
  • Thanks for your answer, but could you elaborate it a bit? I have now edited the code to char keyPadScanner(){ char var; char out; if ((PORTC & 0x3) == 0) out = '0'; //very quick exit if nothing pressed PORTC.RC2 = HIGH; PORTC.RC3 = LOW; var = PORTC & TRISC; if ((var) == 0x1) out = '1'; if ((var) == 0x2) out = '2'; PORTC.RC2 = LOW; PORTC.RC3 = HIGH; var = PORTC & TRISC; if ((var) == 0x1) out = '4'; if ((var) == 0x2) out = '5'; return out; (I dont know how to format it in the comments sorry :( – Joeliomason Jan 05 '15 at 10:36
  • My answer has nothing to do with your software. You need to make sure that open inputs are not floating. To do this you need pull up or pull down resistors. In your original software it would appear you need pull down resistors. [If you find this helpful could you please up vote my answer. I've been running short of creditability which makes it difficult to answer questions here at stackoverflow.] – st2000 Jan 05 '15 at 19:09