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 mikroPascal 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; var kp, oldkp : byte; cnt : word; txt : string[6]; begin 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 do begin nop; kp := 0; // Wait for key to be pressed while kp = 0 do kp := Keypad_Key_Click(); if oldkp = kp then Inc(cnt) else cnt := 1; oldkp := kp; // Prepare value for output case kp of 1: kp := 49; // 1 2: kp := 50; // 2 3: kp := 51; // 3 4: kp := 65; // A 5: kp := 52; // 4 6: kp := 53; // 5 7: kp := 54; // 6 8: kp := 66; // B 9: kp := 55; // 7 10: kp := 56; // 8 11: kp := 57; // 9 12: kp := 67; // C 13: kp := 42; // * 14: kp := 48; // 0 15: kp := 35; // # 16: kp := 68; // D end; Glcd_Write_Char(kp, 30, 4, 1); if cnt = 65535 then cnt := 0; // Print on LCD WordToStr(cnt, txt); Glcd_Write_Text(txt, 10, 7, 1); end; //while end.
program Button_test; begin ADPCFG := $FFFF; TRISF := $0000; LATF := $AAAA; Delay_ms(200); LATF := $0000; while TRUE do begin // if button on PORTB.0 is logic high 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; end.