PDF Print E-mail

 

HOW TO: Interface a 4x3 Matrix Keypad to the FlamingICE Microcontroller 

 matrix keypad interface

 

You will need:

 

  • 4x3 Membrane Keypad (or any other similar keypads you have lying around)
  • Four 470? resistors
  • Wires (for connecting the keypad to the microcontroller)
  • A 6V-12V DC Power Supply
  • FlamingICE Microcontroller Board (FI28/FI40/FI80)

 

 

 

Introduction

In this tutorial, we’re going to use the most straight-forward way of obtaining which button is being pressed from the 4x3 Membrane Keypad. Please note that there are tons of methods out there on how to do this and while this might not be the best way, most beginners should find it simple and fast to get started from the hardware connections to the explanations on how the programming is done.

 

 

The Hardware

matrix keypad schematic

 

 

As per the diagram above illustrating the entire interfacing connections, we will be using a FI28 Microcontroller Board to do this. We will be using 7 I/O lines to connect to the 4x3 matrix keypad, with an extra power line that ties the rows ‘high’ through a 470 ? resistor on each row line.  We shall leave the columns alone and connect it directly to the Microcontroller Board.

 

Now that we’ve got the hardware up, let’s start programming.

 

The Software

The 4x3 Matrix Keypad is essentially pushbuttons arranged and connected in an XY matrix. Allow me to illustrate with the following figure.

 

keypad internal circuit

 

So what we are going to try to find out when a key is pressed is the row and column that a closed circuit is occurring. We do this by checking the columns and rows in turn.

We start off by setting the state of the pins used to logic ‘high’. This is to initialize the pins at the beginning.

CODE:

High(1)

High(2)

High(3)

High(4)

High(5)

High(6)

High(7)

It really doesn’t matter which one we check first but we are going check the columns first, then the rows.

 

The pressing of key would result in the flow of current and one or more of the input pins would be logic ‘low’. Since current flows from a higher potential to a lower potential, we would need to set the column pins high and the row pins to a logic low when scanning the columns. This action is reversed when scanning the rows.

 

CODE:

‘scanning the columns

low(1)

low(2)

low(3)

low(4)

high(5)

high(6)

high(7)

‘wait for voltage to settle

Delay(1)

C1 = GetPin(5)

C2 = GetPin(6)

C3 = GetPin(7)

 

Here’s a little trick that we’re going to apply based on our hardware connections.

We are going to combine the logic state of the pins as individual bits into a single byte. However, since we only have 7 lines, we will only use 7 bits.

E.g. we let column3 on pin 7 represent the 7th bit in a byte.

 

keypad_bytePos.png

Following the same concept for the rest of the pins, we apply a little math after getting the logic state of the pins.

 

 

CODE:

 

‘Scanning the columns, and then doing the math

C1 = GetPin(5)

C2 = GetPin(6)

C3 = GetPin(7)

d1 = c1 * 16 + c2 * 32 + c3 * 64

 

‘Scanning the Rows, and then doing the math

C1 = GetPin(1)

C2 = GetPin(2)

C3 = GetPin(3)

C4 = GetPin(4)

d2 = c1 * 1 + c2 * 2 + c3 * 4 + c4 * 8

 

The value obtained later on should correspond to the respective key pressed. The figure below can be used as a reference.

 

keypad_byteVal.png

 

TIP: Using the keys to carry out commands is now as simple as using a select case statement based on the byte value.

 

CODE:

 

'Module1

Public C1, C2, C3, C4 As Integer

Public  d, d1, d2 As Integer

'======================================================================

'======================================================================

 

Public Sub Main()

 

High(1)

High(2)

High(3)

High(4)

High(5)

High(6)

High(7)

 

 

‘keep checking for a key to be pressed
Do

      low(1)

      low(2)

      low(3)

      low(4)

      high(5)

      high(6)

      high(7)

‘wait for voltage to settle

      Delay(1)

      C1 = GetPin(5)

      C2 = GetPin(6)

      C3 = GetPin(7)

      Delay(1)

      C1 = GetPin(5)

      C2 = GetPin(6)

      C3 = GetPin(7)

      d1 = c1 * 16 + c2 * 32 + c3 * 64

 

      high(1)

      high(2)

      high(3)

      high(4)

      low(5)

      low(6)

      low(7)

‘wait for voltage to settle

      Delay(1)

      C1 = GetPin(1)

      C2 = GetPin(2)

      C3 = GetPin(3)

      C4 = GetPin(4)

      Delay(1)

      C1 = GetPin(1)

      C2 = GetPin(2)

      C3 = GetPin(3)

      C4 = GetPin(4)

 

      d2 = c1 * 1 + c2 * 2 + c3 * 4 + c4 * 8

 

      d = d1 + d2

      debug.Print cstr(d)

      Delay(200)

Loop

 

End Sub

 

 

 


download_sourceCode.png

 

 

 

 

 

 
Previous Next
Two New Tutorials
Learn how to control a Servo Motor with the FIDE, as well as adding a wireless peer-to-peer communication link using the EBlue Bluetooth Module.
Hoorah! We've Updated!
3 Cheers to a new tutorial, a FIDE patch, and a Language Reference Update!
New Sensor and Documentation Updates

 

We now carry the distance measuring sensor, SHARP GP2D12, WITH the compatible 3pin connector.

In other updates, we have uploaded the technical paper for our GLCD+RTC Combo complete with example configurations to our FlamingICE Boards. 

Our Hardware Reference now stands at version 1.5 with slight changes to the pin-outs on the FI28 Board. 

 

Christmas Updates

Download the latest FIDE v2.1 and get all the updated documentation that comes along with it!

Check out the new goodies that we have over at our updated Products Page!

Resource Page

Now that FIDE 2.0 is up, we published the Resource Page with 2 major tutorials to boot.

Start clicking and head over to check out those tutorials!

FIDE v2.0 RELEASED!

AIS Cube is proud to release the latest and most complete installment to date, FIDE v2.0. Delivering faster feature packed goodness, whats not to love?

Head over to the downloads tab and grab your copy!

FIDE v1.4.2 PATCH

IMPORTANT: Responding to a bug report, the FIDE v1.4.2 PATCH is a patch for a major bug fix in the FIDE SD Tool. Please ensure that you have FIDE v1.4.2 installed before proceeding with the Patch.

Patch is available over on the Downloads Tab.

FIDE v1.4 Released!
More features with the latest release of FIDE at version 1.4. Extended syntax highlighting range, greater support for FIDE system language instructions...
FIDE v1.3 Released!

FIDE v1.3 is now available for download over at the downloads tab. Featuring bug fixes and other features such as an expanded syntax highlighting coverage, it is highly recommended that you upgrade to the newest version.

AVRIL LE BOB

We have a new project named AVRIL LE BOB up over at the projects page (with videos!!). Check it out today!


Copyright © 2007-2008. AIS Cube. All Rights Reserved.
All trade and/or services marks mentioned are the property of their respective owners.
Joomla! is Free Software released under the GNU/GPL License.