James Child
Newbie level 3
- Joined
- Dec 10, 2014
- Messages
- 4
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 13
- Location
- Hatfield, Hertfordshire
- Activity points
- 56
A standard keypad has this arrangement:
4 5
1 2
so you only need 2x2 and four bits of the port are sufficient. The principle is to set the specific bits driving the keypad to the states 01 then 10 while reading back on the other two pins. You may need pull-up or pull-down resistors on the inputs so they go to the inactive state when keys are not pressed. To make a pin high, you OR the port pins with a number where only the required bit is a '1'. To make the pin low you AND it with a number where only the required bit is low.
For example to make RC0 high you use PORTC | b'00000001'
For example to make RC1 high you use PORTC | b'00000010'
For example to make RC0 low you use PORTC & b'11111110'
For example to make RC1 low you use PORTC & b'11111101'
Note the positions of the '1' and '0' in the examples match the bit position in PORTC you want to change. You can do it by writing the whole byte to the port but using the method I show ensures all the other pins are unaffected.
Knowing which of the bits is high when you read a number back from the keypad lets you work out which of the keys conducted the signal back to the port inputs.
Brian.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 BYTE getKeypad(){ TRISC = 0b00000011; //RC0, RC1 = Col1, Col2 PORTC = 0b00001100; //RC2, RC3 = Row1, Row2 return PORTC&TRISC; //returns the low nibble of PORTC } When called in my GLCD_out function, and tested on my emulator, the value output is; Key 1 = 1 |1| |2| |3... //very crude representation of the section of keypad Key 2 = 2 |4| |5| |6... //that I am using. Key 4 = 1 Key 5 = 2 Obviously, you can see my problem here, I need a different bit value for each key, not the repeated nibble values for the keys on Row 2. So I tried another method variation to try and distinguish between the possible bit patterns. See; BYTE getkeypad(){ BYTE key; PORTC = 0b00001100; //rows TRISC = 0b00000011; //colomns //These are the four possible bit pattern combinations from a key being pressed if((PORTC&TRISC) == 0b00000101) return key = 1; if((PORTC&TRISC) == 0b00001010) return key = 2; if((PORTC&TRISC) == 0b00001001) return key = 5; if((PORTC&TRISC) == 0b00000110) return key = 6; }
Without a computer at the moment so I can't write and debug code but the 'flow' is like this:
1. initialize the ports so POTC bits 0 and 1 are inputs, bits 2 and 3 are outputs.
2. start a loop
3. set bit 2 = 1 and bit 3 = 0
4. read PORTC then logic AND the result with 0x03 (so only the input bits remain)
5. if the result is zero, no key is pressed, if it is 0x01 then '1' was pressed, otherwise it was '4' that was pressed. Display your result.
6. set bit 2 = 0 and bit 3 = 1
7. read PORTC then logic AND the result with 0x03 (so only the input bits remain)
8. if the result is zero, no key is pressed, if it is 0x01 then '2' was pressed, otherwise it was '5' that was pressed. Display your result.
9. go back to step 3.
I think I got the numbers right, if the rows or columns are swapped it will still work and you can edit the values to reflect the correct keys numbers.
Brian.
BYTE getkeypad(){
int q, w;
PORTC = 0b00000100; //Set RC2 High
for(q=0;q<2;q++){ //Cycle through row RC2
if((PORTC & 0x3)==0x1) return 1;
if((PORTC & 0x3)==0x2) return 2;
}
PORTC = 0b00001000; //Set RC3 High
for(w=0;w<2;w++){ //Cycle through row RC3
if((PORTC & 0x3)==0x1) return 4;
if((PORTC & 0x3)==0x2) return 5;
}
if((PORTC & 0x3)==0) return 0; //Returns 0 if nothing is pressed
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?