;keypad.asm ; Example of reading the keypad ; LIST P=16F877, R=DEC INCLUDE "C:\MPLAB\P16F877.inc" ; Definitions ; __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC ; Define start of vars ScratchPad EQU 0x20 ; Variables KCode EQU ScratchPad+0 ; Value from keypad Row EQU ScratchPad+1 Count EQU ScratchPad+2 Temp EQU ScratchPad+3 CurrD EQU ScratchPad+4 ; Start of the Program ORG 3 goto Start ; This code reads the keypad and puts the code ; on the led_bar. ; ORG 0x50 Start ; Set up RC3 to be output, RB3 to be input, A4-A0 to be output. bsf STATUS, RP0 clrf TRISA movlw b'11110000' movwf TRISB clrf TRISD bcf STATUS, RP0 ; A simple loop checks all of the rows or until something is returned BigLoop movlw b'10000000' ; set the scan row to begin with movwf Row movlw 4 movwf Count clrf KCode ; nothing read ScanLoop call ScanKeyPadRow movlw 0xF0 andwf KCode,F ; if KCode is zero, nothing was read btfss STATUS,Z goto KeyRead bcf STATUS,C ; rotate the row indicator rrf Row,F decfsz Count,F goto ScanLoop ; We've done the scan, KCode should contain any button pressed ; loop to continuously read the keypad. If KCode is zero, there is nothing ; there KeyRead movf KCode,W call DispLedBar goto BigLoop ScanKeyPadRow movlw 0x1D ; enable keypad output movwf PORTA movf Row, W ; write the row number to the keypad movwf PORTD fill (nop),10 ; wait for scan to complete ; Read port B. The high order 4 bits contain the 4 bits of the scan, ; where each bit represents a different key in the row. Note that the ; polarity is reversed - 1 = no and 0 = yes. movf PORTB, W movwf KCode movlw 0x10 ; Disable reading of keypad movwf PORTA movlw 0xF0 andwf KCode,F movlw 0xF0 ; Reverse the reverse bit meaning xorwf KCode,F return ; ======================================================================= ; Routine to display a code on the led bar DispLedBar ; Enable the led bar and put the value in W there. movwf CurrD ; save W movlw 0x12 movwf PORTA comf CurrD,W movwf PORTD return ; Wait forever Forever waitforever nop goto waitforever return END