Hardware for the keypad
Implementing the hex keypad is challenging. Before writing down an application program for a keypad, it is very important to understand how the hardware for the keypad was set up. A major part of the program varies with changes in the hardware.
- As we are using a 4Ć4 matrix keypad, a total of 8 input-output pins of the micro-controller will be required for interfacing.
- One half of the 8 pins will be hardware controlled and the other half will be software controlled.
[fusion_imageframe image_id=”4530″ style_type=”none” stylecolor=”” hover_type=”liftup” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”yes” gallery_id=”” lightbox_image=”http://learn.openlabpro.com/wp-content/uploads/2018/03/keypad.png” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2018/03/keypad.png[/fusion_imageframe]
- From the fig., the blue lines are the columns and the red lines are the rows. Therefore, the columns can be pulled up or pulled down. The rows are the four LSBs of the 8 bit pins and the columns are the MSBs of the 8 bit pins. (GENERAL CONVENTION: ROWS: Horizontal & COLUMNS: Vertical)
- In our program, the MSBs (columns) will be set in the input mode and the LSBs (rows) will be set in the output mode. The default state of the MSBs can be HIGH or LOW based on the hardware connection. Based on this we have two modes of operation for the keypad, i.e. Pull Up mode or Pull Down mode (refer the fig.). In the pull up mode, MSBs (columns) by default will be high and in the pull down mode, MSBs by default will be low. We will learn this chapter in the Pull-Up Mode operation.
- When a key is pressed, the output and input line or in other words, rows and columns common to the pressed key will be connected to each other and hence the value on the output line will be reflected on the corresponding input line.
- Without any software change, the columns, which we will be configuring as input pins in the software, will continue to be in the pulled up state. Therefore, when any key is pressed our aim is to cause a deviation from this default state. This change is performed in the software.
- When a software change occurs and a value is written on the output pins (rows), the four rows will reflect the value from the controller. This value gets passed to the corresponding column when the key common to the row and column is pressed. As we are operating in pull up state, we will continuously send a LOW state to each of the four output pins (rows) keeping all other pins HIGH.
- For every LOW state to each rows, we scan for a key press by checking for a LOW state at any of the column pins. This is because, when the rows and columns get connected, the circuit takes the path of least resistance (internal pull up when configured in the input mode is weak) and the LOW state on the row is reflected on the corresponding column, thus deviating from the default state. Based on this change in the state of the signal on the respective input line (column), we can determine the identity of the pressed key.
General setup for the program
- First we must set the direction of the hardware pins according to its software functionality. As discussed, the LSB (rows) is set in the output mode and the MSB (columns) will be set in the input mode.
- Next, we must define a function that performs a continuous scan and checks for a key press. The design logic is based on the following figure of the keypad connection and the functioning is as follows.
[fusion_imageframe image_id=”4531″ style_type=”none” stylecolor=”” hover_type=”liftup” bordersize=”” bordercolor=”” borderradius=”” align=”center” lightbox=”yes” gallery_id=”” lightbox_image=”http://learn.openlabpro.com/wp-content/uploads/2018/03/keypad_button.png” alt=”” link=”” linktarget=”_self” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]http://learn.openlabpro.com/wp-content/uploads/2018/03/keypad_button.png[/fusion_imageframe]
- The ROWS are the LSB pins and the COLUMNS are the MSB pins.
- Since the MSB is hardware controlled (pull up or pull down), these bits on the keypad will only take the logic value set in the hardware (LOGIC HIGH in our case).
- When a key is pressed, the corresponding row and column will be connected.
- We will be writing and shifting LOGIC 0 continuously to row ānā keeping all the other row pins at LOGIC 1. (n: 1-4)
- When a key is pressed and LOGIC 0 appears on the corresponding row pin, LOGIC 0 from the row will be reflected to the corresponding column.
- Since the columns are set as input pins in the microcontroller, while reading the values of the rows, if any of the pin is at LOGIC 0, then it is an indication that a key has been pressed.
- A function for reading the pressed key, must be defined which uses a switch to identify the row corresponding to the pressed key. If any of the switch case is satisfied, then it checks for the column corresponding to the pressed key. If the column reflects a LOGIC 0, then we can determine the value of the pressed key.
- Now, using a while loop, we will wait till the LOGIC 0 disappears so that the software waits till the current state in the hardware is changed. This is done to avoid a hardware de-bouncing. De-bouncing is a common issue that is caused when the hardware bounces its values in between software scans which causes an improper operation.
- A small delay must be provided in between each column write so that the reading operation does not get skipped.
- Once we have the value of the pressed key, this can be displayed and the software moves onto scan the next key press.