4
\$\begingroup\$

enter image description here

I am extremely new to microcontrollers and PIC programming. I need help in understanding how a 4x4 matrix keypad works.

I know the 8 pins are used to determine which of the keys are pressed by the user but other than that, I am lost in the concept of exactly how it works. What are the A,B,C,D outputs?

\$\endgroup\$
5
  • \$\begingroup\$ That's a keyboard matrix scanner chip which scans the matrix for you and gives decoded output with data strobe. You can download the chip data sheet to learn the details how it works. What does that have to do with microcontrollers and PIC programming? If you have this on an microcontroller evaluation board, it must have also documentation and examples how to work with it. \$\endgroup\$
    – Justme
    Commented Nov 12, 2021 at 9:01
  • \$\begingroup\$ Supply the one Row only at a time, change it periodically and after every change scan all of columns. If some column is high you will recognize what button was pressed according column/row combination. \$\endgroup\$
    – user208862
    Commented Nov 12, 2021 at 9:31
  • \$\begingroup\$ @MichalPodmanický I understand that but I don't understand what it outputs(ABCD)? \$\endgroup\$
    – Fiidisks
    Commented Nov 12, 2021 at 9:46
  • \$\begingroup\$ 74C922 is a key encoder IC and ABCD outputs gives you the button press code. If you are not going to use this IC and do the encoder in MCU you don't need to care about it. \$\endgroup\$
    – user208862
    Commented Nov 12, 2021 at 9:59
  • \$\begingroup\$ @MichalPodmanický How do the column and rows decide which button was pressed? \$\endgroup\$
    – Fiidisks
    Commented Nov 12, 2021 at 10:26

2 Answers 2

2
\$\begingroup\$

Assuming that you actually don't have a decoder chip like in your schematic, but instead an actual MCU, then:

Generally you check buttons from a cyclic timer, which checks the keys every 10ms or so. Some care needs to be taken for de-bouncing, like a simple median filter or just storing the previous results and comparing them - that's another topic.

Reading the keyboard can be done by for example having the columns as GPIO outputs and the rows as GPIO inputs (with pull resistors). Then activate one column at a time and check which row pins that were active. Basically something like this pseudo code:ish C language:

bool pressed[4][4] = {false};

for(uint8_t i=0; i<4; i++)
{
  COL_PORT = 1u << i; // activate one single column, deactive the rest
  uint8_t input = ROW_PORT; // read all row pins

  // assuming active high, then:
  for(uint8_t j=0; j<4; j++)
    pressed[i][j] = input & (1u << j); // implicit conversion to true if a bit is set
}

COL_PORT = 0; // no need to keep driving these once done reading
\$\endgroup\$
2
\$\begingroup\$

Some confusion is perhaps due to the fact that A,B,C,D appear on the keypad as well. These are just labels. You could have had "a","b","c","d" written on the keys.

  • Beware that the ..923 version has 20 pins.

For the 922 part:

  • pins 1,2,3,4 are Y1,Y2,Y3,Y4 respectively in the block diagram below: i.e. rows
  • pins 10,11,8,7 are X1, X2, X3, X4: i.e. column scans
  • Data Available is to interrupt the uC if desired.
  • Output Enable tristates these outputs, if you want to accomodate multiple devices on the same port.
  • The outputs A,B,C,D will change depending on what button is pressed, These go from 0000 to 1111, depending on the key pressed. Check the datasheet for this chip.

Let's say button 1 on the block diagram (not your figure) below is pressed. Its location is X2-Y1 on the matrix. If we scan that column output, i.e. make it 0 (since it is active low), then the current will go from Vcc through the INTERNAL pullup connected to Y1 and to ground through the column decoder. Hence Y1 will be at 0V and we can determine that the button is pressed at X2Y1. The ABCD output will be 1000 as the manufacturer has assigned it this way. Never assume any regular pattern or "natural" pattern for key assignments without consulting the datasheet.

This scan proceeds automatically, and whenever a keypress happens, you can interrupt the uC and provide it with data read from the ABCD lines. In the absence of this type of chip, all this functionality has to be provided using an interrupt to scan the key matrix.

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ I understand how its determined which key was pressed, what I am lost on is how it would look like in code. A lot of articles online show that if a particular key is pressed, then the corresponding character is the output but in this case, the output is 4bit so how do I even approach coding this. I am lost \$\endgroup\$
    – Fiidisks
    Commented Nov 12, 2021 at 19:14
  • \$\begingroup\$ esd.cs.ucr.edu/labs/decode_key/decode_key.html \$\endgroup\$
    – Syed
    Commented Nov 13, 2021 at 4:33
  • \$\begingroup\$ Hello, I am still unclear on the purpose of the output enabled pin \$\endgroup\$
    – Fiidisks
    Commented Nov 13, 2021 at 14:56
  • \$\begingroup\$ Lookup "tristate" outputs. It is done for uC buses. How do you connect many memory chips on the same data bus. The whole idea is that at a given time, because of proper address decoding, there is only one device/chip using the bus and the rest are in tristate or present a high impedance. This output enable would be coming from the address decode circuitry for IO and peripherals. \$\endgroup\$
    – Syed
    Commented Nov 13, 2021 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.