/*------------------------------------------------
Keyboard 4*4 read
P1.7 ->-R3----------------F\|-E\|-D\|-C\|
P1.6 ->-R2----------------B\|-A\|-9\|-8\|
P1.5 ->-R1----------------7\|-6\|-5\|-4\|
P1.4 ->-R0----------------3\|-2\|-1\|-\0|
P1.3 -<-C3------------------| | | |
P1.2 -<-C2----------------------| | |
P1.1 -<-C1--------------------------| |
P1.0 -<-C0------------------------------|
keypressed = (1) 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
0-Low 15-High
Interrupt routine for 0 Low level needs.
------------------------------------------------*/
sfr P1 = 0x90; /* SFR definition for Port 1 */
sfr P3 = 0xB0; /* SFR definition for Port 3 */
#define MASK 0x0F /* low nibble mask 00001111 Setup P1 for Input P1.0-P1.3 Output P1.4-P1.7 */
/*------------------------------------------------
MAIN C Function
------------------------------------------------*/
void main (void)
{
unsigned int keypressed; /* variable for key values */
P1 = MASK; /* Setup P1 for Input P1.0-P1.3 Output P1.4-P1.7 */
/*--------------------------------------
Use the Toolbox buttons in the debugger
to change the value of P1.0-P1.3.
--------------------------------------*/
while (1)
{
char row,runbit,pval; /* temp variable for port values */
for (row=0, runbit=0x10; row<4; row++)
{
P1 = ~runbit|MASK; /*Output P1.4-P1.7 running 0 */
pval = ~P1&MASK; /* Read Input P1.0-P1.3 into pval -- 1 is pressed key*/
keypressed = pval|keypressed<<4; /* Pack pressed key in to keypressed */
runbit <<= 1;
}
}
}