Introduction: Analog Discovery PWM and Digital IO Control Though LabVIEW
The goal of this project is to teach you how to control the Analog Discovery with LabVIEW. In particular this project will demonstrate how to control the Analog Discovery's digital IO and PWM pins. This project primarily focuses on the PWM generation. In the project there is a 2d color pallet. Depending on where you click a RGB color value is selected. This RGB value is converted to a PWM duty cycle and used to drive a tricolor LED. Three other LEDS that emit only Red, Green and Blue light are also driven by PWM signals. They are there to visualize amount of Red Green and Blue that are present in selected color. Whenever the mouse is clicked within the color map, the Analog Discovery’s digital pins are used to drive a Seven Segment Display. When driven the seven segment display reads “On”. To demonstrate that the color maps virtual RGB matches the actual LED colors, some virtual LEDs are animated on the front panel of the project. These LEDs are change color based on where you click in the color map. Above is a video of the project in action.
Step 1: Materials
Perquisites: You must have completed step 2 in the previous project. Following Option 2.1 (i.e Install LabVIEW) from a previous project is required.
- These are the only materials you really need to do the project.
- Analog Discovery
- Tricolor LED with a common cathode.
- These materials are only needed if you want some extra effects.
- Clear LED that produces red light.
- Clear LED that produces blue light.
- Clear LED that produces green light.
- Dual seven segment display
- WaveForms version 2.6.2 or later
- LabVIEW (versions 2013 and 2014 have been tested to work so far)
Step 2: Main GUI Code Overview
In GUI Main, the code is a simplified derivative of GUI Main from the previous LabForms Project. For neatness a lot of the Waveforms API calls have been grouped together into subVIs. In the new GUI Main there are two primary sections to focus on, the Device Selection Loop and the Run Device Functions Section.
Device Selection Loop
The Device Selection Loop has a couple primary functions. First it searches for compatible devices to connect to using the "Find Connected Devices.vi". A refresh button option has been added so you can connect a device after starting the GUI Main.vi. The "Find Connected Devices.vi" checks to make sure no other applications are already using the device you want to open. If the device is already in use, the front panel controls are configured so the "OK" button is grayed out. This prevents you from opening a device that is already in use. If the device isn't in use you can hit 'OK' to move on to the opening process. Once the opening process beings the Device Selection Loop terminates, and the Run Device Functions section is initiated.
Run Device Functions
The first portion of this section opens the selected device. The process is rather self explanatory and does not need to be explained in detail. Refer to the numbered comments in the code for more info. Once the device is opened, any custom VI you designed will begin to run. In this project the "GUI Custom Waveform Gen.vi" is run. If you want to scope your custom signal output, you can click the Enable Scope button before selecting the device. This will open the standard oscilloscope window from LabForms.
Step 3: Wiring
Wiringfor the project is fairly straight forward. The only required wiring for on the bread board is for the Tricolor LED. If you want to know how to wire a Tricolor LED go here. The seven segment display is an optional feature. Because of this and the fact the wiring for Seven Segment displays varies depending on the manufacture, its wiring will not be discussed.
Step 4: Preface
To properly understand how this project functions, it is important to understand the Waveforms SDK. Normally the analog discovery is controlled through the free software package Waveforms. WaveForms also comes with an open source development kit. This SDK gives you access to a C/C++ API library allowing anyone to write their own software interface for the Analog Discovery. An overview of all the functions available in the API can be found in theWaveForms Reference Manual.
At its core this LabVIEW project is simply calling the C/C++ API functions. Special VIs used to call each individual Waveforms API function in LabVIEW. You can see list of the available API functions by clicking the digilent.lvllib in the project window. Every VI in this list has a corresponding C/C++ API function. For more info about how each function works refer to the WaveForms Reference Manual, as illustrated in the image above. If you want to learn more about how to call C functions in LabVIEW check out the following links.
Step 5: Digital Output GUI Code
This VI that Handles the GUI animations as well as calling the functions to control the analog Discovery. This step will discuss the code in context of the Color Map Code, the Animation Code, the Analog Discovery Control Code.
NOTE: The project code is attached above.
Before beginning the loop the VI reads in pixel map of a JPEG. The JPEG is an image of a color map. The RGB values for each pixel in the pixel map are sorted into three arrays, one for each color. A 2nd copy of the pixel map is made and drawn on the front panel where it can be clicked. This clickable image is labeled "2D Color Map". Depending on where you click the 2D Color Map a RGB color value is selected. This is done by using the mouse pointer coordinates (relative to the 2D Color Map’s borders) to access the sorted color arrays that were created from the pixel map. These color values are used for animation purposes as well as controlling the Analog Discovery.
To give a virtual visualization of what the real Tricolor LED would look like, virtual LEDs are animated on the front panel. The RGB values grabbed from the sorted color arrays are applied to the LEDs drawn on the front panel. One of the LEDs on the front panel (the tricolor LED) shows the mixed RGB values. Each of the other three LEDs are dedicated to single color channel (Red, Blue, or Green). The LEDs on the front panel are made from decoration shapes. In LabVIEW all decoration references for a VIs front panel are contained within an array called Decos. Using a decorations reference you can control certain properties of the decoration such as color. After I had drawn the LEDs (using two rectangles, and one circle for each), I determined their position in the Decos array. I did this by plugging in an index value (obtaining the decorations reference) and hiding and unhiding the decoration. After seeing what decoration changed disappeared I knew what index in Decos corresponded to it. Using this process I derived the cluster constants of “LED Shape ID Numbers”. Note, that I call the number “Shape ID Numbers” but they are really index values of the Decos array. Beware if you add a decoration, or modify a decoration the index values in Decos change. This will cause the animation to malfunction. With the proper Shape ID Numbers I use a for loop to select a group of decorations, and apply the appropriate RGB colors. This is how the LEDs are animated on the front panel.
Analog Discovery Control
To control the Analog Discovery an event structure is used. The event structure triggers whenever the mouse is moved, or clicked within the 2D Color Map. The event structure also triggers if the PWM values or PWM change. The PWM values are calculated based on the RGB values. In a JPEG colors are 8 bit meaning the numbers 0 to 255 are used to represent how intense a color channel is (i.e how intese Red Green or Blue value of a pixel is). To calculate the PWM duty cycle percentage, the color values are divided by 255. These values are fed into the “Tricolor LED PWM Out.vi”This VI is primarily for organization. It contains 3 identical VIs that generate PWM signals on the Analog Discovery. Refer to the next step for details about how PWM is generated. The event structure that controls the PWM VI's also triggers a few Digital Write commands for the Analog Discovery. This is what causes optional seven segment display to light up. The pin number for the Digital Write is hard coded in the event structure. Feel free to change them and experiment, but be sure you do not try to use the same pin for PWM and Digital write. For details about how the Digital Write VI works refer to the next step.
Step 6: PWM and Digital Write Code
This step goes over how the PWM Write and Digital Write VIs work,
This VI is the core of the project. Inside the Analog Discovery a hardware counter is used that is used to generate a PWM signal (see page 65 of Waveforms SDK reference manual). The way this VI uses the counter, it configures it to count at 1 of two speeds. If the user wants to generate a PWM Signal between 100Khz and 10Khz the Analog Discovery Base clock of 100Mhz is used. If the user wants to generate a PWM Frequency between 10Khz and 50Hz a 1Mhz clock (generated by dividing the base clock by 100) is used to drive the counter. A slower counter clock is needed to generate slower PWM frequencies so the counter doesn’t exceed its maximum count value.
Knowing the counters frequency, desired PWM frequency and the Desired duty cycle, the number of High and low ticks can be calculated. The number of high ticks is the number the counter will count to, before switching the PWM signal from High to Low. The number of low ticks is the number the counter will count to, before switching the PWM signal from Low to High. By varying the ratio of high ticks to low ticks, and the frequency of the counter, a PWM signal can be generated a lot of frequencies. This VI in particular can generate PWM frequencies from 100Khz, to 10Khz with an Duty cycle resolution of 1%.
This VI is a modified version of the PWM Write VI. Based on
if the input is a 1 or 0, a PWM signal with a 100% or 0% duty cycle is generated. This means the counter inside the analog discovery will just hold the output High or Low until a new Value is plugged into the Digital Write VI.
yinghanlong made it!
We have a be nice policy.
Please be positive and constructive.