Activity 11. Build Function Generator Using Waveform Data

Your objective is to build a simple function generator that can generate a sine wave, a triangle wave, a square wave, and a sawtooth wave.

Front Panel

  1. Open a new front panel by selecting File»New VI, pressing <Ctrl-n>, or pressing the New VI button on the LabVIEW dialog box.
  2. Add the following items to the front panel:
    1. Digital Control located on the Controls»Numeric palette. Label it Amplitude.
    2. Digital Control located on the Controls»Numeric palette. Label it Frequency.
    3. Digital Control located on the Controls»Numeric palette. Label it Square Duty Cycle (%).
    4. Enum Control located on the Controls»Ring & Enum palette. Label it Signal Source.
    5. Digital Control located on the Controls»Numeric palette. Label it Phase.
    6. Boolean Push button located on the Controls»Boolean palette. Label it Reset Phase.
    7. Waveform Graph located on the Controls»Graph palette. Leave its label as Waveform Graph.
    8. Boolean Stop button located on the Controls»Boolean palette.
    9. (Optional) Use the Coloring tool to color the button red.
  3. Add elements to the Signal Source enum control.
    1. Click in the text box of the enum.
    2. Type Sine Wave and press <Shift-Enter> to add another element. Macintosh and UNIX Press <Shift-Return>.
    3. Type Triangle Wave <Shift-Enter>, Square Wave <Shift-Enter>, and Sawtooth Wave.
    4. Click the Enter button on the toolbar or click anywhere on the background of the front panel to enter the elements.
  4. Arrange the objects on the front panel to appear similar to the following illustration.


    The Signal Source control selects the type of waveform that you want to generate.


    The square duty cycle control is used only for setting the duty cycle of the square wave.

Block Diagram

  1. Open the block diagram by selecting Window»Show Diagram, pressing <Ctrl-e>, or clicking the block diagram window. Macintosh Press <Command-e>.
  2. Add a While Loop that fills up most of the visible area of the block diagram window. The While Loop causes the block diagram inside it, the subdiagram, to repeat until a TRUE or FALSE value is passed to the continuation terminal .
    1. Select the While Loop from the Functions»Structures palette and drag it to one of the corners of the block diagram. Do not release the mouse button.
    2. While still holding the mouse button, drag out a region to the opposite corner of the block diagram window.
    3. When the region fills most of the block diagram window, as indicated by the dotted rectangle, release the mouse button. LabVIEW creates the While Loop.
  3. Because the Stop button will control the While Loop, wire the Stop terminal to the continuation terminal.
    1. Move the Stop terminal next to the continuation terminal of the While Loop. As you drag the Boolean terminal close to the terminal, press the spacebar to activate automatic wiring. When LabVIEW draws a wire connecting the two terminals, release the mouse button. You also can use the Wiring tool to wire the two terminals together.
    2. Right-click the continuation terminal of the While Loop and select Stop if True from the shortcut menu. Notice the continuation terminal changes its appearance to the Stop if True state . When the user presses the Stop button, it passes a TRUE value to the continuation terminal, which stops the While Loop.
  4. Because it is the indicator, move the Waveform Graph terminal to the right side of the While Loop. Arrange all the other terminals, which are controls, to the left side of the While Loop.
  5. Between the controls and the Waveform Graph terminal, add a Case structure. The Case structure contains different subdiagrams, which only execute when the specific case is selected. The selector terminal of the Case structure determines which case is selected. The case selector label at the top of the Case structure indicates which case is currently visible.
    1. Select the Case structure from the Functions»Structures palette and drag it near the middle of the While Loop. Do not release the mouse button.
    2. While still holding the mouse button, drag out a region between the control terminals and the Waveform Graph terminal.
    3. When the region fills up most of the area between the control terminals and the Waveform Graph terminal, release the mouse button. LabVIEW creates the Case structure.
  6. Because the Signal Source Enum will control which waveform is displayed on the graph, wire the Signal Source terminal to the selector terminal of the Case structure.
    1. Move the Signal Source terminal just to the left of the selector terminal of the Case structure.
    2. Use the Wiring tool to wire the Signal Source terminal to the selector terminal of the Case structure. Notice the case selector label now shows the name of one of the elements you entered in the Signal Source enum control.
  7. By default, the Case structure has two cases. Because the Signal Source enum control contains four elements, you must add two more cases to the Case structure.
    1. Right-click the Case structure and select Add Case After from the shortcut menu. Notice the case selector label indicates the new case is the Square Wave case.
    2. Add another case as you did in step 7a. Notice the case selector label indicates the new case is the Sawtooth Wave case.
  8. Add the waveform generation VIs to their respective cases in the Case structure. Click the increment or decrement arrow on each side of the case selector label to switch between cases.
    1. Add the Sine Waveform VI located on the Functions»Analyze»Waveform Generation palette to the Sine Wave case.
    2. Add the Triangle Waveform VI located on the Functions»Analyze»Waveform Generation palette to the Triangle Wave case.
    3. Add the Square Waveform VI located on the Functions»Analyze»Waveform Generation palette to the Square Wave case.
    4. Add the Sawtooth Waveform VI located on the Functions»Analyze»Waveform Generation palette to the Sawtooth Wave case.
  9. Use the Wiring tool to wire the terminals from each waveform generation VI to the terminals outside the Case structure. Rearrange the position of the terminals as necessary to maintain an orderly block diagram. The first time you wire a terminal on the VI to a terminal outside the Case structure, a tunnel appears at the border of the Case structure. Thereafter, as you increment through the cases, the wire from the tunnel to the terminal outside the Case structure remains, so you only need wire from the tunnel to the terminal on the VI to complete the wire connection.
    1. Wire the reset phase terminal of each VI to the Reset Phase control terminal.
    2. Wire the frequency terminal of each VI to the Frequency control terminal.
    3. Wire the amplitude terminal of each VI to the Amplitude control terminal.
    4. Wire the phase terminal of each VI to the Phase control terminal.
    5. Wire the signal out terminal of each VI to the Waveform Graph terminal.
    6. Because it pertains only to the Square Wave VI, move the Square Duty Cycle (%) control terminal inside the Square Wave case of the Case structure. Wire the duty cycle (%) terminal of the Square Wave VI to the Square Duty Cycle (%) control terminal.
  10. The sampling info terminal of each VI allows you to control the sample rate and number of samples. Add a sampling info cluster to your VI.
    1. Use the Wiring tool to right-click the sampling info terminal and select Create Control from the shortcut menu.
    2. Use the Positioning tool to drag the sampling info terminal outside the Case structure. When you move the terminal outside the Case structure, the wire connection is broken.
    3. Select Edit»Remove Broken Wires or press <Ctrl-b> to remove the broken wire. Macintosh Press <Command-b>.
    4. Use the Wiring tool to wire the sampling info terminal outside the Case structure to the sampling info terminal on each VI as you did in step 9.
    5. (Optional) You might need to move the sampling info front panel control. Select Window»Show Panel, click the front panel window, or press <Ctrl-e> to display the front panel. Macintosh Press <Command-e>. Use the Positioning tool to move the sampling info control.
  11. Arrange the objects on the block diagram. Your block diagram should be similar to the following illustration. Each case is shown below the While Loop to illustrate the contents of each case. Do not create separate Case structures.

  12. Save the VI as function generator.vi in the labview\activity directory.

Running the VI

  1. Set the Amplitude to 1, frequency to 10, reset phase to ON, and signal source to Sine Wave, Fs (the sampling rate) to 1.00k, and #s (the number of samples) to 100. Because Fs is 1000 (1.00k) and the frequency is 10 Hz, every 100 samples corresponds to one cycle.
  2. Run the VI and observe the resulting plot.
  3. Change #s to 200, 300, and 400. How many cycles of the waveform do you see? Why?
  4. Reset the #s control to 100 and change frequency to 10.01 Hz. What happens? Why?
  5. Change reset phase to ON. Now what happens? Why?
  6. (Optional) Select different waveforms from the Signal Source enum and repeat steps 1 through 5.

End of Activity 11. Build a Function Generator