Continue to Site

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 with PIC16F877A issues....

Status
Not open for further replies.

si2030

Member
Hi All,

Like a lot of others I am new to this. I wanted to develop a function generator so I designed a development board with a number of external components including a 4x4 matrix keypad. It all electrically is connected correctly and in the program below the flashing LEDs work but I cannot get the 4x4 board to even light up a LED..

I have used the code from here as a start
https://www.qsl.net/pa3ckr/signalgenerator/keyb441.txt

I have attached both the schematic and the code I have been working on.

In the end I have set both RB4-7 to INPUTS and RD4-7 to INPUTS. [should they be both inputs???] I am not sure this is OK but it seems to work in MPLAB when using stimulus.

There are weak pullups on PORTB. I am not sure how this ties into the process but there is 5 volts on the each of the pins on the matrix board (EDIT 5 volts on RB4-7 and 1.48volts on pins RD4-7)...

I have no way of know if the 4X4 board is connected properly.

Anyway any help here would be greatly appreciated...

Simon schematic..jpgView attachment keypad..txt
 
Last edited:
RB4-7 should be input but RD4-7 should be output and initially set high. You then set 1 of the port pins low and read RB4-7 to read one column. You repeat this for each of the row pins to read the complete keypad. I don't have time now to look at the code but will look tomorrow if you are still stuck.

Mike.
 
Dear Mike, Others

OK I am still having trouble getting this to work. I can get the LEDs to flash however I want but the 4x4 board does not work with the PIC.

Heres what I have done:


1. CHECK THE BASIC CIRCUIT: I am using a zif board to remove the PIC easily. I removed it and then set up a simple circuit (jump wired from 5V to RD4 then jumped a LED from RB4 to GND. 2k2 resistor sits between RB4 and the ROW 1 pin). I wanted to check that when I press a key the LED came on and that there was no shorts. OUTCOME - there is no voltage on the LED until the associated button is pressed.


2. CHECK THE CODE:Gone through my intitial code carefully. (I have uploaded this). I have set RD4-7 high and I do this at the end of the flashing LED routine. This sets up for the interupt on B should a button be pressed.

When Reset this is what I observe on the board.

The LEDs flash.

Press a key nothing happens.

Checked voltages and at PINS RB4-RB7 I have a voltage of 1.33V. This occurs when nothing is pressed and I have no idea why. It simply flashes indicating that the interupt is on all the time...

I then added in the following line.

BCF OPTION_REG, NOT_RBPU ;Enable weak pull-ups
;on port B

This forced the pins RB4-7 to be high. Again nothing happening when I pressed a key.

In conclusion it seems to be CODE and in particular making the interrupt work correctly.....

I added:

INTERRUPT
CALL FLASH4 ; Flash LEDs if interrupt tripped.
MOVLW B'11110000' ;Set RB4-RB7 HIGH.
MOVWF PORTD

to make sure that if the interupt did get tripped then it would flash all four LEDs to indicate it had made it to this routine..... it never did... I checked the pins and they are at 5V all the time with the weak pullups.


So is it possible for someone who knows this stuff alot better than me to have a look at the code (and the schematic just in case the design is wrong) and see if they can determine why it wont work on interupt?? There must be something fundamental I have not included.

Kind Regards

Simon
 

Attachments

  • keypad1..txt
    9.8 KB · Views: 273
The first thing I see that is wrong with your code is,
Code:
; PORTD bits
RD7			EQU	10000000		; Keyboard, ROW 1
RD6			EQU	01000000		; Keyboard, ROW 2
RD5			EQU	00100000		; Keyboard, ROW 3
RD4			EQU	00010000		; Keyboard, ROW 4
Should be,
Code:
; PORTD bits
RD7			EQU	b'10000000'		; Keyboard, ROW 1
RD6			EQU	b'01000000'		; Keyboard, ROW 2
RD5			EQU	b'00100000'		; Keyboard, ROW 3
RD4			EQU	b'00010000'		; Keyboard, ROW 4
Same for the other equates. However, in your code you are doing btfss PORTB,RB4 which will do btfss PORTB,16 instead of btfss PORTB,4. So you need to change one or the other.
Maybe you should change it to,
Code:
; PORTD bits
RD7			EQU	7		; Keyboard, ROW 1
RD6			EQU	6		; Keyboard, ROW 2
RD5			EQU	5		; Keyboard, ROW 3
RD4			EQU	4		; Keyboard, ROW 4


For the RB interrupt to work you need to make D4-7 zero so when a key is pressed it makes one of the PORT B pins go low.

Mike.
 
Hi Mike,

Thanks for the reply.

I am getting this logic backwards I think... I thought I was setting RD4-7 high and with a key pressed this would send one of the RB4-7 pins high thus setting off an interrupt. How should this work? it seems I should have it about face with RB4-7 high with a weak pullup and RD4-7low.

If this is the case then how does the interupt work when the RB4-7 pins are already high?

Simon
 
Hi simon,

The port B change interrupt happens whenever B4-7 change. If you turn on the WPUs then port B will be high. If you make the port D pins low then when a key is pressed it will make one of the port B pins go low and trigger the interrupt. The interrupt will also happen when you release the key as the port B pin will go back to the high level.

Mike.
 
Hi Mike, Others,

Again thanks for helping here.

I have attached my current code.

I have programmed the chip and then while working used a multimeter to check RB4-7 and each is sitting at 5V.

Firstly I am having a problem with the debugger on MPLAB. I cannot get the program to interrupt. I dont know why as I have set INTCON GIE and INTCON RBIF to "1" as well as INTCON INTF to "0". I have set stimulus so RB0 is toggled but it wont jump.

I have also changed RB4-7 to be INPUTS and RD4-7 to be outputs.

I also changed to Getkey routine.


Hope you can help.

Simon
 

Attachments

  • keypad3..txt
    9.7 KB · Views: 291
You are setting RBIF but you should be setting RBIE and clearing RBIF in the interrupt.

Also, you are calling routines from your interrupt that call other routines etc. This is not good, your interrupt is filling the stack and will crash. The stack is only 8 locations deep.

Mike.
 
Hi Mike, Others

I have added that line and I moved the START routine but it keeps jumping over the call statements.

I have added a jing video to show what it does... it shifts the literal values into w and jumps... also the debugger seems to stop on white space between the lines..

**broken link removed**

I have also added the asm file so you can see the program..

Not sure why its jumping over the call statements..

Kind Regards

Simon


Ignore this post as its actually for another thread :)
 

Attachments

  • LCD 1-0..asm
    11.5 KB · Views: 217
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top