Beruflich Dokumente
Kultur Dokumente
2. On the Channels page, select your data acquisition device from the drop-down list.
Click the New button to add the channel object. Specify the channel string for the
waveform generation and configure the acquisition settings.
The update rate is the frequency at which your data is updated on individual
channels. For example, if you add channel 1 and channel 0 and set the update clock
to a frequency of 1000 updates per second, the DAQ device updates each DAC
every millisecond. This frequency is not the same as the waveform frequency of the
output. The actual frequency of the output waveform, the points per cycle, and the
update rate are interdependent. In this example, the update rate is a constant 10000
updates/second. The maximum frequency of the waveform is 1000 Hz, giving a
minimum of 10 points per cycle. Less than 10 points per cycle will produce an
inaccurate waveform.
a. On the Axes page, uncheck the Auto scale box and change the
Minimum and Maximum for the XAxis to 0 and 250,
respectively. The graph now displays 250 points at a time.
a. On the Numeric page, check the Do range checking box and set
the Minimum and Maximum to 0 and 1000, respectively. Range
checking enforces a minimum and maximum on the values in the
numeric edit box.
b. On the Format page, type .#" Hz" in the Format string box to
format and label the value in the numeric edit control. The .#
means the numeric edit displays one digit of precision to the right
of the decimal, and the " Hz" appends the unit after the value.
5. Place a Label on the form. Change Name to Update and delete the
default Caption value. This indicator displays the number of updates
that have been output to the data acquisition board since the start of
the waveform generation. Place another Label next to the control and
change Caption to Update Count:.
a. At the top of the code window, you can declare global variables
and constants. These variables and constants are accessible from
any part of the program. Place the following code in the General
Declarations section at the top of the code window:
Option Explicit
'BufferSize is the size of the buffer
Const BufferSize = 10000
Const TwoPi = 6.2832
Dim WholeBuffer
c. When you run the Waveform Generator and press the Start button,
data is generated. Before waveform generation can begin, the
device needs to be configured, and the AO buffer must already be
filled with data so that it is not empty at any point. Double click on
the Start button and add the following code:
d. The Progress event fires after 5000 updates on the DAC because
the Progress interval defaults to half of the buffer size, which you
declared as 10000. When the Progress event fires, the number of
updates appears on the user interface. Double click on the CWAO
control and add the following code:
e. When you press the Stop button, the data generation stops, and the
device resets. Double click on the Stop button and add the
following code:
9. To begin writing data to the channel, press the Start button. Notice
that a sine wave appears on the graph. Watch the update count change.
To change the frequency of the wave, press the Stop button, reset the
frequency, and restart the generation. You can use an oscilloscope to
verify the waveform generation. Alternatively, you can connect the
output channel to an input channel and use National Instruments
Measurement & Automation Explorer to monitor the input channel.
Press the Stop button when you want to stop the data generation and
reset the device.
Tip: If you choose to monitor your output, set the scan rate on the
analog input to a minimum of double the rate of the analog output to
avoid aliasing.
With the custom property pages, you can quickly select a device and set parameters to
output data to that device, but the property pages are not available during run time.
However, you can set the parameters programmatically and alter them from the user
interface while the example is running. In this section, you will add controls to the user
interface so that you can set the device and channel during run time.
Figure 3. Setting the Devices and Channels from the User Interface
1. Place the controls on the form as shown in Figure 3 to select the device
and channel from the user interface at run time.
Note: The properties set in the property pages are overwritten by those
set programmatically.
CWAO1.Device = numDevice
'Remove existing channels
CWAO1.Channels.RemoveAll
'Add channel specified on UI
CWAO1.Channels.Add numChannel
CWAO1.UpdateClock.Frequency = BufferSize
'Set a continuous acquisition
CWAO1.Infinite = True
'Specify the number of points in
'the waveform
CWAO1.NUpdates = BufferSize
'Configure the Progress event to fire
'every half buffer
CWAO1.ProgressEnabled = True
CWAO1.AutoSelectProgressInterval = False
'Progress interval is half of the
'full buffer size
CWAO1.ProgressInterval = BufferSize / 2
CWAO1.Configure
CWGraph1.ChartY WholeBuffer
End Sub
5. Enter the values for the device, channel, and frequency and press the
Start button. The plot of the output appears on the graph, and the Update
Count indicator displays the number of updates. Press the Stop button to
stop generating data.
Currently, you can set the frequency of the waveform during run time. Now, you will add
code so that you can change the frequency on the fly. When a Progress event is fired, it
indicates that the number of updates equal to the Progress interval has been written out
from the buffer. At this point, you can write new data to the buffer to replace the data that
has been written out. For this part of the example, you will generate new data when the
frequency is changed. This new data will then be written to the buffer on each Progress
event.
HalfBuffer = GeneratePoints(BufferSize / 2)
CWAO1.Device = numDevice
'Remove existing channels
CWAO1.Channels.RemoveAll
'Add channel specified on UI
CWAO1.Channels.Add numChannel
CWAO1.UpdateClock.Frequency = BufferSize
'Set a continuous acquisition
CWAO1.Infinite = True
'Specify the number of points in the waveform
CWAO1.NUpdates = BufferSize
CWAO1.Configure
CWGraph1.ChartY WholeBuffer
End Sub
Note: Notice that each time the frequency changes, half of a buffer is
generated. Before you can begin the acquisition, you need to create a full
buffer and half buffer of data. If you change the frequency of the
waveform before pressing the Start button, you will create HalfBuffer
twice.
7. Press the Start button to begin the data generation. Notice that Update
Count changes, as it did before. Change the frequency. Notice that the
waveform changes soon after.
You can expand this example by generating different waveforms. Figure 4 shows the
modified Waveform Generator during run time. The slide functions as a waveform
selector with settings to generate sine, square, or triangle waves.
a. On the Style page, choose Vertical Slide and click the Value
pairs only option. A value pair associates a symbolic name with a
value and marks a specific point on an axis, which you can specify
in the Value Pairs property page.
b. On the Value Pairs page, delete the last entry (Index 4). Choose
Index 1 and change Name to Triangle and Value to 2. For Index
2, change Name to Square and Value to 1. For Index 3, change
Name to Sine and leave Value at 0. Each of the values will be
used in the program to identify their respective functions in a case
statement.
c. On the Pointer page, choose None for the Fill style. Press OK.
2. Double click on the slide and add the following code so that when the
slide position changes, data is generated for the selected waveform
type:
Private Sub WaveformType_PointerValueChanged _
(ByVal Pointer As Long, Value As Variant)
HalfBuffer = GeneratePoints(BufferSize / 2)
End Sub
3. To implement the new waveform types, add the bolded code to the
existing GeneratePoints function:
Private Function GeneratePoints(Updates _
As Integer)
Dim i, PointsPerCycle
Dim j, Mid
Dim Pattern() As Double
ReDim Pattern(Updates)
PointsPerCycle = BufferSize / numFreqVal.Value
Mid = PointsPerCycle / 2
For i = 1 To Updates
j = i Mod PointsPerCycle
Select Case WaveformType.Value
Case 0
'Compute sine wave
Pattern(i) = Sin(TwoPi * i _
/ PointsPerCycle)
Case 1
'Compute square wave; the \
'represents integer division
Pattern(i) = 1 - 2 * (j \ Mid)
Case 2
'Compute triangle wave
Pattern(i) = 1 - 2 * (1 - _
Abs((Mid – j) / Mid))
End Select
Next i
GeneratePoints = Pattern
End Function
6. Enter values for the device, channel, and frequency and press the
Start button to begin. The plot of the output appears on the graph, and
the number of updates appears. Change the slide to output a different
waveform. You can see the different waveform appear on the graph.
Press the Stop button to stop generating data and reset the device.
Conclusion
The routines outlined in this application note demonstrate the basics of analog output
using the CWAO control. You can customize the routines and update the techniques in
this example to create a more efficient program that meets your specific needs.
Buffered AO provides a flexible way to continuously generate waveform data and change
the data without interrupting the data stream. You can customize your waveform
generator in the following ways: