PIC Development Board - Hex Keypad
sbit X_1 at RB0_bit;
sbit X_2 at RB1_bit;
sbit X_3 at RB2_bit;
sbit X_4 at RB3_bit;
sbit Y_1 at RB4_bit;
sbit Y_2 at RB5_bit;
sbit Y_3 at RB6_bit;
sbit Y_4 at RB7_bit;
#define Keypad_PORT PORTB
#define Keypad_PORT_Direction TRISB
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
char keypad_scanner(void)
{
X_1 = 0; X_2 = 1; X_3 = 1; X_4 = 1;
if (Y_1 == 0) { delay_ms(100); while (Y_1==0); return '1'; }
if (Y_2 == 0) { delay_ms(100); while (Y_2==0); return '2'; }
if (Y_3 == 0) { delay_ms(100); while (Y_3==0); return '3'; }
if (Y_4 == 0) { delay_ms(100); while (Y_4==0); return '4'; }
X_1 = 1; X_2 = 0; X_3 = 1; X_4 = 1;
if (Y_1 == 0) { delay_ms(100); while (Y_1==0); return '4'; }
if (Y_2 == 0) { delay_ms(100); while (Y_2==0); return '5'; }
if (Y_3 == 0) { delay_ms(100); while (Y_3==0); return '6'; }
if (Y_4 == 0) { delay_ms(100); while (Y_4==0); return '8'; }
X_1 = 1; X_2 = 1; X_3 = 0; X_4 = 1;
if (Y_1 == 0) { delay_ms(100); while (Y_1==0); return '7'; }
if (Y_2 == 0) { delay_ms(100); while (Y_2==0); return '8'; }
if (Y_3 == 0) { delay_ms(100); while (Y_3==0); return 'A'; }
if (Y_4 == 0) { delay_ms(100); while (Y_4==0); return 'B'; }
X_1 = 1; X_2 = 1; X_3 = 1; X_4 = 0;
if (Y_1 == 0) { delay_ms(100); while (Y_1==0); return 'C'; }
if (Y_2 == 0) { delay_ms(100); while (Y_2==0); return 'D'; }
if (Y_3 == 0) { delay_ms(100); while (Y_3==0); return 'E'; }
if (Y_4 == 0) { delay_ms(100); while (Y_4==0); return 'F'; }
return 'n';
}
char switch_press_scan(void) // Get key from user
{
char key = 'n'; // Assume no key pressed
while(key=='n') // Wait untill a key is pressed
key = keypad_scanner(); // Scan the keys again and again
return key; //when key pressed then return its value
}
void InitKeypad(void)
{
Keypad_PORT = 0x00; // Set Keypad port pin values zero
Keypad_PORT_Direction = 0xF0; // Last 4 pins input, First 4 pins output
OPTION_REG &= 0x7F;
}
char Key;
void main()
{
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
InitKeypad();
while(1)
{
Key = switch_press_scan();
Lcd_Chr_Cp(Key);
}
}