Lab 5: Digital IO Bits, Nybbles, and Bytes Introduction We live in a digital world where music, pictures, and control algorithms are all stored as digital bits. One form of communication between the real world and the digital world is done through digital IO ports. Fig.1 VU-Meter with Screen LED Indicators MyDAQ has one 3.3 V digital port consisting of eight digital bits that can be configured as inputs, outputs, or a combination of both. The port can talk to a single line (a bit), 4 bits in parallel (a nybble), or 8 bits in parallel (a byte). Feeling hungry? Four bits make a nybble and two nybbles make a byte. Purpose This lab shows how to use the mydaq port to talk to switches (inputs) or LEDs (outputs). The switches will be used to enter a secret number, which you will try to guess. This will lead us to the successive approximation technique, a common algorithm used in most analog-to-digital converter chips. Eight LEDs will be used to build a common VU-meter (Volume Unit meter) display, as in Fig 1. Equipment National Instruments Corporation 1
NI mydaq 1 Push Button Switch (SPST) 8 Switches in a DIP Package Resistors: 8 x 330 Ω and 9 x 1 kω LEDs: 2 Red, 2 Yellow, and 4 Green Solderless Breadboard Prerequisite Reference Materials Using the mydaq Digital Reader: http://zone.ni.com/devzone/cda/tut/p/id/11507 Using the mydaq Digital Writer: http://decibel.ni.com/content/docs/doc-12945 Output Light Using a Basic 5 mm LED: http://zone.ni.com/devzone/cda/epd/p/id/6410 Exercise 5-1: Getting Started The first step is to make a single input switch for the mydaq digital port. Connect a 1kΩresistor to the +5 V mydaq screw terminal. The output after the resistor is called a logic 1 or HI. The mechanical switch goes between the logic 1 and ground (DGND on the mydaq connector). The digital input will be taken at the node point (logic 1 and Switch). Connect a wire from this point to DIO(0) on the mydaq connector. Fig. 2 Connection Circuit for a Single Switch to the mydaq Port Note: the 1 kω resistor limits the current from the power supply and prevents damage that may occur if the power supply was accidentally shorted. From the NI ELVISmx Instrument Launcher strip, select DigIn. 2 ni.com
Fig. 3 NI ELVISmx Digital Reader Instrument Panel Verify the settings seen in Fig. 3 and press the button [Run]. With no switch pushed, bit 0 will be ON or HI, and the Numeric Value will read 1. Pressing the push button produces the opposite state, OFF or LO, and the numerical value will read 0. Now that we have verified our test circuit, it is time to build a complete 8-bit input port. 8-Bit Data Input Port You will need to replicate your test circuit for all the other bits (b1-b7). An easy way is to employ a 16-pin dual-in-line set of eight switches. You will also need eight 1 kω resistors, 10 wires, and a breadboard. Build the circuit as shown in Fig. 4. National Instruments Corporation 3
Fig. 4 Wiring Layout for Connecting Eight Switches to the mydaq Digital Port After checking all the wires, connect the bit lines (yellow and green) and power lines (red and black) to the mydaq black edge connector. [Run] the DigIn Virtual Instrument. For all bits OFF, the Numerical Value reads 0. For all bits ON, the Numerical Value reads FF. Nybble Bits A nybble consists of four lines (bits) connected in parallel, and each bit binary is weighted 2 i, where i = 0-3. Weights are 1, 2, 4, and 8. Example: Bits (0111) read as 7, and bits (1011) read as B. There are 2 4 or 16 possible bit combinations with numerical values running from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E to F Note: MyDAQ will display bit values in hexadecimal (x). 4 ni.com
A small x beside the numerical value on the mydaq display indicates that the port value is in hexadecimal notation. Try various switch combinations of the first four switch positions. Byte Bits A byte consists of eight lines (bits) connected in parallel with each bit binary weighted as 2 i,where i = 0 7. Weights are 1, 2, 4, 8, 16, 32, 64, and 128. Example: Bits (0000 0111) read as 07, and bits (1001 1011) read as 9B. There are 2 8 or 256 possible combinations from 00 to FF (hexadecimal). MyDAQ DigIn reads the 8-bit port values as two binary encoded nybbles called the LOW nybble and HI nybble. Each is represented by a single character: The numerical value is just 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F HI Nybble (Character Value) *16 + LO Nybble (Character Value) or Hex Decimal 0000 0000 0 0 0001 0001 11 17 1111 1111 FF 255 Any number between and including the numbers 0 to 255 can be represented by eight binary encoded bits. These can be represented by two hexadecimal characters or three numerical characters. What combination of switch positions would correspond to 123? Exercise 5-2: A Guessing Game Suppose you asked a friend to guess a number between 0 and 255, and you would attempt to guess it in as few tries as possible. If you are really National Instruments Corporation 5
lucky, you might guess it within a few tries. If you were really unlucky, it could take 256 tries. No fun at all! Now suppose you changed the rules and asked your friend to tell you if your guess was too high, too low, or right on. You would probably get his secret number within 10 to 15 tries. But there is a method that will always give you the correct number within eight or fewer tries. It is called the successive approximation technique and uses an inverse binary scheme guessing from the most significant bit to the least significant bit. Successive Approximation is central to most analog-to-digital converter ICs. Our next project demonstrates how SA works. Load the LabVIEW program called Guessing Game.vi. It uses the 8-bit I/O port connected to a bank of eight switches, as discussed above. Have a friend load a secret binary encoded number into the switches. Now, when ready, [Run] the program. Guesses are loaded into the [Guess #] control box. Click on the blue button to test your guess. Responses are shown as Red: Guess is greater than secret number Green: Guess is less than secret number Yellow: Guess is correct. Bingo! The number of tries is also shown. Fig. 5 LabVIEW Front Panel for Simple Guessing Game 6 ni.com
How Did They Do That? Take a look at the program s block diagram in Fig. 6. Fig. 6 LabVIEW Block Diagram for Simple Guessing Game The DAQAssistant has been used to read mydaq s 8-bit I/O port. It outputs an array of unsigned 8-bit integers. The first array element index (0) contains the measured bit values on the port. [Guess #] is compared with the port value using two compare VIs. One tests for greater than (>), while the other tests for equality (=). The output of [>] is an LED indicator (red {true} and green {not true}). The output of [=] is an LED indicator (yellow {true} and transparent {not true}). Both indicators have the same size and location; however, the [>] indicator is on top of the [=] one. When the correct guess is found, the top indicator (red or green) becomes invisible, and the underlying yellow indicator becomes visible. BINGO! Exercise 5-3: Successive Approximation (SA) Algorithm Successive Approximation creates a test value equal to the most significant bit in an 8-binary number (128). National Instruments Corporation 7
o If the test value is greater than the secret number, then this bit is set to 1 in an answer register. o If the test value is less than the secret number, then this bit is set to 0 in an answer register. The next test value becomes the answer register. Now add the next msb value (64) to the test value and repeat the above test. o If the test value is greater than the secret number, then this bit is set to 1 in an answer register. o If the test value is less than the secret number, then this bit is set to 0 in an answer register. The next test value becomes the answer register. Continue until you have tested all the remaining bits, b5-b0. When completed, you will have the secret number after exactly eight guesses. If you also test for equality, then you will get the answer in fewer tries if the secret number is some simple combination of 128, 64, 32, 16, 8, 4, or 2. Set up a secret number on the eight switches. Use the Successive Approximation technique with the Guessing Game.vi program to find out how many tries it takes to find the secret number. Example: Guess (128). If n > 128, then keep 128. Next guess is 128 + 64, If n < 128, then do not keep 128. Next guess is 64. Continue until you have completed eight guesses. Works every time! Exercise 5-4: MyDAQ Binary Outputs Binary bits can be output easily with the mydaq digital writer. From your NI ELVISmx Instrument Launcher strip, select DigOut. 8 ni.com
Fig. 7 NI ELVISmx Digital Writer Instrument Panel Set the Lines to Write to [0-7]. This mydaq virtual instrument is capable of outputting manually an 8-bit binary number with operations like Toggle, Rotate, or Shift (left or right) applied. In addition, common binary operations such as Ramp, Alternating 1/0 s, and Walking 1 s generate useful binary bit shows. To see these patterns in the real world, connect eight LEDs to the digital port lines (b0-b7). Each LED has a 333 Ω current-limiting resistor wired from the LED to +5 V. The anode (long leg) is on the side of the +5 V power supply. National Instruments Corporation 9
Take your LEDs for a test ride. Fig. 8 Eight Colored Leds in a VU-Meter Format Exercise 5-5: VU Meter A VU meter is a bar graph display consisting of a linear series of eight LEDs. The greater the number, the more LEDs are lit. Example: If V = 0, no LEDs are lit; if V = 1.0, all LEDs are lit. In this project, use eight LEDs (four green, two yellow, and two red) configured as green for the lower numbers, yellow for mid-range numbers, and red for the highest numbers. Load the LabVIEW program VU Meter.vi. The front panel is shown in Fig. 1. View the block diagram in Fig. 9. 10 ni.com
Fig. 9 VU Meter.vi BD Converts Audio Levels to a db-scaled LED Output The VU meter has a bar-like graphical display where all LEDs are lighted, up to and including the measured value. The VU meter uses a relative scale measured in decibels. For this example, the maximum signal is set to 1.0 V, which is suitable for use with audio signals. The reference voltage is 1.0 V. Hence, the maximum output is 0 decibels with all LEDs lit. Just like we embedded a digital reader inside a LabVIEW program to read external switches, you can use the NI ELVISmx Digital Writer to embed a digital writer into a program to write to external LEDs. Note: The front panel [Input] box is set to a logarithmic scale to better reflect audio input levels. Give it a try. In later labs, you can use this program and circuit as an external VU Meter. The VU meter then becomes an interesting real-world display. Any signal, such as audio level, temperature, or frequency range, can be expressed in a VU meter-type display. National Instruments Corporation 11