Getting an input from a user may seem trivial, but in real word it will lead to resolve some problems like switch bounce, input correction, input validation, polling function in each major loop of the program, and so on... Here is a very convenient way to solve them : this interrupt-based keypad library will do it for you. |
Page index :
Why another PIC Keypad LIBRARY ?
There are already tons of keypad PIC libraries on the web, but I think this one will be the most universal and versatile, featuring :
Learn more below :
Library Functions
PROTOTYPE | void kp_init() |
PARAMETERS |
none |
RETURNS | nothing |
DESCRIPTION |
prepare the PIC keypad and button library. |
REQUIRES |
the file keypad_cfg.h must be set accordingly to user's hardware (see below) |
EXAMPLE | kp_init() ; |
PROTOTYPE | unsigned char kp_full() |
PARAMETERS |
none |
RETURNS |
return > 0 if the buffer is full, 0 otherwise |
DESCRIPTION |
check for buffer full |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
if(kp_full()) return ; // return if buffer full |
PROTOTYPE | unsigned char kp_hit() |
PARAMETERS |
none |
RETURNS |
return > 0 if a key is being pressed, 0 otherwise (pseudo function) |
DESCRIPTION |
check for a key hit |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
while(kp_hit()) ; // wait for key to be released |
none return > 0 if enter key has been pressed, 0 otherwise (pseudo function) check for enter key kp_init() must have been called and timer interrupt must be enabled if(kp_enter()) { ... } // do input treatment on enter key
PROTOTYPE
unsigned char kp_enter()
PARAMETERS
RETURNS
DESCRIPTION
REQUIRES
EXAMPLE
PROTOTYPE | unsigned char kp_erase() |
PARAMETERS |
none |
RETURNS |
return > 0 if last key was erase , 0 otherwise (pseudo function) |
DESCRIPTION |
check for erase key |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
if(kp_erase()) { ... } // do input treatment on erase key |
PROTOTYPE | unsigned char kp_last() |
PARAMETERS |
none |
RETURNS |
return last key pressed |
DESCRIPTION |
get last key |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
switch(kp_last()) { ... } // process keys |
PROTOTYPE | void kp_setCircular() |
PARAMETERS |
none |
RETURNS | nothing |
DESCRIPTION |
switch on circular mode on input buffer : |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
kp_setCircular() ; |
PROTOTYPE | void kp_setLinear() |
PARAMETERS |
none |
RETURNS | nothing |
DESCRIPTION |
switch on linear mode on input buffer : |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
kp_setLinear() ; |
PROTOTYPE | unsigned char kp_circular() |
PARAMETERS |
none |
RETURNS |
return > 0 if input buffer is in circular mode, 0 otherwise. |
DESCRIPTION |
check for buffer circular mode, default is linear mode. |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
if(kp_circular()) kp_setLinear() ; |
PROTOTYPE | void kp_setTypematic() |
PARAMETERS |
none |
RETURNS | nothing |
DESCRIPTION |
switch on typematic (auto-repeat) mode |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
kp_setTypematic() ; |
PROTOTYPE | void kp_unsetTypematic() |
PARAMETERS |
none |
RETURNS | nothing |
DESCRIPTION |
switch off typematic (auto-repeat) mode |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
kp_unsetTypematic() ; |
PROTOTYPE | unsigned char kp_typematic() |
PARAMETERS |
none |
RETURNS |
return > 0 if typematic (auto-repeat) in enable, 0 otherwise. |
DESCRIPTION |
return typematic state, default is not enabled. |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
if(kp_typematic()) kp_unsetTypematic() ; |
PROTOTYPE | void kp_flush() |
PARAMETERS |
none |
RETURNS | nothing |
DESCRIPTION |
flush input, buffer is cleared |
REQUIRES |
kp_init() must have been called and timer interrupt must be enabled |
EXAMPLE |
kp_flush() ; |
PROTOTYPE | void kp_isr() |
PARAMETERS |
none |
RETURNS | nothing |
DESCRIPTION |
this function must not be called directly by user, but must be placed in the timer interrupt() function. the input is stored in buffer kp_buf, defined as a pointer to char. |
REQUIRES |
kp_init() must have been called |
EXAMPLE |
void interrupt(void) INTCON.T0IF = 0 ; // done |
Configuration
Keypad configuration is defined in file keypad_cfg.h, you can change it depending on your hardware and preferences :
SYMBOL | USAGE | EXAMPLE |
KEYPAD_MODE | define this symbol if you are using a keypad (rows and colums) do not define this symbol if you are using buttons (colums only) |
#define KEYPAD_MODE |
KP_ROW_PORT |
PORT where rows are connected to |
#define KP_ROW_PORT PORTB |
KP_ROW_TRIS | TRIS direction register corresponding to KP_ROW_PORT (only if KEYPAD_MODE is defined) |
#define KP_ROW_TRIS TRISB |
KP_ROW_BITNUM |
bit number of the first row in the PORT, other bits must be next and contiguous |
#define KP_ROW_BITNUM 0 |
KP_ROW_NUMBER | number of rows (only if KEYPAD_MODE is defined) |
#define KP_ROW_NUMBER 3 |
KP_COL_PORT | PORT where columns are connected to. Each column input must be pulled down with a 10 K resistor. |
#define KP_COL_PORT PORTB |
KP_COL_TRIS | TRIS direction register corresponding to KP_COL_PORT | #define KP_COL_TRIS TRISB |
KP_COL_BITNUM | bit number of the first row in the PORT, other bits must be next and contiguous | #define KP_COL_BITNUM 3 |
KP_COL_NUMBER | number of columns | #define KP_COL_NUMBER 4 |
KP_SCAN_CODES |
scan codes lookup table the top left key is the first one, the bottom right key is the last one. |
#define KP_SCAN_CODES "*7410852#963" |
KP_ERASE |
erase key, must be one in KP_SCAN_CODES string |
#define KP_ERASE '*' |
KP_ENTER | enter key, must be one in KP_SCAN_CODES string don't define it if you don't need an enter key |
#define KP_ENTER '#' |
KP_MAX_LENGTH | maximum input length | #define KP_MAX_LENGTH 16 |
KP_TMR_REPEAT |
typematic rate, must be set depending on timer overflow period and user's preferences. when using a PIC16F877A @ 8 Mhz with 8 bit TMR0 and no prescaler, if you want an auto repeat rate of 300 ms, use : |
#define KP_TMR_REPEAT 2343 |
KP_TMR_DEBOUNCE |
debounce time, must be set depending on timer overflow period and user's preferences. when using a PIC16F877A @ 8 Mhz with 8 bit TMR0 and no prescaler, if you want a debounce time of 30 ms, use : |
#define KP_TMR_DEBOUNCE 234 |
KP_MAX_LENGTH | maximum input length | #define KP_MAX_LENGTH 16 |
CIRCUIT EXAMPLE
MIKROC SOURCE CODE EXAMPLE
Here is the mikroC source code of the program example. You have to use the zipped mikroC project to build it.
|
PROJECT DOWNLOAD
You can use this software as you wish, if you accept to do it at your own risks.
Download PIC Keypad Library C source code with demo example for mikroC : zipped file, 6 Ko
content of the archive :
You can get mikroC from here : http://www.mikroe.com/en/compilers/mikroc/pic/
Please report any bug, comment or suggestion in my forums. Thanks !