Keypad skeleton program
; uncomment following two lines if using 16f627 or 16f628. config uses internal oscillator
LIST p=16F628a ;tell assembler what chip we are using
include "P16F628a.inc" ;include the defaults for the chip
__config 0x3D18 ;sets the configuration settings (oscillator type etc.)

; Filename : keypad.asm
; This is a skelatl program to get you started on reading the keypad and transmitting
; NOTE: there are many details omitted. That's for you to fill in


; Make life easy. Send bits in 1 msec intervals!!


; DECLARE VARIABLES!!!!!!
; We are telling the assembler we want to start allocating symbolic variables starting
; at machine location 0x20. Please refer to technical documents to see if this is OK!!

cblock 0x20 ;start of general purpose registers
count1 ; count1 is symbolic name for location 0x20
counta ; counta is symbolic name for location 0x21
countb ; countb is symbolic name for location 0x22
NUMBER
endc



;un-comment the following two lines if using 16f627 or 16f628
; FORGET THESE 2 LINES AND FUNNY STUFF HAPPENS

movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)

; The following is very typical. We must change memory banks to set the TRIS registers

bsf STATUS,RP0
movlw 0x0f
movwf TRISB ; portb is input. Remember pushdown (to ground) resisitors on input lines
; this was erronoeously report as pull-ups. Corrected 2/13/2019
; membrane keypad
; R1 R2 R3 R4 Col1 Col2 Col3 Col4
; B0 B1 B2 B3 B4 B5 B6 B7
; 1 1 1 1 0 0 0 0
movlw 0x00
movwf TRISA ;porta is output. Connect to resistor and IR LED
bcf STATUS,RP0 ;return to bank 0

;start main code here
main_loop

read_kbd

movlw 0x00

; inelegant code, but it does work!
; code 1- turn left
; 2- go straight
; 3- go right
; 5- go in reverse
; anything else, transmit a zero and stop motors

bsf PORTB, 4 ; lets scan the first column of keys
btfsc PORTB, 0 ; has the 1 key been pressed? if yes then send 1
movlw 0x01 ;
btfsc PORTB, 1 ; has the 4 key been pressed? if yes then
movlw 0x00 ;
btfsc PORTB, 2 ; has the 7 key been pressed? if yes then
movlw 0x00 ; copy decimal number 07 into w. but if not then continue on.
bcf PORTB, 4 ; now we have finished scanning the first column of keys

bsf PORTB, 5 ; lets scan the middle column of keys
btfsc PORTB, 0 ; has the 2 key been pressed? if yes then
movlw 0x02 ; copy decimal number 02 into w. but if not then continue on.
btfsc PORTB, 1 ; has the 5 key been pressed? if yes then
movlw 0x05 ; copy decimal number 05 into w. but if not then continue on.
btfsc PORTB, 2 ; has the 8 key been pressed? if yes then
movlw 0x00 ;copy decimal number 00 into w. but if not then continue on.
bcf PORTB, 5 ; now we have finished scanning the middle column of keys

bsf PORTB, 6 ; lets scan the last column of keys
btfsc PORTB, 0 ; has the 3 key been pressed? if yes then
movlw 0x03 ; copy decimal number 03 into w. but if not then continue on.
btfsc PORTB, 1 ; has the 6 key been pressed? if yes then
movlw 0x00 ; copy decimal number 06 into w. but if not then continue on.
btfsc PORTB, 2 ; has the 9 key been pressed? if yes then
movlw 0x00 ; copy decimal number 09 into w. but if not then continue on.
bcf PORTB, 6 ; no


; lazy programmer! didn't even check other keys!!!


movwf NUMBER
movf NUMBER,1
btfsc STATUS,Z ; was a number other than 0 entered ? If yes, skip next statement

goto read_kbd

; Time to transmit. There is a start bit plus three more bits.
; You might want to read up on the rlf and rrf commands, since you need to
; shift bits to get to the three low order bits



send all 4 bits in here


call delay_ a_milli_or_2_or_3 ; let it resettle!!!!! Don't send stuff so fast!!
goto read_kbd





end