$regfile = "2313def.dat" $crystal = 4000000 'I2C lines Config Sda = Portd.5 Config Scl = Portd.4 'default I2C speed Config I2cdelay = 10 'LED on this pin Config Pind.6 = Output 'Int from PCF8574 to Int0 pin Config Pind.2 = Input Config Int0 = Falling Const Pcf8574write = &H40 Const Pcf8574read = &H41 'Mask for reading columns and rows state Const Columnsmask = &B11110000 Const Rowsmask = &B00001111 'Keyboard debounce time Const Debouncetime = 150 Const Keystringlength = 10 Const Wtime = 100 Dim Pcf8574input As Byte Dim Keycoderow As Byte Dim Keycodecol As Byte Dim Keycode As Byte Dim Keychar As String * 1 Dim Keystring As String * Keystringlength Dim Keycharpos As Byte Dim Endofstring As Bit Cls On Int0 Pcfint 'Set PCF8574A to input, masked pins high (pull-up) I2cstart I2cwbyte Pcf8574write I2cwbyte Columnsmask I2cstop Enable Interrupts Enable Int0 Do Endofstring = 0 Keystring = Space(keystringlength) Keycharpos = 0 Cls Lcd "? " While Endofstring = 0 Set Portd.6 Waitms Wtime Reset Portd.6 Waitms Wtime Wend Lowerline Lcd "S " ; Keystring Wait 1 Loop 'PCF8574 interrupt routine Pcfint: 'read the input pins I2cstart I2cwbyte Pcf8574read I2crbyte Keycodecol , Nack I2cstop 'shift upper nibble to lower nibble Shift Keycodecol , Right , 4 'switch column and row input/output state I2cstart I2cwbyte Pcf8574write I2cwbyte Rowsmask I2cstop 'read the input pins I2cstart I2cwbyte Pcf8574read I2crbyte Keycoderow , Nack I2cstop 'Cls 'Lcd Bin(keycodecol) ; Bin(keycodecol) 'switch column and row input/output state back I2cstart I2cwbyte Pcf8574write I2cwbyte Columnsmask I2cstop Select Case Keycoderow Case 7 : Keycode = 0 Case 11 : Keycode = 4 Case 13 : Keycode = 8 Case 14 : Keycode = 12 Case Else : Keycode = 99 End Select 'make final keycode from portb pins read Select Case Keycodecol Case 7 : Keycode = Keycode + 0 Case 11 : Keycode = Keycode + 1 Case 13 : Keycode = Keycode + 2 Case 14 : Keycode = Keycode + 3 Case Else : Keycode = Keycode + 99 End Select 'illegal keycode from bounce effects If Keycode > 15 Then Keycode = 16 'Cls 'Lcd Keycoderow ; " " ; Keycodecol ; " " ; Keycode Keychar = Lookupstr(keycode , Keycodes) If Keychar <> "?" Then Lcd Keychar If Keychar <> "E" Then Keycharpos = Keycharpos + 1 Mid(keystring , Keycharpos , 1) = Keychar Print "keystring: " ; Keystring If Keycharpos = 16 Then Endofstring = 1 Else Endofstring = 0 End If Else Endofstring = 1 End If End If Waitms Debouncetime Gifr = 64 Return End Keycodes: Data "1" , "2" , "3" , "A" , "4" , "5" , "6" , "B" , Data "7" , "8" , "9" , "C" , "R" , "0" , "E" , "D" , "?"