Switch in Matrix

Thread Starter

rt694157

Joined Dec 15, 2019
68
I don't understand how 4*4 matrix keypad works. first I thought why not just try to understand how only one switch works in a matrix keypad.

I assume PIC microcontroller and V+ is 5V DC.

1. Is following picture is useful to understand logic to read one switch in matrix keypad?​
2. How to know if switch have been pressed ?​
3. What will the input pin of microcontroller ?​
4. What will the output pin of microcontroller ?​

switch.png

Note - This may be not a good way to understand matrix keypad but this is my one attempt to understand matrix keypad
 

MrChips

Joined Oct 2, 2009
23,926
No. That is not a good way to understand how these switches work.
You need to look at the entire matrix.

1623699606343.png

A 4x4 keypad matrix of sixteen pushbuttons would be interfaced to 8 GPIO lines of an MCU.
Eight GPIO lines can be randomly chosen. However, it would make life simpler and the programming easier if the 8 lines were grouped into two groups of four. The two groups can be of the same port such that a single 8-bit port is utilized.

In order to make the discussion easier to follow we will connect lines Y1-Y4 to PORTA0-PORTA3.
Lines X1-X4 will be connected to PORTB4-PORTB7.

Now, there are two styles of programming.

1) We can create a program that continuously scans all 16 pushbuttons to see which one is pressed. The scan rate does not have to be particularly fast. For example, if the program took 1ms to scan each key it would take 16ms to scan all 16 keys. This is faster than the time it takes for a human to press and release one key.

2) Scanning takes up precious CPU clock cycles. A better way is to use interrupts. The MCU is fully available to perform useful tasks. The MCU responds to a key-pressed event only when a key is actually pressed.

I will briefly introduce method (2).

Of the two 4-bit ports, one port is always configured as OUTPUT. The other port is always configured as INPUT. It does not matter how you select which is input and which is output.
Let us configure PORTA0-PORTA3 as OUTPUT (Y1-Y4).
Let us configure PORTB4-PORTB7 as INPUT (X1-X4).

The output port (Y1-Y4) is always driven either LOW or HIGH (low impedance).
The input port (X1-X4) is in a high impedance state. Hence the lines are floating. We need to assert the inputs as either LOW with a pull-down resistor, or HIGH with a pull-up resistor. You may choose either.
For simplicity of discussion we will pull down all four lines at X1-X4 with four 10kΩ resistors to GND.

Preliminary
When the MCU reads PORTB, it reads 0000XXXX where X represents an unknown condition, i.e. we don't know what function has been assigned to PORTB0-PORTB3.

In the program we mask the data received by using an AND operation with a mask 11110000.

Step 1
In the program, we set PORTA0-PORTA3 to all HIGH.
For example:
PORTA = 0x0F;
(Note that if PORTA4-PORTA7 are being used for another function then this must be taken into account.)

With all switches in normal open (NO) state, PORTB reads back as 00000000, after the AND operation.
If any key is pressed, PORTB will read back a non-zero value.
We configure the MCU to respond to an interrupt on change, i.e. when an input pin changes state an interrupt is initiated.

Now the MCU has detected that a key is pressed. We do not know at this point which key is pressed.
This is the next step, scanning the 4x4 matrix.

Do you understand everything so far?
 
Top