;******************************* ;4x4 Keypad.asm ;KEYPAD DEMONSTRATION PROGRAM ;Tests 4x4 keypad and flashes LED to indicate key number ;Keypad does not control any devices ; 13-6-2013 ;******************************* list p=12F629 radix dec include "p12f629.inc" errorlevel -302 ; Dont complain about BANK 1 Registers during assembly __CONFIG _MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT ;Internal osc. ;_MCLRE_OFF - master clear must be off for gp3 to work as input pin ;**************************************************************** ; variables - names and files ;**************************************************************** Cblock 20h temp1 ;used in delays temp2 ;used in delays temp3 ;used in delays temp16 ;16 keys flashes ;LED flashes to indicate key number loops SwFlags endc ;**************************************************************** ;Equates ;**************************************************************** status equ 0x03 rp1 equ 0x06 rp0 equ 0x05 GPIO equ 0x05 status equ 03h option_reg equ 81h ; bits on GPIO pin7 equ 0 ;GP0 LED - flashes to indicate key number pin6 equ 1 ;GP1 pin5 equ 2 ;GP2 pin4 equ 3 ;GP3 pin3 equ 4 ;GP4 pin2 equ 5 ;GP5 4x4 Keypad switches ;bits rp0 equ 5 ;bit 5 of the status register ;**************************************************************** ;Beginning of program ;**************************************************************** org 0x00 nop nop nop nop nop SetUp bsf status, rp0 ;Bank 1 movlw b'11001000' ;Set TRIS GP0,1,2,4,5 out GP3 not used movwf TRISIO bcf status, rp0 ;bank 0 movlw 07h ;turn off Comparator ports movwf CMCON ;must be placed in bank 0 clrf GPIO ;Clear GPIO of junk movlw 4Fh ;start of GPR's movwf fsr call NoKey ;see if a switch is pressed btfss SwFlags,1 ;test bit1 for switch pressed goto SetUp2 ;load 50h to 5Fh from EEPROM call GenKeyvalue ;to see if key16 is pressed before start-up. movlw 0Ch ;Put a value into W and perform SUBWF subwf loops,1 ;Carry flag is CLEAR if W is greater than file value btfsc 03,c ;Test carry flag goto SetUp2 movlw .16 movwf temp16 ;for 16 keys call NoKey ;test bit1 for switch pressed btfsc SwFlags,1 ;detect no key pressed for debounce goto $-2 bsf gpio,0 ;flash LED briefly call _10mS bcf gpio,0 call _10mS call NoKey ;test bit1 for switch pressed btfss SwFlags,1 ;detect key pressed for debounce goto $-2 call GenKeyvalue ;get number of loops incf fsr,f ;fsr starts at file 50h Store 16 key-values movf loops,w ;move number of loops into w movwf indf ;put loops into file 50h to 5Fh decfsz temp16,f goto $-.15 call toEEPROM ;put 16 values into EEPROM bsf gpio,0 ;added call _500mS bcf gpio,0 call _500mS goto $-4 ;keeps flashing - turn off project ;load files 50h to 5fh with key values from EEPROM SetUp2 bsf status,rp0 ;select bank1 clrf eeadr ;start EEPROM at location 0 bcf status,rp0 ;select bank0 movlw .16 movwf temp16 ;for 16 keys movlw 4Fh ;start of GPR's movwf fsr incf fsr,f ;fsr starts at file 50h bsf status,rp0 bsf EECON1,0 ;starts EEPROM read operation storing result in EEDATA movf EEDATA,w ;move read data into w bcf status,rp0 movwf indf ;put EEPROM data into file 50h to 5Fh bsf status,rp0 ;select bank1 incf eeadr,1 bcf status,rp0 ;select bank0 decfsz temp16,f goto $-.10 goto Main ;******************** ;* Delays * ;******************** _uS goto $+1 _uS_1 goto $+1 goto $+1 retlw 00 _1mS movlw 80h movwf temp1 nop decfsz temp1,f goto $-2 retlw 00 _10mS movlw 0Ah movwf temp2 _10 nop decfsz temp1,f goto _10 decfsz temp2,f goto _10 retlw 00 _100mS movlw .100 movwf temp2 _100 nop decfsz temp1,f goto _100 decfsz temp2,f goto _100 retlw 00 _500mS movlw 5 movwf temp3 call _100mS decfsz temp3,1 goto $-2 retlw 00 ;**************************** ;* Sub Routines * ;**************************** ;detect key16 at start-up to generate key-loops for storage in EEPROM ;this sub-routine also detects a pressed-key and ;returns with a value in "loops" GenKeyvalue clrf loops bsf status, rp0 ;Bank 1 bcf TRISIO,5 ;clear bit GPIO,5 as output to charge cap. bcf status, rp0 ;bank 0 bsf gpio,5 ;make pin2 HIGH to charge cap. call _10mS ;charge cap bsf status, rp0 ;Bank 1 bsf TRISIO,5 ;set TRIS GP0,5 to detect discharged cap bcf status, rp0 ;bank 0 call _uS incf loops,1 btfsc gpio,5 goto $-3 retlw 00 ;return with value in loops ;NoKey - detect no key pressed NoKey bcf SwFlags,1 bsf status, rp0 ;Bank 1 (see if switch is pressed) bcf TRISIO,5 ;clear bit GPIO,5 as output to charge cap. bcf status, rp0 ;bank 0 bsf gpio,5 ;make pin2 HIGH to charge cap. call _10mS ;charge cap bsf status, rp0 ;Bank 1 bsf TRISIO,5 ;set TRIS GP0,5 to detect discharged cap bcf status, rp0 ;bank 0 call _1mS btfsc gpio,5 retlw 00 ;no switch pressed bsf SwFlags,1 retlw 00 ;key pressed - bsf SwFlags,1 ;ShowKey shows the key value via LED flashes ShowKey bsf gpio,0 ; call _500mS bcf gpio,0 call _500mS decfsz flashes,1 goto $-5 ; goto Main ;TestKeys is a test routine that flashes the key number TestKeys clrf loops clrf flashes incf flashes,1 call GenKeyvalue movf loops,w ; xorwf 50h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 51h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 52h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 53h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 54h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 55h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 56h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 57h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 58h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 59h,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 5Ah,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 5Bh,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 5Ch,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 5Dh,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 5Eh,w btfsc 03,z goto ShowKey incf flashes,1 movf loops,w ; xorwf 5Fh,w btfsc 03,z goto ShowKey retlw 00 ;after 16 keys . . . . ;put number of loops in 50h to 5Fh into EEPROM toEEPROM bsf status,rp0 ;select bank1 clrf eeadr ;start EEPROM at location 0 bcf status,rp0 ;select bank0 movlw .16 movwf temp16 ;for 16 keys movlw 4Fh ;start of GPR's movwf fsr incf fsr,f ;fsr starts at file 50h movf indf,w ;retreive data in file 50h to5Fh bsf status,rp0 ;select bank1 movwf eedata ; bcf status,rp0 ;select bank0 call write bsf status,rp0 ;select bank1 incf eeadr,1 bcf status,rp0 ;select bank0 decfsz temp16,f goto $-10 retlw 00 write bsf status,rp0 ;select bank1 bsf eecon1,wren ;enable write movlw 55h ;unlock codes movwf eecon2 movlw 0aah movwf eecon2 bsf eecon1,wr ;write begins bcf status,rp0 ;select bank0 writeA btfss pir1,eeif ;wait for write to complete goto writeA bcf pir1,eeif bsf status,rp0 ;select bank1 bcf eecon1,wren ;disable other writes bcf status,rp0 ;select bank0 retlw 00 ;************************************ ;* Main * ;************************************ Main call TestKeys goto Main END