Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////
- //// keypad.c ////
- //// Generic keypad scan driver ////
- //// ////
- //// kbd_init() Must be called before any other function. ////
- //// ////
- //// c = kbd_getc(c) Will return a key value if pressed or /0 if not ////
- //// This function should be called frequently so as ////
- //// not to miss a key press. ////
- //// ////
- ///////////////////////////////////////////////////////////////////////////
- #define __delay_cycles _delay
- //Now using RB change interrupt
- #ifndef BYTE
- #define BYTE unsigned char
- #endif
- #define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where
- // n is the number of times you expect
- // to call kbd_getc each second
- // Keypad layout:
- char const KEYS[4][4] = {{'7','8','9','/'},
- {'4','5','6','x'},
- {'1','2','3','-'},
- {'C','0','=','+'}};
- BYTE kchar;
- BYTE col;
- BYTE row;
- BYTE kbd_down=0;
- BYTE last_key=0;
- //--Interrupt Service function--
- interrupt ISR (void) // Interrupt function definition
- {
- if (RBIF) // If the RB change flag is 1, do .....
- {
- RBIE=0; //Disable RB change interrupt
- {
- kbd_down=1;
- col = (PORTB >> 4) & 0x0F;
- if (col == 0b1110)
- col = 0;
- else if (col == 0b1101)
- col = 1;
- else if (col == 0b1011)
- col = 2;
- else
- col = 3;
- last_key = KEYS[row][col];
- kchar = last_key;
- }
- RBIF=0; // Reset the external interrupt flag
- RBIE=1; // RE-Enable RB change interrupt
- }
- GIE=1; //Enable Global Interrupt
- }
- void kbd_init( )
- {
- TRISB = 0xF0; //Port B in/output
- nRBPU = 0; // Enable PORTB pullups
- RBIE=1; //Enable the RB port change interrupt
- GIE=1; // Global interrupt enable
- RBIF=0; //Clear RB port change interrupt flag
- }
- char kbd_getc( )
- {
- static BYTE kbd_call_count;
- kchar=last_key;
- for (row=0;row<4; row++)
- {
- switch (row)
- {
- case 0 : PORTB = PORTB | 0b11111110;
- RB0 = 0;
- break;
- case 1 : PORTB = PORTB | 0b11111101;
- RB1 = 0;
- break;
- case 2 : PORTB = PORTB | 0b11111011;
- RB2 = 0;
- break;
- case 3 : PORTB = PORTB | 0b11110111;
- RB3 = 0;
- }
- __delay_cycles(KBD_DEBOUNCE_FACTOR );
- if (kbd_down==1)
- {
- kbd_down=0;
- break;
- }
- else kchar = 0;
- }
- PORTB = PORTB | 0xFF; //Reset Port B
- return(kchar);
- } //End of keypad
Advertisement
Add Comment
Please, Sign In to add comment