Introduction: How to Connect a Keypad to the Iolinker FPGA Board
We intend to connect a 4x4 matrix keypad to the iolinker FPGA board and react to button presses.
To read in the key matrix, we will cycle through all four columns, with one column output high (all others pulled down) at a time. After every column switch, we read in the four row pulldown inputs. When a button is pressed, the appropriate row input reads high.
Step 1: The Hardware
Get a 4x4 matrix keypad (https://www.parallax.com/product/27899) or build one yourself and plug it into your FPGA board. Any GPIO pins (P…) are fine.
Wire up the FPGA board (https://jinvent.de) to your 3.3V master device as usual. In the picture earlier the FPGA board was connected to a Raspberry Pi.
Step 2: Parameters
You can find the main program keypad_example.cpp in the git repository https://github.com/jvm123/iolinker. At least download the keypad_example.cpp, IOLinker.cpp, IOLinker.h and Makefile files.
Let's define the relevant parameters for our 4x4 keypad. The KEYPAD_ROW0 define is vital for configuring the lowest pin your keypad is connected to. All other pins are derived from that automatically. If the keypad is connected to P17 - P31, enter '16' (= P17).
Since it is convenient to plug in the 1x8 connector from the keypad directly onto one of the 2x10 headers or onto the 2x13 header on the FPGA board, the PIN_INTERVAL define allows you to only use every second pin.
Step 3: The Keypad Code
Now for the function that determines the actual key that is pressed:
First all pins are configured as inputs, and then one column is changed to a high output at a time. When the user presses a button, the row input is connected to the column pins — and only the column currently configured as an output can pull the row input high.
This would even allow to detect simultaneous button presses individually, but the example code only returns the first one it finds.
Note that it is vital that the inputs are pulldowns; as they are implicitly on the iolinker board. If they weren't, floating input pins could cause random behaviour.
Step 4: The Main Program
The main program can continuously call the function and react to button presses, or, as in this example, just output them.
Step 5: Trying It Out
To compile the example on a Linux PC or Raspberry Pi, just type 'make keypad_example' in a console window. Then run with 'sudo ./keypad_example'.
Verify that the main() initialization routine connects to the appropriate port as described at https://github.com/jvm123/iolinker.