' Using the PIC16C620A PIC For a serial keypad encoder ' The keypad sends each key number entered out PortA.3 ' serially, with PortA.4 acting as data-available pin. ' Dgood, [PortA.4] will remain high until key is released. ' Dgood will go low immediately after serial data is sent ' on Serpin [PortA.3] define osc 4 include "bs2defs.bas" cmcon = 7 'Port A all I/O [dissable comparators]. dgood var porta.2 'Data available pin. [high/low pulse on keypress] serpin var porta.3 'Serial output pin baudin var porta.4 'Baud rate selection pin. [can select on the fly] nokey con %11111111 'No keys pressed keyin var portb 'Key input port on port B keydata var byte trisb = %11111111 'Port B = all inputs trisa = %00010011 'A.0,A.1,A.4 inputs, rest are outputs key1 var portb.0 key2 var portb.1 key3 var portb.2 key4 var portb.3 key5 var portb.4 key6 var portb.5 key7 var portb.6 key8 var portb.7 key9 var porta.0 key10 var porta.1 '=========== Find the key pressed ========== findkey: if key1 = 0 then press1 if key2 = 0 then press2 if key3 = 0 then press3 if key4 = 0 then press4 if key5 = 0 then press5 if key6 = 0 then press6 if key7 = 0 then press7 if key8 = 0 then press8 if key9 = 0 then press9 if key10 = 0 then press10 goto findkey '=========== Sort em out here ============== press1: keydata = 1 goto dout press2: keydata = 2 goto dout press3: keydata = 3 goto dout press4: keydata = 4 goto dout press5: keydata = 5 goto dout press6: keydata = 6 goto dout press7: keydata = 7 goto dout press8: keydata = 8 goto dout press9: keydata = 9 goto dout press10: keydata = 10 goto dout dout: low dgood 'Pulse data good pin PortA.2. pause 100 '100mS delay [software debounce]. wait1: if keyin <> nokey then wait1 'Wait for keys to be released. wait2: if key9 = 0 then wait2 if key10 = 0 then wait2 baudselect: if baudin = 1 then fast 'If baudin=1 then N9600, else N2400 serout serpin,N2400,[keydata] 'Send key value serially out PortA.3 goto x2 fast: serout serpin,N9600,[keydata] x2: high dgood keydata = %00000000 'Clear data storage byte variable. goto findkey end