The example shows the connection of a 4 x 4 keyboard to a dsPIC30F6014A microcontroller. The example shows decoding the keyboard [0...15] to obtain ASCII symbols [0...9,A...F]. Also shown is a counter of the pressed keys. The value of the counter is written in the second line of an LCD module. The realization is carried out by using the mikroBasic compiler for dsPIC30F microcontrollers. Fig. 13-5 shows the interconnection of the keyboard and a dsPIC30F6014A microcontroller.
Fig. 13-5 Interconnection of the keyboard and a dsPIC30F6014A microcontroller
program Keypad_4x4_test dim kp, oldkp as byte cnt as word txt as string[6] main: cnt = 0 Keypad_Init(PORTF) ADPCFG = $FFFF Glcd_Init_dsPICPro3() Glcd_Fill(0x00) Glcd_Set_Font(@FontSystem5x8, 5, 8, 32) Glcd_Write_Text("Keypad 4x4 example", 10, 0, 1) Glcd_Write_Text("Key pressed :", 10, 3, 1) Glcd_Write_Text("Times :" , 10, 6, 1) oldkp = 1 while TRUE nop kp = 0 ' Wait for key to be pressed while kp = 0 kp = Keypad_Key_Click() wend if oldkp = kp then Inc(cnt) else cnt = 1 end if oldkp = kp ' Prepare value for output select case kp case 1 kp = 49 ' 1 case 2 kp = 50 ' 2 case 3 kp = 51 ' 3 case 4 kp = 65 ' A case 5 kp = 52 ' 4 case 6 kp = 53 ' 5 case 7 kp = 54 ' 6 case 8 kp = 66 ' B case 9 kp = 55 ' 7 case 10 kp = 56 ' 8 case 11 kp = 57 ' 9 case 12 kp = 67 ' C case 13 kp = 42 ' * case 14 kp = 48 ' 0 case 15 kp = 35 ' # case 16 kp = 68 ' D end select Glcd_Write_Char(kp, 30, 4, 1) if cnt = 65535 then cnt = 0 end if ' Print on LCD WordToStr(cnt, txt) Glcd_Write_Text(txt, 10, 7, 1) wend 'while end.
program Button_Test main: ADPCFG = $FFFF TRISF = $0000 LATF = $AAAA Delay_ms(200) LATF = $0000 while true ' if button on portb.0 is 5v for 100ms if Button(PORTB, 0, 100, 1) = 1 then LATF = $0001 else if Button(PORTB, 1, 100, 1) = 1 then LATF = $0002 else if Button(PORTB, 2, 100, 1) = 1 then LATF = $0004 else if Button(PORTB, 3, 100, 1) = 1 then LATF = $0008 else LATF = 0 end if end if end if end if wend end.