Lab 6: Simon Says—16 Push-Button Matrix Keyboard Introduction

Transcription

Lab 6: Simon Says—16 Push-Button Matrix Keyboard Introduction
Lab 6. Simon Says
Lab 6: Simon Says—16 Push-Button
Matrix Keyboard
Introduction
Have you ever played the electronic version of Simon Says? A sequence
of varied color lights in a 4 x 4 grid flash on one at a time. The game is to
memorize the sequence and push switches in a 4 x 4 matrix in the same
pattern. Each time the sequence is mastered, the sequence becomes
longer.
Fig. 1 Simon Says Display
Purpose
This lab shows how to use the myDAQ 8-bit port to read 16 switches
arranged in a 4 x 4 matrix (Fig. 1). A LabVIEW program converts the
switch pressed into a byte containing the row and column address. This
byte is then converted into a 4-bit binary encoded nybble, which in turn is
converted into a single ‘LED’ indicator in the same position as the switch.
Equipment



NI myDAQ
4 x 4 Matrix Keypad
Solderless Breadboard
Prerequisite Reference Materials
NI ELVISmx Digital Reader (DigIn):
http://decibel.ni.com/content/docs/DOC-12944
© National Instruments Corporation
1
Lab 6. Simon Says
NI ELVISmx Digital Writer (DigOut):
http://decibel.ni.com/content/docs/DOC-12945
______________________________________________________________________
Exercise 6-1: Getting Started
The first step is to make an interface between the matrix switch and the
myDAQ 8-bit port. A 4 x 4 matrix switch places the switches into a square
arrangement with four lines to address the rows and four lines to address
the columns (Fig. 2).
Fig. 2 Schematic Diagram and Picture of a 4 x 4 Matrix Switch
When you press on a switch position, a row address line is connected to a
column address line. There is a unique address for each switch. Connect
the row lines to myDAQ bits (0-3). Connect the column lines to myDAQ
bits (4-7).
2
ni.com
Lab 6. Simon Says
From the NI ELVISmx Instrument Launcher strip, select DigOut, as shown
in Fig. 3.
Fig. 3 NI ELVISmx Digital Writer Instrument Panel
Select (0 - 3) in the [Lines To Write] box. Select (Manual) in the [Pattern]
box. The [Run] button will output the pattern set on Lines: (3,2,1,0) to the
lower nybble on the myDAQ port and hence activate the row address. HI
corresponds to +4 volts and LO corresponds to 0 volts.
From the NI ELVISmx Instrument Launcher strip, select DigIn, as shown in
Fig. 4.
Select (4 - 7) in the [Lines to Read] box. Pressing [Run] will read the
pattern on myDAQ port bits (7, 6, 5, and 4) and hence will read the column
address lines.
© National Instruments Corporation
3
Lab 6. Simon Says
Fig. 4 NI ELVISmx Digital Reader Instrument Panel
[Run] both instruments and explore the interaction between the writer bit
signals and the read bit signals. This is a simple method to determine the
row and column addresses of the matrix switch.
Note: Verifying the data sheet for the keypad is crucial, as different
manufacturers may use different pin assignments.
Exercise 6-2: Scanning a Keyboard
For our 4 x 4 matrix switch to function properly, one row address is output
at a time. After each row is activated, the digital reader gets the response
from the myDAQ port (column address lines):
If no switch on the activated line is pressed, the response nybble
would read (0000).
If one of the switches is pressed on the activated line, then the
response would read (0001), or (0010), or (0100), or (1000).
These codes correspond to switches 1, 2, 3, or 4.
4
ni.com
Lab 6. Simon Says
The next row activated corresponds to switches 5, 6, 7, or 8. The following
row corresponds to switches 9, A, B, or C. And the final row codes
correspond to switches D, *, #, or 0.
Note: Different matrix keyboards may have different characters.
The column value of a switch pressed is
{0001} = 1
{0010} = 2
{0100} = 3
{1000} = 4
then add 0 to the response number if row 1
then add 4 to the response number if row 2
then add 8 to the response number if row 3
then add 12 to the response number if row 4
Example:
If the row code is {0100} and the column code is (0100), then switch
11 has been pressed. In Fig. 2, switch 11 is key [B].
Note: If no switch has been pressed, the column code is (0000).
LabVIEW Scanning Program
Open the LabVIEW program entitled Scanning.vi.
Look at the block diagram in Fig. 5 (Windows/Show Block Diagram).
A row activation and column response in the sequence structure is
repeated over and over in the while…loop until a key is pressed. This
stops the while…loop and displays the 4-bit row address, the 4-bit column
address, and a [Key Pressed] flag on the front panel.
Fig. 5 LabVIEW Program to Scan the Matrix Switch for a Key Press and Deliver
the Row and Column Address in a Boolean Array
© National Instruments Corporation
5
Lab 6. Simon Says
How It Works
The scanning program is contained within a while…loop, which repeats
until a key is pressed. The first row code is entered into the sequence
structure. The first frame sends the row code via the DAQ Assistant
configured to write bits (0-3) to the port. In the next frame, a second DAQ
Assistant is configured to read the column states bits
(4-7) from the port. These bits are now tested for a key press.
Anytime a switch is pressed, a non-zero value for the column address is
read.
The value of the column nybble is tested with a LabVIEW compare (Not
Zero |#0>).
This generates a (True/False) flag, which signals a keypress:
If Key press = 1, then stop scanning and record the row and column
address.
If Key press = 0, then continue scanning.
When no key is pressed, the row code is rotated to the next row (Rotate)
and is used in the next iteration. This process repeats until a key is
pressed. Returned from the program is the row address, the column
address, and the key-pressed flag.
Encoder
The Data from the scanning program is passed to a LabVIEW program
entitled Encoder.vi, which converts the row and column address nybbles
into a single binary encoded nybble. Recall from Lab 5, a nybble has
sixteen possible combinations (0 to 15), just the number of switches on
the keypad.
Open and click [Run Continuously] in the LabVIEW program entitled
Encoder.vi, as shown in Fig. 6.
6
ni.com
Lab 6. Simon Says
Fig. 6 Encoder Front Panel Displays Conversion of a
Row and Column Address into 1 of 16 Numeric Key Codes
The row and column address lines are represented by two 4-bit arrays
containing Boolean switches. Click on the allowable row and column
addresses to see the resulting ‘Display Nybble’ and ‘Key Code’.
Example:
Row (0100), column (0010) is the key numbered 7, but the binary
code is 6.
Notes: The array index 0 is the least significant bit.
Within a LabVIEW program, binary counting starts at zero.
The keypad is labeled from (1-9, A, B, C, D, *, #, or O), but the
`’Key Code’ is (1-16).
Look at the block diagram of the Encoder.vi program, as shown in Fig. 7.
© National Instruments Corporation
7
Lab 6. Simon Says
Fig. 7 Encoder.vi Block Diagram
The program Encoder.vi takes the address nybbles from the scanning
program and delivers a 4-bit binary array to the decoder program.
Decoder
The purpose of the decoder program is to convert a 4-bit input array into a
selection of one and only one display. There are many ways to write this
program. Here we have chosen to emulate a 74154 digital IC, which is a 1
of 16 data distributor.
Open and [Run Continuously] the LabVIEW program entitled Decode.vi,
as shown in Fig. 8.
8
ni.com
Lab 6. Simon Says
Fig. 8 Conversion of a 4-bit Binary Array Value into 1 of 16 LED-type
Indicators
Operate the binary switches to verify that each 4-bit binary combination
yields a unique switch number (#).
At the lower left, there is a box containing eight indicators. These
represent the internal bus of the simulated 74154 chip, bits (0 - 3) and
complements (0 - 3).
Each indicator is decoded with a 4-bit AND function. This yields a true
output only when its inputs are (1111). With our eight bus lines, one can
uniquely select one and only one of the 16 combinations.
Example:
Input bits (0101) [#11] goes to the bus as bits (0101) and
complementary bits (0101).
Switch #11 is decoded with (b1 - b3) and (b0 - b2).
Stop the program and look at the block diagram.
Observe how the input array is converted into the four input lines
(b0 - b3) by the index array function. Four Boolean inverters are used to
generate the complement bus lines (b0 - b3). All eight lines are terminated
with a bus line indicators (B0-B3 and B0-B3). On the right side of the
© National Instruments Corporation
9
Lab 6. Simon Says
wiring diagram, there are sixteen four-input AND functions, each requiring
a unique set of binary inputs to light its indicator (#).
You can check out the emulation by searching the Web for
74154 IC.
To observe the 16 color display, you load and [Run] the program
Light Show.vi.
Exercise 6-3: Simon Says Program Version 1.0
Open and look at the block diagram of the LabVIEW program entitled
Simon.vi, as shown in Fig. 9.
Fig. 9 Block Diagram of Simon.vi
When a key is pressed, Scanning.vi passes the row and column address
to Encoder.vi, which generates a unique 4-bit binary for Decoder.vi. From
the decoder, one of 16 indicators is lit on the front panel.
Take Simon for a run and create some light shows.
LabVIEW Challenge: Build a complete Simon Says game. You now have
all of the major subVIs and a great front panel display.
10
ni.com
Lab 6. Simon Says
Here is the recipe:
Randomly pick a number from 0 to 15 and display it for three
seconds.
Repeat this operation, say, three more times.
Store each random number into an array.
Use Simon Says Version 1 as a basis to get four key presses
and record them into another array.
Compare the random numbers array with the guessed numbers
array.
If the arrays agree, then try another round with five random
numbers.
Have Fun!
© National Instruments Corporation
11