Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

4x4 Matrix Keypad PIC18F6520

Status
Not open for further replies.

goaliejoe35

New Member
I am trying to integrate a 4x4 matrix keypad into a previous code that was used to light a 7 segment LED when BCD was entered via a dip switch. I am having trouble trying to write the code for the keypad.

I want to be able to press any button on the keypad and make the 7 segment LED display the correct character for as long as the key is pressed and for 1 second after it is released. The '*' and '#' keys should make the LED go dark.

Can someone help me add to the code below to make this work?

The 4x4 keypad I am using is set up like this:
C1 C2 C3 C4
R1 1 2 3 A
R2 4 5 6 B
R3 7 8 9 C
R4 * 0 # D

Here is the code I have: (I also attached it as a document)

#include "CS110000.h"

#define USED 1 // Setting numbers that are used

// Storage/Loop Counter
#define W 0
#define STARTLOOP 16
#define I 17
#define J 18
#define K 19
#define L 20
#define C1 21
#define C2 22
#define C3 23
#define C4 24
#define NUM 25

// Outputs
#define LTA LATE
#define LTB LATE
#define LTC LATG
#define LTD LATG
#define LTE LATF
#define LTF LATF
#define LTG LATF
#define A 1
#define B 0
#define C 0
#define D 1
#define E 4
#define F 6
#define G 5

// Inputs
#define IN1 PORTD
#define IB1 7
#define IN2 PORTD
#define IB2 6
#define IN3 PORTD
#define IB3 5
#define IN4 PORTD
#define IB4 4

// For comparing, Indirect, read/writting register
#define CREAD INDF0
#define CWRITE FSR0

void main(void)
{
_asm // Setting Values for delay
MOVLW 5
MOVWF L, ACCESS
MOVLW 198
MOVWF I, ACCESS
MOVLW 198
MOVWF J, ACCESS
MOVLW 85
MOVWF K, ACCESS
// Move 0 to W and set reg 1-16 as unused
MOVLW 0
MOVWF 1, ACCESS
MOVWF 2, ACCESS
MOVWF 3, ACCESS
MOVWF 4, ACCESS
MOVWF 5, ACCESS
MOVWF 6, ACCESS
MOVWF 7, ACCESS
MOVWF 8, ACCESS
MOVWF 9, ACCESS
MOVWF 10, ACCESS
MOVWF 11, ACCESS
MOVWF 12, ACCESS
MOVWF 13, ACCESS
MOVWF 14, ACCESS
MOVWF 15, ACCESS
MOVWF 16, ACCESS

// Set up outputs
BCF TRISE, 1, ACCESS
BCF TRISE, 0, ACCESS
BCF TRISG, 0, ACCESS
BCF TRISG, 1, ACCESS
BCF TRISF, 4, ACCESS
BCF TRISF, 6, ACCESS
BCF TRISF, 5, ACCESS

// Set up inputs
BSF TRISD, IB1,ACCESS
BSF TRISD, IB2,ACCESS
BSF TRISD, IB3,ACCESS
BSF TRISD, IB4,ACCESS

CALL CLEAR, 0 // Clear all outputs
CALL DELAY, 0 // Initial Delay for 5 seconds

LOOP: MOVLW 0 // Initial Work Reg to 0 to add correctly
CALL GINPUT, 0 // Get the inputs/Display Hex number
CALL DELAY, 0 // Delay - hold for 5 seconds
// Setting where to start pointing and
// Working register to compare
MOVLW STARTLOOP // Puts whats in location 4 into W
MOVWF CWRITE, ACCESS // Writes position to FSR0
MOVLW 1 // Moves current 1 to W (not address)
CALL ALLUSED, 0 // Checks to see if all are used/termination
BRA LOOP // Branch back to get input

GINPUT: BTFSC IN1, IB1, ACCESS // Most Sig bit
ADDLW 8 // If on then add W + 8
BTFSC IN2, IB2, ACCESS // 2nd Sig bit
ADDLW 4 // If on then add W + 4
BTFSC IN3, IB3, ACCESS // 3rd Sig bit
ADDLW 2 // If on then add W + 2
BTFSC IN4, IB4, ACCESS // Least Sig Bit
ADDLW 1 // If on then add W + 1
MOVWF NUM, ACCESS // Move the final number to NUM
CALL OUTPUT, 0 // Correctly get the right output
RETURN 0 // Return to main loop

// Calls all the displays, if it is the correct one - displays it
OUTPUT: CALL L0, 0
CALL L1, 0
CALL L2, 0
CALL L3, 0
CALL L4, 0
CALL L5, 0
CALL L6, 0
CALL L7, 0
CALL L8, 0
CALL L9, 0
CALL LA, 0
CALL LB, 0
CALL LC, 0
CALL LD, 0
CALL LLE, 0
CALL LF, 0
RETURN 0

// At beginning of program, sets WREG to 1 to test if its been used
ALLUSED:CPFSEQ CREAD, 0 // Compares what in ADDRESS to W
RETURN 0 // IF CREAD != W(1) not used then RETURN
DECFSZ CWRITE, 1, ACCESS // Decrease memory location
BRA ALLUSED // Go back to loop
CALL ENDPRO, 0 // If all been used check to End Program
RETURN 0 // Return

// Structure for L0-LF is the same
L0: MOVLW 0 // Move number we are at into W
CPFSEQ NUM, ACCESS // Check to see if it is the right number
RETURN 0 // If it isn't then return and check others
CALL CLEAR, 0 // The right number so clear previous number
// Setting the pins for the specific number
BSF LTA, A, ACCESS
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
BSF LTD, D, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
MOVLW USED // Moving the USED bit into W
MOVWF 1, ACCESS // Marking (NUM + 1) as used - fixs offset of checking 0
RETURN 0 // Return

L1: MOVLW 1
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
MOVLW USED
MOVWF 2, ACCESS
RETURN 0

L2: MOVLW 2
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTB, B, ACCESS
BCF LTC, C, ACCESS
BSF LTA, A, ACCESS
BSF LTG, G, ACCESS
BSF LTE, E, ACCESS
BSF LTD, D, ACCESS
MOVLW USED
MOVWF 3, ACCESS
RETURN 0

L3: MOVLW 3
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
BSF LTD, D, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 4, ACCESS
RETURN 0

L4: MOVLW 4
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 5, ACCESS
RETURN 0

L5: MOVLW 5
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTC, C, ACCESS
BSF LTD, D, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 6, ACCESS
RETURN 0

L6: MOVLW 6
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTC, C, ACCESS
BSF LTD, D, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 7, ACCESS
RETURN 0

L7: MOVLW 7
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
MOVLW USED
MOVWF 8, ACCESS
RETURN 0

L8: MOVLW 8
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
BSF LTD, D, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 9, ACCESS
RETURN 0

L9: MOVLW 9
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 10, ACCESS
RETURN 0

LA: MOVLW 10
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 11, ACCESS
RETURN 0

LB: MOVLW 11
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTC, C, ACCESS
BSF LTD, D, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 12, ACCESS
RETURN 0

LC: MOVLW 12
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTD, D, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
MOVLW USED
MOVWF 13, ACCESS
RETURN 0

LD: MOVLW 13
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTB, B, ACCESS
BSF LTC, C, ACCESS
BSF LTD, D, ACCESS
BSF LTE, E, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 14, ACCESS
RETURN 0

LLE: MOVLW 14
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTD, D, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 15, ACCESS
RETURN 0

LF: MOVLW 15
CPFSEQ NUM, ACCESS
RETURN 0
CALL CLEAR, 0
BSF LTA, A, ACCESS
BSF LTE, E, ACCESS
BSF LTF, F, ACCESS
BSF LTG, G, ACCESS
MOVLW USED
MOVWF 16, ACCESS
RETURN 0

// Sets all the output pins to 0
CLEAR: BCF LATE, 1, ACCESS
BCF LATE, 0, ACCESS
BCF LATG, 0, ACCESS
BCF LATG, 1, ACCESS
BCF LATF, 4, ACCESS
BCF LATF, 6, ACCESS
BCF LATF, 5, ACCESS
RETURN 0

// Delays the controller for 5 seconds
// A quad-for loop, took our original 1 sec delay
// And added a loop to run that 5 times.
DELAY: MOVFF I, C1
D1: MOVFF J, C2
CALL D2, 0
DECFSZ C1, 1, ACCESS
BRA D1
RETURN 0
D2: MOVFF K, C3
CALL D3, 0
DECFSZ C2, 1, ACCESS
BRA D2
RETURN 0
D3: MOVFF L, C4
CALL D4, 0
DECFSZ C3, 1, ACCESS
BRA D3
RETURN 0
D4: DECFSZ C4, 1, ACCESS
BRA D4
RETURN 0

ENDPRO: MOVLW 0 // Moves 0 to W
CPFSEQ NUM, ACCESS //Check to see if NUM is 0
RETURN 0 // If it isnt then return
CALL CLEAR, 0 // Else Clear
BRA ENDPRO // Termination.
RETURN 0 // Return - never reached.

_endasm
}
 

Attachments

  • Project3_Code.doc
    41 KB · Views: 320

Pommie

Well-Known Member
Most Helpful Member
I posted code to read a 3*4 keypad in this thread. It is in C but should be easy to follow if you know C. You appear to be using asm within a C file. Do you know C?

Mike.
 

goaliejoe35

New Member
I know some C but I'm not an expert. I am using asm because I'm required to do this project in asm code. Could you help me with asm?
 

colin55

Well-Known Member
We need a circuit diagram before we can provide a program.
You can connect the keypad to the same lines as the 7 segment display (to scan the keypad). Simply output lows and detect when a row is low.
 

Pommie

Well-Known Member
Most Helpful Member
You can connect the keypad to the same lines as the 7 segment display (to scan the keypad). Simply output lows and detect when a row is low.

As long as you use blocking diodes (or resistors), otherwise pushing multiple buttons will short between segments.

Mike.
 

colin55

Well-Known Member
You can add 470R resistors or connect to the 180R resistors driving the LEDs. Pushing two buttons at the same time will show on the display, but it will be absolutley minimal.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top