ROWB added successfully ! don’t unerstand all in asm but it works.
keypad_rs232.zip (29.6 KB)
no Carry Return or other line in code , just ASCII char to match with remote control
; 5x4 Keypad reading with RS232 output
LIST p=16F628 ;tell assembler what chip we are using
include "P16F628.inc" ;include the defaults for the chip
ERRORLEVEL 0, -302 ;suppress bank selection messages
__config 0x3D18 ;sets the configuration settings (oscillator type etc.)
cblock 0x20 ;start of general purpose registers
count ;used in looping routines
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
tmp1 ;temporary storage
tmp2
key
rows
index
Xmit_Byte ;holds byte to xmit
Rcv_Byte ;holds received byte
Bit_Cntr ;bit counter for RS232
Delay_Count ;delay loop counter
endc
SER_PORT Equ PORTA
SER_TRIS Equ TRISA
SER_OUT Equ 0x06
KEY_PORT Equ PORTB ;keypad port
KEY_TRIS Equ TRISB
Col1 Equ 0 ;pins used for keypad inputs
Col2 Equ 1
Col3 Equ 2
Col4 Equ 3
org 0x0000
goto Start
Key_Table ADDWF PCL , f
RETLW 0x44 ;row4 (D)
RETLW 0x45
RETLW 0x46
RETLW 0x30
RETLW 0x43 ;row3 (C)
RETLW 0x39
RETLW 0x38
RETLW 0x37
RETLW 0x42 ;row2 (B)
RETLW 0x36
RETLW 0x35
RETLW 0x34
RETLW 0x41 ;row1 (A)
RETLW 0x33
RETLW 0x32
RETLW 0x31
RETLW 0x57 ;ROWB (Z)
RETLW 0x58
RETLW 0x59
RETLW 0x5A
Start movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
Initialise clrf count
clrf PORTA
clrf PORTB
SetPorts bsf STATUS, RP0 ;select bank 1
movlw B'00000000';0x00 ;make all pins outputs
movwf SER_TRIS
movlw 0x0F ;set keypad pins
movwf KEY_TRIS ;half in, half out
bcf STATUS, RP0 ;select bank 0
call SER_INIT ;setup serial port
call Delay255 ;a;;ow time to settle
clrf count ;set counter register to zero
Main movlw 0x0F
movwf KEY_PORT ;set all output pins low
Loop call Chk_Keys ;check keys
movf key, w ;restore key pressed
call XMIT_RS232 ;display as ASCII
;movlw 0x0D ;CR
;call XMIT_RS232
goto Loop
;Keypad subroutine
Chk_Keys
movlw 0x00 ;wait until no key pressed
movwf KEY_PORT ;set all output pins low
movf KEY_PORT, W
andlw 0x0F ;mask off high byte
sublw 0x0F
btfsc STATUS, Z ;test if any key pressed
goto Keys ;if none, read keys
call Delay20
goto Chk_Keys ;else try again
Keys call Scan_Keys
movlw 0x14;0x10; ;check for no key pressed
subwf key, w
btfss STATUS, Z
goto Key_Found
call Delay20
goto Keys
Key_Found movf key, w
andlw B'00011111';0x0f;
call Key_Table ;lookup key in table
movwf key ;save back in key
return ;key pressed now in W
Scan_Keys clrf key
BSF SER_PORT,2
movlw 0xF0 ;set all output lines high
movwf KEY_PORT
bcf STATUS, C ;put a 0 into carry
Scan rrf KEY_PORT, f
bsf STATUS, C
btfss KEY_PORT, Col4
goto Press
incf key, f
btfss KEY_PORT, Col3
goto Press
incf key, f
btfss KEY_PORT, Col2
goto Press
incf key, f
btfss KEY_PORT, Col1
goto Press
incf key, f
rrf KEY_PORT, f
bsf STATUS, C
btfss KEY_PORT, Col4
goto Press
incf key, f
btfss KEY_PORT, Col3
goto Press
incf key, f
btfss KEY_PORT, Col2
goto Press
incf key, f
btfss KEY_PORT, Col1
goto Press
incf key, f
rrf KEY_PORT, f
bsf STATUS, C
btfss KEY_PORT, Col4
goto Press
incf key, f
btfss KEY_PORT, Col3
goto Press
incf key, f
btfss KEY_PORT, Col2
goto Press
incf key, f
btfss KEY_PORT, Col1
goto Press
incf key, f
rrf KEY_PORT, f
bsf STATUS, C
btfss KEY_PORT, Col4
goto Press
incf key, f
btfss KEY_PORT, Col3
goto Press
incf key, f
btfss KEY_PORT, Col2
goto Press
incf key, f
btfss KEY_PORT, Col1
goto Press
incf key, f
BCF SER_PORT,2
rrf KEY_PORT, f
bsf STATUS, C
btfss KEY_PORT, Col4
goto Press
incf key, f
btfss KEY_PORT, Col3
goto Press
incf key, f
btfss KEY_PORT, Col2
goto Press
incf key, f
btfss KEY_PORT, Col1
goto Press
incf key, f
Press return
;end of keypad subroutines.
;Serial routines
SER_INIT
BSF STATUS, RP0 ;select bank 1
BCF SER_TRIS, SER_OUT ;set A6 as an output
;BSF SER_TRIS, SER_IN ;set A7 as an input
BCF STATUS, RP0 ;select bank 0
BSF SER_PORT, SER_OUT ;set SER_OUT high
RETURN
XMIT_RS232 MOVWF Xmit_Byte ;move W to Xmit_Byte
MOVLW 0x08 ;set 8 bits out
MOVWF Bit_Cntr
BCF SER_PORT, SER_OUT
CALL Bit_Delay
Ser_Loop RRF Xmit_Byte , f ;send one bit
BTFSS STATUS , C
BCF SER_PORT, SER_OUT
BTFSC STATUS , C
BSF SER_PORT, SER_OUT
CALL Bit_Delay
DECFSZ Bit_Cntr , f ;test if all done
GOTO Ser_Loop
BSF SER_PORT, SER_OUT
CALL Bit_Delay
RETURN
Start_Delay MOVLW 0x0C
MOVWF Delay_Count
Start_Wait NOP
DECFSZ Delay_Count , f
GOTO Start_Wait
RETURN
Bit_Delay MOVLW 0x18
MOVWF Delay_Count
Bit_Wait NOP
DECFSZ Delay_Count , f
GOTO Bit_Wait
RETURN
;End of serial routines
Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw d'100' ;delay 100mS
goto d0
Delay50 movlw d'50' ;delay 50mS
goto d0
Delay20 movlw d'20' ;delay 20mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock)
d0 movwf count1
d1 movlw 0xC7 ;delay 1mS
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
end