Re: Help with Keypad 4x4 using RCM5600W
Scott Henion <[email protected]>
| Newsgroups | gmane.comp.hardware.rabbit-semiconductor |
|---|---|
| Message-ID | <[email protected]> |
On 8/20/2012 1:10 PM, [email protected] wrote: > Hello, > > I created a program to read a 4x4 keypad, but the output always 0xFF which means > there is no button is pressed. Where is my fault location under this program? > > // all outputs add pull up to 3.3v with 4k7 resistor > // all inputs add series resistors 1k > // > // Keypad 4x4 with inputs (PB0-3) and outputs (PB4-7) > //////////////////////////////////////////// > // <-- INPUTS > // 1 2 3 Clear <-- PB0 > // 4 5 6 Up <-- PB1 > // 7 8 9 Down <-- PB2 > // Menu 0 Cancel Enter <-- PB3 > /////////////////////////////////////////// > // PB4 PB5 PB6 PB7 OUTPUTS > > #use "rcm56xxw.lib" > > unsigned char getKey(){ > unsigned char rowScan, readVal, writeVal, d; > for(rowScan=0; rowScan<4; rowScan++){ > writeVal = ~(unsigned char)pow2(rowScan); > WrPortI(PBDDR, &PBDDRShadow, writeVal); > readVal = RdPortI(PBDDR); > if(readVal != writeVal) break; // if button pressed, exit for > } > > switch(readVal){ > > // scan row 1 // OUTPUTS <-- INPUTS > case 0xEE: // 1110 1110 <-- 1111 1110 > return 1; > case 0xDE: // 1101 1110 <-- 1111 1110 > return 2; > case 0xBE: // 1011 1110 <-- 1111 1110 > return 3; > case 0x7E: // 0111 1110 <-- 1111 1110 clear button > return 0x0A; > > // scan row 2 > case 0xED: // 1110 1101 <-- 1111 1101 > return 4; > case 0xDD: // 1101 1101 <-- 1111 1101 > return 5; > case 0xBD: // 1011 1101 <-- 1111 1101 > return 6; > case 0x7D: // 0111 1101 <-- 1111 1101 up button > return 0x0B; > > // scan row 3 > case 0xEB: // 1110 1011 <-- 1111 1011 > return 7; > case 0xDB: // 1101 1011 <-- 1111 1011 > return 8; > case 0xBB: // 1011 1011 <-- 1111 1011 > return 9; > case 0x7B: // 0111 1011 <-- 1111 1011 down button > return 0x0C; > > // scan row 4 > case 0xE7: // 1110 0111 <-- 1111 0111 menu button > return 0x0E; > case 0xD7: // 1101 0111 <-- 1111 0111 > return 0; > case 0xB7: // 1011 0111 <-- 1111 0111 cancel button > return 0x0F; > case 0x77: // 0111 0111 <-- 1111 0111 enter button > return 0x0D; > > default: > return 0xFF; // no button is pressed > } > } > > void main(){ > brdInit(); > > WrPortI(PBDDR, &PBDDRShadow, 0xFF); > > for(;;){ > costate{ > printf("%02X ", getKey()); > } > } > } > You are writing to the port B direction register and trying to read it back. You never wrote anything to the data reg. The direction register PBDDR may be write only (it is on earlier CPUs.) One comment about this: writeVal = ~(unsigned char)pow2(rowScan); pow2() is a higher math function, takes considerable overhead in the function and then also converting float to integer. This is much simpler: writeVal = ~(unsigned char)(1<<rowScan); <Scott> -- ------------------------------------------ Scott G. Henion, Consultant Web site: http://SHDesigns.org ------------------------------------------