Object Application Note #1
Using the oKeypad Object to read a Matrix Keypad.

Back to top of page Introduction:
 This Object Application Note demonstrates how to use the oKeypad Object to read a 4 x 4 Matrix Keypad.

A keypad is a convenient way of entering data into the OOPic without tying up one I/O line for each switch. A 4 row by 4 column keypad consists of 16 individual push buttons, but only needs 8 I/O lines to determine which of the sixteen keys has been pressed. 

The OOPic has a Hardware Object called oKeypad which has been specifically designed to scan the 8 I/O lines connected to a 4 x 4 keypad and provides a Key property that indicates which key was last pressed and a Received property that indicates when a button is being pressed.

A 4 x 4 keypad is often referred to as a Hexadecimal Keypad because it has 16 keys on it.  With 16 keys, the keypad can represent the full numeric span of a 4-bit number.  

Back to top of page Operation:
 A 4 row by 4 column keypad consists of 16 individual push buttons and 8 wires.  Each switch will connect two of the 8 wires together when pressed.  The 8 wires are arranged in a 4 wire by 4 wire grid pattern and when any one of the 16 push buttons is pressed, a contact is made between the wire that is associated with the switches column and the wire that is associated with the switches row. By scanning the 8 wires for these connections, the button that is pressed, if any, can be determined.

The oKeypad Object, when its Operate Property is set to 1, continuously performs a scan of the 8 I/O lines 8 - 15 which it is expecting to be connected to the wires on the keypad.  When a button is pressed and its connection is detected between any one of the row wires and any one of the column wires, the Key property of the oKeypad Object is updated with the button's value and the Received property is set to 1.  If two connections are detected, then the Key property will be updated with the button with the highest value.  When the button is released, the Received property is set to 0, but the Key property remains unchanged.

Because of the way that the oKeypad Object scans the 8 wires, no other external hardware components are needed in addition to the keypad itself. This is accomplished by the oKeypad Objects using the OOPic I/O lines 8 - 15 which have internal pull-up resistors.  The I/O lines also do not need to be diode protected because the scan pattern only outputs to one I/O line at any given time.

Back to top of page Programming:
To use the oKeypad Object, the programmer only needs to declare that an oKeypad Object is needed and to turn it on.
Visual Basic SyntaxC and Java Syntax
Dim A As New oKeypad

Sub Main()
  A.Operate = 1
End Sub
oKeypad A = New oKeypad;

Void Main(Void){
  A.Operate = 1;
}
Basic Syntax 
A As oKeypad(cvOn)
  ' At this point in the program,
  ' K.Key will be the last button
  ' pressed and K.Received will be
  ' 1 when it is being pressed.
Back to top of pageConnections:
 As shown, the keypad connects directly to the OOPic's I/O Lines 8 - 15.

The column wires are connected in this way:

  • I/O line 8 connects to the column wire associated with buttons 0, 4, 8 & C.
  • I/O line 9 connects to the column wire associated with buttons 1, 5, 9 & D
  • I/O line 10 connects to the column wire associated with buttons 2, 6, A & E
  • I/O line 11 connects to the column wire associated with buttons 3, 7, B & F

The row wires are connected in this way:

  • I/O line 12 connects to the row wire associated with buttons 0, 1, 2 & 3
  • I/O line 13 connects to the row wire associated with buttons 4, 5, 6 & 7
  • I/O line 14 connects to the row wire associated with buttons 8, 9, A & B
  • I/O line 15 connects to the row wire associated with buttons C, D, E & F

Typically, the wires in the cable coming from the keypad are already in this order and therefore can be plugged directly into the OOPic.

Back to top of page Examples:
In the following example, the oKeypad's properties are displayed on LEDs
Visual Basic SyntaxC and Java Syntax
'This program reads a 4x4 oKeypad and
'outputs the value to I/O lines 8 - 15.

Dim A As New oKeypad
Dim D As New oDIO1
Dim C As New oDIO8

Sub Main()
  C.IOGroup = 3
  C.Direction = cvOutput
  D.IOLine = 7
  D.Direction = cvOutput
  A.Operate = 1
  Do
    C = A.Key
    D = A.Pressed
  Loop
End Sub
// This program reads a 4x4 oKeypad and
// outputs the value to I/O lines 8 - 15.

oKeypad A = New oKeypad;
oDIO1 D = New oDIO1;
oDIO8 C = New oDIO8;

Void Main(Void){
  C.IOGroup = 3;
  C.Direction = cvOutput;
  D.IOLine = 7;
  D.Direction = cvOutput;
  A.Operate = 1;
  Do{
    C = A.Key;
    D = A.Pressed;
  } While (1);
}
Basic Syntax 
'This program reads a 4x4 oKeypad and
'outputs the value to I/O lines 8 - 15.

A As oKeypad(cvOn)
D As oDIO1(7,cvOutput)
C As oDIO8(3,cvOutput)

Do
  C = A.Key
  D = A.Pressed
Loop
 
 In this picture, the 9 key is being pressed. Notice that the binary number 9 is being displayed on the 8 I/O lines 24 - 31 indicating that the Key property has been set to 9 and that I/O line 1 is lit indicating that the Received property is set to 1. Also notice that the 8 I/O lines 8 - 15 are lit, showing that the keypad is being scanned.
 In this picture, the 9 key has been released. Notice that the binary number 9 is still being displayed on the I/O lines 24 - 31 indicating that the Key property remained at 9 and that I/O line 1 is not lit indicating that the Received property is now set to 0.
 In this picture, the F key was pressed and released. Notice that the binary number 15 is being displayed, indicating that the Key property was set to 15 and that I/O line 1 is not lit indicating that the button has been released.
Back to top of page Sources:
 B.G. Mirco sells 2 different styles of keypads that work with the oKeypad Object. They also sell the OOPic.

Experimenter's Keypad:
Part No.: ACS1048

This keypad has a 4 by 4 hexadecimal keypad area with an additional 9 buttons.  It has a peel and stick backing, making mounting easy.

 In addition, the keypad already has a flat cable and a connector that will fit on an 8-pin header connected to the OOPic with no modifications.
 Although this keypad has an additional 9 buttons, they can be trimmed off with an ordinary pair of scissors.  When trimmed, only the traces for the additional 9 buttons are cut leaving the 16 primary keys operating properly.
 Experimenter's Keypad:
Part No.: SWT1067

This keypad has a 4 by 4 keypad area arranged as a telephone pad with the letters A-D on the left side.

The keypad has mounting holes for an 8 pin header and are in the order expected by the OOPic.

NOTE: When using this keypad, use the oKeypadP Object.