PDA

View Full Version : Keyboard Matrix with a Twist



k3v1nP
- 27th October 2008, 07:18
I'm wondering if its possible to use a similar circuit to the keyboard matrix circuit that you can find all over the place. What I'm trying to do is replace all the normally open switches with normally closed ones. Then with picbasic pro write a program that will tell you what switches if any are open. I've done a little experimenting and have got some results but its not perfect. I can get it to work with the top row of switches but does nothing after that.

This is my code ....
;-----------------------------------------------------------------------

CLEAR
DEFINE OSC 4 ; System speed
DEFINE LCD_DREG PORTD ; Define LCD connections
DEFINE LCD_BITS 4 ;width of data path
DEFINE LCD_DBIT 4 ;data starts on bit 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
ADCON1 = 7 ; Make PORTA and PORTE digital
LOW PORTE.2 ; LCD R/W low (write)
PAUSE 200 ; Wait for LCD to start

; define the variables
BUFFER VAR BYTE
ALPHA VAR BYTE ; counter for rows
COLUMN VAR BYTE
ROW VAR BYTE
SWITCH VAR BYTE

; SET UP PORT B PULL UPS
OPTION_REG.7 = 0 ; Enable PORTB pullups to make B4-B7 high
TRISB = %11110000 ; Make B4-B7 inputs, B0-B3 outputs,

; Set up the initial LCD readings
LCDOUT $FE, 1 ; Clear the LCD

BUFFER =PORTB
BUFFER = BUFFER ^%11111111

; print the first line
LCDOUT $FE, $80, "ROW=",BIN4 (BUFFER & $0F), "COL=", BIN4 BUFFER >>4
LCDOUT $FE, $C0,BIN8 BUFFER

END ; always end all programs with an END statement

;----------------------------------------------------

The last LCDOUT line displays 00010001, 00110001, 01110001, 11110001 when I run the program for 1st switch on (open), 1st and 2nd switch on (open), 1st, 2nd and 3rd switch on (open) and all the top row on. Or any combination of the top row the respective 0 or 1 is displayed. Nothing seems to work at all for any of the other rows of switches. I think its stuck on the first row. I'm trying to get to the point were if all switches are on it would display 11111111.
To add, I'm pretty new to the micro controller and picbasic programming. I'm using a Melabs Lab-X1 board with a PIC16F877A MPU. I'm not sure if I can do this with the circuit or if its just a matter of the programming. Any help would be greatly appreciated.

skimask
- 27th October 2008, 13:36
http://www.picbasic.co.uk/forum/showthread.php?t=3250&highlight=matrix

k3v1nP
- 27th October 2008, 16:47
A conventional keyboard matrix can be drawn as rows and columns of wires, with a switch where each row wire crosses a column wire.
You scan the matrix by (for example) having pull-up resistors on each of the column wires. You drive one of the row wires low, and see which of the column wires have gone low in response - and you conclude that the switches on the junctions between the driven row and the responding column(s) are closed (i.e. pressed).

This is fine until you have several switches closed.
Suppose you have the switches that join row A with column 2, row A with column 3 and row B with column 3 all closed.
When you pull row B low, column 3 will be pulled down.
The problem is that this will pull row A low. And this will pull column 2 low so you think you have the switch between row B and column 2 closed as well.
(Sorry I'm no good at ascii art).

There is a fix:
Put a diode in series with each switch.

Hope this helps.
- Danish

mister_e
- 27th October 2008, 17:46
appart from having an existing zillion of these in stock, could you explain WHY you want to use closed-type switches???

Sure it's all doable...

k3v1nP
- 28th October 2008, 00:49
I'm trying to make my own alarm system.

Archangel
- 28th October 2008, 03:31
Welcome back Steve.

aratti
- 28th October 2008, 11:51
You can solve the problem with a 16 channels mux (74hc4067)

You connect one input pin of your pic to pin 1 of the mux.

Ground pin 12 and pin 15 of mux (Enable)

Connect pin 24 to + 5V

Connect Pins 10; 11; 13 and 14 to 4 output pins of your pic.

Pullup all 16 mux inputs with a 10k resistor

Ground all 16 mux input with n.o. switches

Write a code to scan the mux. Read input pin. If pin=0 then switch is close.
If pin=1 then switch is open.
Scan position will tell you wich swicth has been activated.

Al.

k3v1nP
- 28th October 2008, 12:37
You can solve the problem with a 16 channels mux (74hc4067)

Ground all 16 mux input with n.o. switches

Al.

Can this be used with normally closed switches?

aratti
- 28th October 2008, 13:30
Yes I made the mistake to write n.o. intending n.c. Sorry for that!

Al.

skimask
- 28th October 2008, 13:46
Can this be used with normally closed switches?
What's up with the switches?
Are they what you have on hand and want to use them up?
Are you short of pins on your present project, yet still need to hook up the switches?
Just trying to get a feel for a good solution. The '4067 isn't a bad solution, for an N.O. switch. But I'm thinking, if you're going to go that route, may as well whip up another PIC to do the tough work for you...

k3v1nP
- 29th October 2008, 04:32
I'm trying to make an alarm system and I don't want it to be limited by to few switches. Now I know everyone is going to say how many doors do you have that you want to put a sensor on. Well its only 3 so far but there are other things like outside gates, garage door, mailbox (then I can have my computer go "you've got mail" and it be real mail ... lol). I'm sure there are other things as well but thats all I've got right now. If i'm going to use up eight pins I might as well get as many switches as I can out of them.
The using a whole other pic might not be a bad idea. hmmmm

skimask
- 29th October 2008, 04:40
If i'm going to use up eight pins I might as well get as many switches as I can out of them.
Well, if you've got a batch of precision resistors, theoretically, you could get 1024 switches on each analog input pin on a PIC with a 10bit ADC.
Or a 74C150, 16 switches per pin, 4 pins, 4 select bits, 64 switches total. But again, a secondary PIC might be easier.

aratti
- 29th October 2008, 09:01
I don't recommend using analog for this application, due to the false allarms generated the system will result useless. Stay on digital!

Al.

skimask
- 29th October 2008, 10:51
I don't recommend using analog for this application, due to the false allarms generated the system will result useless. Stay on digital!

Al.

Well, I don't recommend it either...but it is possible. Yes, it would be noisy. Ya think a filter might work?
:rolleyes: