If you have a sound card installed, you can run the
following code, which collects one second of data.
ai = analoginput('winsound');
addchannel(ai,1);
set(ai,'SampleRate',11025)
set(ai,'SamplesPerTrigger',11025)
start(ai)
data = getdata(ai);plot(data)delete(ai)clear ai
To list all the toolbox functions and demos, type
help daq
To display the command line help for a function, type
daqhelp function_name
Toolbox Components
The Data Acquisition Toolbox™ components are
described below.
These components are shown below.
MATLAB
Data Acquisition Toolbox
M-file functions
Data acquisition engine
Hard ware driver ada ptors
Hardware driver
Hardware
®
Interactive commands and data
Properties, data, and events
Properties, data, and events
Disk file
Sensors
Actuators
ComponentPurpose
M-filesCreate device objects, acquire or
output data, configure property values, and evaluate your acquisition status and resources.
EngineStore device objects and their
property values, control the storage of acquired or queued data, and control the synchronization of events.
AdaptorsPass properties, data, and events
between the hardware and the engine.
Device Objects
Device objects allow you to access specific hardware
subsystems. The device objects supported by the toolbox
include analog input (AI), analog output (AO), and digital
I/O (DIO) objects.
AI
object
AI
subsystem
AO
object
AO
subsystem
DIO
object
DIO
subsystem
Toolbox deviceobjects
Hardware subsystems
1
The Data Acquisition Session
A complete data acquisition session consists of five steps:
1 Creating a device object
2 Adding channels or lines to the device object
3 Configuring property values to control the behavior of
your data acquisition application
4 Acquiring data (AI) or outputting data (AO)
5 Cleaning up
Adding Channels or Lines
Before you can use a device object, you must add at least
one channel or line to it. To add channels to a device
object, you must use the
example, to add two channels to
chans = addchannel(ai,1:2);
You can think of a device object as a channel or line
container, the added channels as a channel group, and
the added lines as a line group.
The relationship between an analog input object and the
channels it contains is shown below.
Analog Input Object
addchannel function. For
ai:
Creating a Device Object
To create a device object, you must call the appropriate
creation function (constructor). As shown below, creation
functions are named for the device object they create.
Subsystem TypeCreation Function
Analog input
Analog output
Digital I/O
ID is the hardware device identifier. This is an optional
argument for sound cards with an
name of the hardware driver adaptor. The supported
adaptors are shown below.
Hardware VendorAdaptor Name
Advantech
®
Measurement Computing™mcc
National Instruments
Parallel port
Microsoft® Windows®
sound cards
analoginput('adaptor',ID);
analogoutput('adaptor',ID);
digitalio('adaptor',ID);
ID of 0. adaptor is the
advantech
®
nidaq
parallel
winsound
Container
(device object)
Channel 1
Channel 2
Channel 3
.
.
.
Channel n
Channel group
For digital I/O objects, this diagram looks the same
except that lines replace channels.
Configuring Properties
You can control the behavior of your data acquisition
application by configuring properties. The rules
associated with configuring properties include
• Property names are not case sensitive.
• You can abbreviate property names.
•
set(ai) returns all settable properties for ai, while
set(ai.Channel(index)) returns all settable
properties for the specified channel.
•
get(ai) returns the current property values for ai,
while
get(ai.Channel(index)) returns the current
property values for the specified channel.
For example, to create the analog input object
sound card:
ai = analoginput('winsound');
ai for a
2
Property Types
Toolbox properties are divided into these two main types:
• Common properties that apply to every channel or line
contained by a device object
• Channel/line properties that you can configure for
individual channels or lines
Common and channel/line properties are divided into
these two types:
• Base properties that apply to all supported hardware
subsystems of a given type (AI, AO, DIO)
• Device-specific properties that apply to the specific
hardware you are using
set and get display the base properties followed by the
device-specific properties.
Issuing a Trigger
To log data to the engine or a disk file (AI), or to output
data from the engine (AO), a trigger must occur. The
trigger types supported for all hardware are given below.
Trigger TypeDescription
Immediate
ManualThe trigger occurs after you manually
Software
(AI only)
The trigger occurs just after you issue
start. This is the default trigger type.
issue the
trigger function.
The trigger occurs when a signal
satisfying the specified condition is
detected. You must specify a channel
as a trigger source.
Property Syntax
You can configure and return property values three
ways: the
get or set functions, dot notation, or named
indexing.
The
get and set syntax is similar to the Handle
Graphics®
out = get(ai,'SampleRate');
set(ai,'SampleRate',11025)
get and set syntax.
The dot notation has the following syntax:
out = ai.SampleRate;
ai.SampleRate = 11025;
Named indexing allows you to associate a descriptive
name with a channel or line. For example, to associate
the name
set(ai.Channel(1),'ChannelName','Chan1');
out = ai.Chan1.UnitsRange;
ai.Chan1.UnitsRange = [0 10];
Chan1 with the first channel contained by ai:
Acquiring or Outputting Data
To acquire (AI) or output (AO) data, you must
1 Start the device object.
2 Log or send data.
3 Stop the device object.
After the trigger occurs, the
property is automatically set to
Logging (AI) or Sending (AO)
On.
Stopping a Device Object
A device object stops when the requested data is acquired
(AI) or output (AO), a run-time error occurs, or you issue
the
stop function.
stop(ai)
Managing Data
Previewing Data
While an AI object is running, you can preview acquired
data with the
1000 samples for
out = peekdata(ai,1000);
peekdata
MATLAB® and does not extract data from the engine.
Extracting Data
At any time after data is acquired by an AI object, you can
extract it from the engine with the
example, to extract 1000 samples for
out = getdata(ai,1000);
getdata
when all requested samples are returned.
peekdata function. For example, to preview
ai:
returns execution control immediately to
getdata function. For
ai:
returns execution control to MATLAB only
Starting the Device Object
To start the device object, use the start function.
start(ai)
After the device object is started, the Running (AI) or
Sending (AO) property is automatically set to On.
Outputting Data
To output data, you must first queue it in the engine with
the
putdata function. For example, to queue 1000
samples for the analog output object
putdata(ao,[1:1000]')
Once data is queued, you can start the AO object.
3
ao:
Reading and Writing Digital Values
Transferring digital values to and from a DIO subsystem
is not clocked at a specific rate in the way that data is
sampled by an analog input subsystem. Instead, values
are either written directly to digital lines with
or read directly from digital lines with
getvalue.
putvalue,
Additionally, DIO objects do not store data in the engine.
Therefore, they do not require starting or triggering. For
example, to write the value 23 to eight DIO lines:
dio = digitalio('nidaq','Dev1');
addline(dio,0:7,'out');
data = 23;
putvalue(dio,data)
getvalue(dio)
Events and Callbacks
An event occurs at a particular time after a condition is
met. Unless an error occurs, all AI and AO data
acquisition sessions contain a start, trigger, and stop
event.
You can access event information with the
property:
EventLog
Deleting and Clearing Device Objects
The delete function removes the specified device object
from the engine but not from the MATLAB® workspace.
delete(ai)
ai
still exists in the MATLAB workspace, but is an
invalid object since it is no longer associated with
hardware. You should remove invalid device objects with
the
clear command.
clear ai
If you clear a valid device object, the object no longer
exists in the workspace, but does exist in the engine. You
can return device objects from the engine with the
daqfind function.
out = daqfind;
ai = out(1);
Saving and Loading Device Objects
You can save a device object to a MAT-file with the save
command.
You can retrieve information from an existing log file
using the
daqread function. To retrieve all logged data:
data = daqread('data.daq');
To retrieve only object and hardware information:
daqinfo = daqread('data.daq','info');
4
Getting Information and Help
You can obtain information or help about installed
hardware, driver adaptors, device objects, functions, or
properties using the functions shown below.
FunctionDescription
daqhelp
Display help for device objects,
constructors, adaptors, functions, and
properties.
daqhwinfoDisplay data acquisition hardware
information.
propinfoReturn property characteristics for
device objects, channels, or lines.
PDF and HTML versions of the Data Acquisition Toolbox™ User’s Guide are available through the Help
browser.
5
Functions
Toolbox functions and the device objects they are associated with are organized into the groups shown below. The
supported device objects include analog input (AI), analog output (AO), and digital I/O (DIO).
Creating Device ObjectsAIAODIO
analoginput
analogoutputCreate an analog output object.3
digitalioCreate a digital I/O object.3
Adding Channels and LinesAIAODIO
addchannel
addlineAdd hardware lines to a digital I/O object.3
Getting and Setting PropertiesAIAODIO
get
setConfigure or display device object properties.333
setverifyConfigure and return the specified property.333
Create an analog input object.3
Add hardware channels to an analog input or analog output object.33
Return device object properties.333
Executing the ObjectAIAODIO
start
stopStop a device object.333
triggerManually execute a trigger.33
waitWait for the device object to stop running.33
Working with DataAIAODIO
flushdata
getdataExtract data, time, and event information from the data acquisition engine.3
getsampleImmediately acquire one sample.3
getvalueRead values from lines.3
peekdataPreview most recent acquired data.3
putdataQueue data in the engine for eventual output.3
Start a device object.333
Remove data from the data acquisition engine.3
6
Working with DataAIAODIO
putsampleImmediately output one sample.3
putvalueWrite values to lines.3
Getting Information and Help AIAODIO
daqhelp
Display help for device objects, constructors, adaptors, functions, and
333
properties.
daqhwinfoDisplay data acquisition hardware information.333
propinfoReturn property characteristics for device objects, channels, or lines.333
General Purpose AIAODIO
binvec2dec
clearRemove device objects from the MATLAB
daqcallbackA callback function that displays event information for the specified event.333
daqfindReturn device objects, channels, or lines from the data acquisition engine
Convert binary vector to decimal value.3
®
workspace.333
333
to the MATLAB workspace.
daqmemAllocate or display memory resources.33
daqreadRead a Data Acquisition Toolbox™ (.daq) file.3
daqregisterRegister or unregister a hardware driver adaptor.333
daqresetRemove device objects and data acquisition DLLs from memory.333
dec2binvecConvert decimal value to binary vector.3
deleteRemove device objects, channels, or lines from the data acquisition engine.333
dispDisplay summary information for device objects, channels, or lines.333
ischannelCheck for channels.333
isdiolineCheck for lines.333
isvalidDetermine whether device objects, channels, or lines are valid.333
lengthReturn the length of a device object, channel group, or line group.333
loadLoad device objects, channels, or lines into the MATLAB workspace.333
makenamesGenerate a list of descriptive channel or line names.333
obj2mfileConvert device objects, channels, or lines to MATLAB code.333
saveSave device objects to a MAT-file.333
showdaqeventsDisplay event log information.33
sizeReturn the size of a device object, channel group, or line group.333
7
Analog Input Base Properties
A
nalog input base properties are divided into two main categories: common properties and channel properties.
Common properties apply to every channel contained by the analog input object, while channel properties can be
configured for individual channels.
Common Properties
Analog Input Basic Setup Properties
SamplesPerTrigger
Specify the number of samples to acquire for each channel group member for each
trigger that occurs.
SampleRateSpecify the per-channel rate at which analog data is converted to digital data.
TriggerTypeSpecify the type of trigger to execute.
Analog Input Logging Properties
LogFileName
LoggingIndicate whether data is being logged to memory or to a disk file.
LoggingModeSpecify the destination for acquired data.
LogToDiskModeSpecify whether data, events, and hardware information are saved to one disk file or to
Specify the name of the disk file to which information is logged.
multiple disk files.
Analog Input Trigger Properties
InitialTriggerTime
Indicate the absolute time of the first trigger.
ManualTriggerHwOnSpecify that the hardware device starts when a manual trigger is issued.
TriggerFcnSpecify the M-file callback function to execute when a trigger occurs.
TriggerChannelSpecify the channel serving as a trigger source.
TriggerConditionSpecify the condition that must be satisfied before a trigger executes.
TriggerCondition
Value
TriggerDelaySpecify the delay value for data logging.
TriggerDelayUnitsSpecify the units in which trigger delay data is measured.
TriggerRepeatSpecify the number of additional times the trigger executes.
TriggersExecutedIndicate the number of triggers that execute.
TriggerTypeSpecify the type of trigger to execute.
Specify one or more voltage values that must be satisfied before a trigger executes.
8
Analog Input Status Properties
Logging
RunningIndicate whether the device object is running.
SamplesAcquiredIndicate the number of samples acquired per channel.
SamplesAvailableIndicate the number of samples available per channel in the engine.
Analog Input Hardware Configuration Properties
ChannelSkew
ChannelSkewModeSpecify how the channel skew is determined.
ClockSourceSpecify the clock used to govern the hardware conversion rate.
InputTypeSpecify the analog input hardware channel configuration.
SampleRateSpecify the per-channel rate at which analog data is converted to digital data.
Analog Input Callback Properties
Indicate whether data is being logged to memory or to a disk file.
Specify the time between consecutive scanned hardware channels.
DataMissedFcn
InputOverRangeFcnSpecify the M-file callback function to execute when acquired data exceeds the valid
Specify the M-file callback function to execute when data is missed.
hardware range.
RuntimeErrorFcnSpecify the M-file callback function to execute when a run-time error occurs.
SamplesAcquiredFcnSpecify the M-file callback function to execute every time a predefined number of
samples is acquired for each channel group member.
SamplesAcquired
FcnCount
StartFcnSpecify the M-file callback function to execute just before the device object starts
Specify the number of samples to acquire for each channel group member before a
samples acquired event is generated.
running.
StopFcnSpecify the M-file callback function to execute just after the device object stops running.
TimerFcnSpecify the M-file callback function to execute whenever a predefined period of time
passes.
TimerPeriodSpecify the period of time between timer events.
TriggerFcnSpecify the M-file callback function to execute when a trigger occurs.
9
Analog Input General Purpose Properties
BufferingConfig
BufferingModeSpecify how memory is allocated.
ChannelContain hardware channels added to the device object.
EventLogStore information for specific events.
NameSpecify a descriptive name for the device object.
TagSpecify a device object label.
TimeoutSpecify an additional waiting time to extract data.
TypeIndicate the device object type.
UserDataStore data that you want to associate with a device object.
Specify the per-channel allocated memory.
Channel Properties
Analog Input Channel Properties
ChannelName
HwChannelSpecify the hardware channel ID.
Specify a descriptive channel name.
IndexIndicate the MATLAB
InputRangeSpecify the range of the analog input subsystem.
NativeOffsetIndicate the offset to use when converting between the native data format and doubles.
NativeScalingIndicate the scaling to use when converting between the native data format and doubles.
ParentIndicate the parent (device object) of a channel.
SensorRangeSpecify the range of data you expect from your sensor.
TypeIndicate a channel.
UnitsSpecify the engineering units label.
UnitsRangeSpecify the range of data as engineering units.
®
index of a hardware channel.
10
Analog Output Base Properties
A
nalog output base properties are divided into two main categories: common properties and channel properties.
Common properties apply to every channel contained by the analog output object, while channel properties can be
configured for individual channels.
Common Properties
Analog Output Basic Setup Properties
SampleRate
TriggerTypeSpecify the type of trigger to execute.
Analog Output Trigger Properties
InitialTriggerTime
TriggerFcnSpecify the M-file callback function to execute when a trigger occurs.
TriggersExecutedIndicate the number of triggers that execute.
TriggerTypeSpecify the type of trigger to execute.
Analog Output Status Properties
Running
SamplesAvailableIndicate the number of samples available per channel in the engine.
SamplesOutputIndicate the number of samples output per channel from the engine.
Specify the per-channel rate at which digital data is converted to analog data.
Indicate the absolute time of the first trigger.
Indicate whether the device object is running.
SendingIndicate whether data is being sent to the hardware device.
Analog Output Hardware Configuration Properties
ClockSource
SampleRateSpecify the per-channel rate at which digital data is converted to analog data.
Analog Output Data Management Properties
MaxSamplesQueued
RepeatOutputSpecify the number of additional times queued data is output.
TimeoutSpecify an additional waiting time to queue data.
Specify the clock used to govern the hardware conversion rate.
Indicate the maximum number of samples that can be queued in the engine.
11
Analog Output Callback Properties
RuntimeErrorFcn
SamplesOutputFcnSpecify the M-file callback function to execute every time a predefined number of
Specify the M-file callback function to execute when a run-time error occurs.
samples is output for each channel group member.
SamplesOutput
FcnCount
StartFcnSpecify the M-file callback function to execute just before the device object starts
Specify the number of samples to output for each channel group member before a
samples output event is generated.
running.
StopFcnSpecify the M-file callback function to execute just after the device object stops running.
TimerFcnSpecify the M-file callback function to execute whenever a predefined period of time
passes.
TimerPeriodSpecify the period of time between timer events.
TriggerFcnSpecify the M-file callback function to execute when a trigger occurs.
Analog Output General Purpose Properties
BufferingConfig
BufferingModeSpecify how memory is allocated.
Specify the per-channel allocated memory.
ChannelContain hardware channels added to the device object.
EventLogStore information for specific events.
NameSpecify a descriptive name for the device object.
OutOfDataModeSpecify how the value held by the analog output subsystem is determined.
TagSpecify a device object label.
TypeIndicate the device object type.
UserDataStore data that you want to associate with a device object.
12
Channel Properties
Analog Output Channel Properties
ChannelName
DefaultChannel
Value
HwChannelSpecify the hardware channel ID.
IndexIndicate the MATLAB
NativeOffsetIndicate the offset to use when converting between the native data format and doubles.
NativeScalingIndicate the scaling to use when converting between the native data format and doubles.
OutputRangeSpecify the range of the analog output hardware subsystem.
ParentIndicate the parent (device object) of a channel.
TypeIndicate a channel.
UnitsSpecify the engineering units label.
UnitsRangeSpecify the range of data as engineering units.
Specify a descriptive channel name.
Specify the value held by the analog output subsystem.
®
index of a hardware channel.
13
Digital I/O Base Properties
Digital I/O base properties are divided into two main categories: common properties and line properties. Common
properties apply to every line contained by the digital I/O object, while line properties can be configured for individual
lines.
Common Properties
Digital I/O Common Properties
Line
NameSpecify a descriptive name for the device object.
RunningIndicate whether the device object is running.
TagSpecify a device object label.
TimerFcnSpecify the M-file callback function to execute whenever a predefined period of time
Contain hardware lines added to the device object.
passes.
TimerPeriodSpecify the period of time between timer events.
TypeIndicate the device object type.
UserDataStore data that you want to associate with a device object.
Line Properties
Digital I/O Line Properties
Direction
HwLineSpecify the hardware line ID.
IndexIndicate the MATLAB
Specify whether a line is used for input or output.
®
index of a hardware line.
LineNameSpecify a descriptive line name.
ParentIndicate the parent (device object) of a line.
PortSpecify the port ID.
TypeIndicate a line.
COPYRIGHT 1999 - 2008 by The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered
trademarks of their respective holders.
The MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information.
14
Loading...
+ hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.