Keypad Interfacing with PIC18 microcontroller
KEYPAD INTERFACING
Keypad and LCDs are the most widely used input/output devices and a basic understanding of them is essential. In this Post, we first discuss keypad fundamentals, along with key press detection and key identification mechanisms.
Then we show how a keypad is interfaced to a PIC18.
Interfacing the keypad to the PIC18
At lowest level, keypads are arranged in a matrix of rows and columns, PIC controller uses two 8 bit ports to entertain 8 x 8 key matrix to connect it.
Connection establish between row and column whenever a key is pressed, in any other case connection considered to be open between row and column.
Generally, In personal computers keyboards, there is also a microcontroller that can handle hardware and software interfacing of the keyboard.
In a simple way ,Read only memory of microcontroller utilized to store the code to scan the keys constantly and detect which rows and column in contact, pass it to motherboard for further action.
Hence, to write code for keypad interfacing we must need two processes: (a) key press detection, and (b) key identification.
Normally, scanning process is used in key press detection. To achieve the desired results, microcontroller reads the column after applying ground to all rows. If the logic levels read from the columns are all high means no key has been pressed and the process repeats until a key press is detected.
If one of the column bits has a logic level zero, however, this indicates that a key press exist. Next, microcontroller needs the code to identify the pressed key, this time microcontroller grounds only first row and reads the column and check the status of data , if all high means no key in that row is pressed, process goes on for remaining rows. When the row is detected in which key has been presses, code will find the relevant column.
Don't worry about the above complex coding ,miKroC library for keypad can easily solve our problem.
Keypad Library in miKroC
Keypad_Init
Prototype:
void Keypad_Init(void); | |
Returns:
Nothing. | |
Description:
Initializes port for working with keypad. | |
Requires: Global variable :
| |
Example:
// Keypad module connections char keypadPort at PORTD; // End of keypad module connections ... Keypad_Init(); |
Keypad_Key_Press
Prototype:
char Keypad_Key_Press(void); | |
Returns:
The code of a pressed key (1..16).
If no key is pressed, returns 0.
| |
Description:
Reads the key from keypad when key gets pressed. | |
char kp; ... kp = Keypad_Key_Press(); |
Keypad_Key_Click
Prototype:
char Keypad_Key_Click(void); | |
Returns:
The code of a clicked key (1..16).
If no key is clicked, returns 0.
| |
Description:
Call to Keypad_Key_Click is a blocking call: the function waits until some key is pressed and released. When released, the function returns 1 to 16, depending on the key. If more than one key is pressed simultaneously the function will wait until all pressed keys are released. After that the function will return the code of the first pressed key. | |
char kp; ... kp = Keypad_Key_Click(); |
Proteus Simulation for Keypad Simulation
miKroC Code for Keypad Interfacing With PIC18f4580
unsigned short kp;
// Keypad module connections
char keypadPort at PORTD;
// End Keypad module connections
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
void main(){
ADCON1 = 0xff;
CMCON = 0x7;
Keypad_Init();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, "Key
Pressed:");
do {
kp = 0;
do
kp = Keypad_Key_Click();
while (!kp);
switch (kp) {
case 1: kp = 49; break; // 1
case 2: kp = 50; break; // 2
case 3: kp = 51; break; // 3
case 4: kp = 65; break; // A
case 5: kp = 52; break; // 4
case 6: kp = 53; break; // 5
case 7: kp = 54; break; // 6
case 8: kp = 66; break; // B
case 9: kp = 55; break; // 7
case 10: kp = 56; break; //
8
case 11: kp = 57; break; //
9
case 12: kp = 67; break; //
C
case 13: kp = 42; break; // *
case 14: kp = 48; break; //
0
case 15: kp = 35; break; //
#
case 16: kp = 68; break; //
D
}
Lcd_Chr(2, 10, kp);
} while (1);
}
CSS Code for keypad Interfacing
#include "flex_lcd.c"
#include "KBD.c"
char k;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config
tab
// TODO: USER CODE!!
lcd_init();
kbd_init();
lcd_putc("\fReady...\n");
while (TRUE) {
k=kbd_getc();
if(k!=0)
if(k=='*')
lcd_putc('\f');
else
lcd_putc(k);
}
}
Friends for further learning about PIC18 checkout
Comments
Post a Comment