Use a 4x4 keypadA keypad is just a set of push-buttons connected in such a way to form 'rows' and 'columns', thus reducing a number of I/O pins necessary for their connection. A keypad with 16 push-buttons arranged in 4 rows and 4 columns is used in this example. The Keypad 4x4 library contains all the functions necessary for reading this keypad as well as for initializing the port it is connected to. In order to demonstrate the operation of the keypad 4x4, the message will be shown on an LCD display.
'Header****************************************************** program example_16 ' Program name dim kp, curX, curY as byte dim keypadPORT as byte at PORTD ' This variable must be defined in all projects using Keypad Lib. ' It define the port used for keypad connection dim LCD_RS as sbit at RB4_bit ' Lcd module connections LCD_EN as sbit at RB5_bit LCD_D4 as sbit at RB0_bit LCD_D5 as sbit at RB1_bit LCD_D6 as sbit at RB2_bit LCD_D7 as sbit at RB3_bit LCD_RS_Direction as sbit at TRISB4_bit LCD_EN_Direction as sbit at TRISB5_bit LCD_D4_Direction as sbit at TRISB0_bit LCD_D5_Direction as sbit at TRISB1_bit LCD_D6_Direction as sbit at TRISB2_bit LCD_D7_Direction as sbit at TRISB3_bit ' End Lcd module connections main: ' Start of program curX=1 ' For keeping a record of the 2x16 LCD cursor position curY=1 ANSEL = 0 ' Configure analog pins as digital I/O ANSELH = 0 TRISB = 0 PORTB = 0xFF Keypad_Init() ' Initialize keypad on PORTC Lcd_Init() ' Initialize LCD on PORTB, Lcd_Cmd(_LCD_CLEAR) ' Clear display while true ' Wait for some key to be pressed and released kp = 0 while kp = 0 kp = Keypad_Key_Click() Delay_ms(10) wend select case kp ' Prepare value for output case 1 kp = "1" case 2 kp = "2" case 3 kp = "3" case 4 kp = "A" case 5 kp = "4" case 6 kp = "5" case 7 kp = "6" case 8 kp = "B" case 9 kp = "7" case 10 kp = "8" case 11 kp = "9" case 12 kp = "C" case 13 kp = "*" case 14 kp = "0" case 15 kp = "#" case 16 kp = "D" end select if (curY > 16) then ' Change cursor position if (curX = 1) then Lcd_Cmd(_LCD_SECOND_ROW) curX = 2 curY = 1 else Lcd_Cmd(_LCD_FIRST_ROW) curX = 1 curY = 1 end if end if Lcd_Chr_CP(kp) ' Display on LCD Inc(curY) wend end. ' End of programIn order to make this example work properly, it is necessary to check the following libraries in the Library Manager prior to compiling: