Matrix 4×4 keypad interfacing with pic microcontroller is another one of the most wide problem which we often face and in today’s tutorial we are going to explain the 4×4 keypad interfacing with pic16f877a microcontroller. It makes very special problem to not even decode the matrix 4×4 keypad but also to open the door of all kind of matrix scanning you can do in assembly language. Let’s jump into the implementation of pic microcontroller based keypad interfacing in assembly language
Components Required:
- PIC16F877A microcontroller
- 4×4 matrix keypad
- Breadboard
- Jumper wires
Connections:
- Keypad: rows to RB0-RB3, columns to RC0-RC3
Key Concepts:
- Keypad scanning
- Assembly language programming
- Input/output interfacing
PIC Assembly code for Keypad interfacing
Here is a complete code for interfacing the 4×4 keypad with PIC16f877A microcontroller.
LIST P=16F877A
INCLUDE<P16F877A.INC>
ORG 0
GOTO START
ORG 4
START:
BSF STATUS, RP0 ; Select bank 1
MOVLW B'00000000' ; Set PORTB as output
TRISB
MOVLW B'11110000' ; Set PORTC as input
TRISC
CLRF COUNT ; Initialize COUNT
CLRF TEMP ; Initialize TEMP
LOOP:
MOVLW 0x0F
MOVWF COUNT1
ROW:
BCF PORTB, 0
BCF PORTB, 1
BCF PORTB, 2
BCF PORTB, 3
BSF PORTB, COUNT ; Set output to current row
COL:
BTFSS PORTC, 0
GOTO COL1
BTFSS PORTC, 1
GOTO COL2
BTFSS PORTC, 2
GOTO COL3
BTFSS PORTC, 3
GOTO COL4
GOTO LOOP
COL1:
MOVLW COUNT
ADDWF COUNT, F ; Update TEMP with current row and column
MOVLW .50 ; Delay for debounce
CALL DELAY
GOTO LOOP
COL2:
MOVLW COUNT
ADDWF COUNT, F
ADDWF COUNT, F ; Update TEMP with current row and column
MOVLW .50
CALL DELAY
GOTO LOOP
COL3:
MOVLW COUNT
ADDWF COUNT, F
ADDWF COUNT, F
ADDWF COUNT, F ; Update TEMP with current row and column
MOVLW .50
CALL DELAY
GOTO LOOP
COL4:
MOVLW COUNT
ADDWF COUNT, F
ADDWF COUNT, F
ADDWF COUNT, F
ADDWF COUNT, F ; Update TEMP with current row and column
MOVLW .50
CALL DELAY
GOTO LOOP
DELAY:
MOVWF TEMP1
LOOP1:
MOVLW .250
MOVWF COUNT1
LOOP2:
NOP
DECFSZ COUNT1, F
GOTO LOOP2
DECFSZ TEMP1, F
GOTO LOOP1
RETURN
COUNT EQU 20h
TEMP EQU 21h
TEMP1 EQU 22h
COUNT1 EQU 23h
END
Code language: PHP (php)
Key Functions:
START
: Initializes PORTB and PORTC and starts the main program loopLOOP
: Scans the keypad and updates TEMP with the current keypressROW
: Sets the output to the current rowCOL
: Checks the current column for a keypressDELAY
: Delays for debounce
Tips:
- Adjust the delay time as needed for your specific application.
- Modify the code to handle the specific characters or functions that your keypad uses.
This code-centric guide demonstrates how to interface a 4×4 matrix keypad with a PIC microcontroller using assembly language programming. By following the provided code and connections, you can create a functional keypad interface for various applications.