برنامج هذا التطبيق يصبح كما يلى :
كود:
// Declaration 4x4 keyboard port
char keypadPort at PORTC;
// Definition pins for LCD.
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;
// Definition of TRIS registers for LCD.
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
// Function to decode the keyboard.
char ReadKey (void)
{
// Switch case structure to assess the values ??returned
// by press keyboard.
switch (Keypad_Key_Press ())
{
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return 'A';
case 5: return '4';
case 6: return '5';
case 7: return '6';
case 8: return 'B';
case 9: return '7';
case 10: return '8';
case 11: return '9';
case 12: return 'C';
case 13: return '*';
case 14: return '0';
case 15: return '#';
case 16: return 'D';
default: return 0; // button not pressed.
}
}
void main (void)
{
// Declaration of variables.
char key;
// Setup and initialization of PIC.
Lcd_init (); // Initialize the LCD.
Lcd_Cmd (_LCD_CURSOR_OFF); // Turns off the cursor.
Lcd_Out (1, 1, "key"); // Print text.
while (1) // infinite loop.
{
Key = ReadKey (); // It reads the keyboard and its result is stored in key.
Lcd_Chr (2,1, key); // Display the value returned by the keyboard.
}
}
بمجرد التحرير يتم الترجمة والمحاكاة حيث يعرض على الشاشة الحرف المقابل للمفتاح المضغوط بلوحة المفاتيح كما فى الشكل التالى :