image thumbnail

DAC Output using Data Acq Toolbox Demo

by

 

13 Oct 2006 (Updated )

Use Data Acq Toolbox along with DataTranslation USB module to produce waveforms

OutputChans(varargin)
%By:  Rene Messier
%Co:  Data Translation
%Date: 10/3/06
%Program demonstrates how to continually output a signal on a DAC.  This
%application expects the clock frequency to be at least 10 times the
%wave frequency.

function varargout = OutputChans(varargin)
% OutputChans M-file for OutputChans.fig
%      OutputChans, by itself, creates a new OutputChans or raises the existing
%      singleton*.
%
%      H = OutputChans returns the handle to a new OutputChans or the handle to
%      the existing singleton*.
%
%      OutputChans('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in OutputChans.M with the given input arguments.
%
%      OutputChans('Property','Value',...) creates a new OutputChans or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before OutputChans_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to OutputChans_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help OutputChans

% Last Modified by GUIDE v2.5 12-Oct-2006 13:36:46

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @OutputChans_OpeningFcn, ...
                   'gui_OutputFcn',  @OutputChans_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});

end
% End initialization code - DO NOT EDIT

% --- Executes just before OutputChans is made visible.
function OutputChans_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to OutputChans (see VARARGIN)

% Choose default command line output for OutputChans
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

function dataMissHandle(obj, event, errorcount)
    ao0 = obj;
    disp('ERROR:  DataMissed error occurred');
    errorcount=errorcount+1;
    stop(ao0);
    set(handles.startbtn,'Enable', 'on');
    set(handles.stopbtn,'Enable', 'off');
    clear ao0;


function runTimeHandle(obj, event, errorcount)
    ao0 = obj;
    disp('ERROR:  Run Time error occurred');
    errorcount=errorcount+1;
    stop(ao0);
    set(handles.startbtn,'Enable', 'on');
    set(handles.stopbtn,'Enable', 'off');
    clear ao0;

function samplesReceivedHandle(obj, event, handles)
% function will not occur in this example
        
% --- Outputs from this function are returned to the command line.
function varargout = OutputChans_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on selection change in toolboxmenu.
function toolboxmenu_Callback(hObject, eventdata, handles)
% hObject    handle to toolboxmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns toolboxmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from toolboxmenu

global myoutput
global DAC
global popup_sel_type

popup_sel_index = get(handles.toolboxmenu, 'Value');
list = get(handles.toolboxmenu, 'String');
popup_sel_type = list{popup_sel_index};
   
try
    DAC = daqhwinfo(char(popup_sel_type));
    myoutput = ['AdapterName: ', DAC.AdaptorName,char(13)];
    set(handles.edit1,'String', myoutput);

    myoutput = [myoutput, 'AdapterDllName: ', DAC.AdaptorDllName, char(13)]; 
    set(handles.edit1,'String', myoutput);

    myoutput = [myoutput, 'AdapterDllVersion: ', DAC.AdaptorDllVersion, char(13)]; 
    set(handles.edit1,'String', myoutput); 

    boards = DAC.BoardNames;
    set(handles.boardmenu, 'String', boards);

    myoutput = [myoutput, 'Boards found: ', boards,char(13)]; 
    set(handles.edit1,'String', myoutput);
catch
    disp('ERROR: Can not find adaptor!');
    myoutput = 'ERROR: Can not find adaptor!';
    set(handles.edit1,'String', myoutput);
    return;
end

% --- Executes during object creation, after setting all properties.
function toolboxmenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to toolboxmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'dtol', 'nidaq', 'nidaqmx'});

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double

function plot1_CreateFcn(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function txtFreq_Callback(hObject, eventdata, handles)
% hObject    handle to txtFreq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtFreq as text
%        str2double(get(hObject,'String')) returns contents of txtFreq as a double


% --- Executes during object creation, after setting all properties.
function txtFreq_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtFreq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in boardmenu.
function boardmenu_Callback(hObject, eventdata, handles)
% hObject    handle to boardmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns boardmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from boardmenu

% set some globals we will need in other functions
global DAC
global myoutput
global ao0

board_index = get(handles.boardmenu, 'Value');
boardlist = get(handles.boardmenu, 'String');
board_type = boardlist{board_index};

try
    ao0 = analogoutput (char(DAC.AdaptorName), board_index-1);
    myoutput = [myoutput, 'Board selected: ', board_type];
    set(handles.edit1,'String', myoutput);
catch
    disp('ERROR: Cannot open DA subsystem!');
    myoutput = [myoutput, 'ERROR: Cannot open DA subsystem!'];
    set(handles.edit1,'String', myoutput);
    return;
end

% --- Executes during object creation, after setting all properties.
function boardmenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to boardmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over toolboxmenu.
function toolboxmenu_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to toolboxmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes on button press in startbtn.
function startbtn_Callback(hObject, eventdata, handles)
% hObject    handle to startbtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ao0
global errorcount
global myoutput
global chan_nums
global numchannels
global popup_sel_type

% set the sample rate
rate = str2double(get(handles.txtFreq, 'String'));
maxRate = daqhwinfo(ao0,'MaxSampleRate');
minRate = daqhwinfo(ao0,'MinSampleRate');
if (rate < minRate)
    myoutput = [myoutput, 'ERROR: Board minSampleRate is ',minRate];
    set(handles.edit1,'String', myoutput);
elseif (rate > maxRate)
    myoutput = [myoutput, 'ERROR: Board maxSampleRate is ',maxRate];
    set(handles.edit1,'String', myoutput);
end
try
    set(ao0, 'SampleRate', rate); 
catch
    disp('ERROR: SampleRate greater than max!');
    return;
end
    
%remove currently set txtChans
numchannels = length(ao0.Channel);
while (numchannels > 0)
    delete(ao0.channel(1))
    numchannels = numchannels - 1;
end

% set the new txtChans
channels = get(handles.txtChans, 'String');
chan_nums = str2num(channels);
addchannel(ao0, chan_nums)
numchannels = length(ao0.Channel);

%set Buffering ( first val = buff size, second val = number of buffs)
Buffsize = 10000; 
set(ao0, 'BufferingConfig', [Buffsize 10]); 
% start right away, no trigger
set(ao0, 'TriggerType', 'Immediate'); 
% continue to output until we call stop
set(ao0, 'RepeatOutput', inf);

%declare some other callbacks
set(ao0, 'StopFcn', {@daqcallback});
set(ao0, 'StartFcn', {@daqcallback});
set(ao0, 'RuntimeErrorFcn', {@runTimeHandle, errorcount});

set(handles.toolboxmenu,'Enable', 'off');
set(handles.boardmenu,'Enable', 'off');
set(handles.txtFreq,'Enable', 'off');
set(handles.txtWave,'Enable', 'off');
set(handles.lstWaveType,'Enable', 'off');
set(handles.txtAmp,'Enable', 'off');
set(handles.txtChans,'Enable', 'off');
set(handles.startbtn,'Enable', 'off');
set(handles.stopbtn,'Enable', 'on');

%create the data to output
Amp = str2double(get(handles.txtAmp, 'String'));
WFreq = str2double(get(handles.txtWave, 'String'));
wave_index = get(handles.lstWaveType, 'Value');
wavelist = get(handles.lstWaveType, 'String');
WaveForm = wavelist{wave_index};

%create our output signal, either sine, square, or triangle
[wave] = GenerateSignal (rate, WFreq, Buffsize, Amp, WaveForm);

%transpose the data
wave = wave';

%fill each column (each channel) with data
%we'll output the same data on all channels selected
wave = repmat(wave,1, numchannels);

putdata(ao0,wave)

myoutput = [myoutput, 'DA Started '];
set(handles.edit1,'String', myoutput);

%set plots
plot(handles.plot1,wave);              
set(handles.plot1, 'XGrid', 'on');
set(handles.plot1, 'YGrid', 'on');

%finally start the DA
start(ao0)

%declare a function that will build our sine, square and triangle.
%this function takes in the rate and buffsize and builds a near perfect
%cycle.  This eliminates glitching in the wave.
function [wave] = GenerateSignal (rate, WFreq, OutBufSize, PeakVoltage, WaveForm)
    global ao0;
    phase = 0;
    Freq = WFreq / rate;
    Ranges = daqhwinfo(ao0,'OutputRanges');
    Bits = daqhwinfo(ao0,'Bits');
    msbit = 2 ^ (Bits - 1);
    Res = (2 ^ Bits) - 1;
    wave = [1:OutBufSize];
    
    if strcmp(WaveForm, 'SINEWAVE')
        pi2 = 3.1415927 * 2;
        Freq = Freq * pi2;
        for i = 1:OutBufSize
            temp = cos(phase) * PeakVoltage; 
            wave(i) = temp;
            phase = phase + Freq;
            if (phase > pi2)
                phase = phase - pi2;
            end
        end
    elseif strcmp(WaveForm,'SQUAREWAVE')
        for (i = 1:OutBufSize)
            if (phase < 0) 
                temp = -PeakVoltage;  
            else
                temp = PeakVoltage; 
            end
            wave(i) = temp;
            phase = phase + Freq;
            if (phase > 0.5)
                phase = phase - 1;
            end
        end
    else       %TRIANGLE
        for (i = 1:OutBufSize)
            temp = 2 * phase * PeakVoltage;
            wave(i) = temp;
            phase = phase + 2 * Freq;
            if (phase > 0.5)
                phase = phase - 1;
                PeakVoltage = -PeakVoltage;
            end
        end
    end


% --- Executes on button press in stopbtn.
function stopbtn_Callback(hObject, eventdata, handles)
% hObject    handle to stopbtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ao0
global myoutput

%stop the AD subsystem immediatly
stop(ao0)

set(handles.toolboxmenu,'Enable', 'on');
set(handles.boardmenu,'Enable', 'on');
set(handles.txtFreq,'Enable', 'on');
set(handles.txtWave,'Enable', 'on');
set(handles.lstWaveType,'Enable', 'on');
set(handles.txtAmp,'Enable', 'on');
set(handles.txtChans,'Enable', 'on');
set(handles.startbtn,'Enable', 'on');
set(handles.stopbtn,'Enable', 'off');

myoutput = [myoutput, 'DA Stopped '];
set(handles.edit1,'String', myoutput);


function txtChans_Callback(hObject, eventdata, handles)
% hObject    handle to txtChans (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtChans as text
%        str2double(get(hObject,'String')) returns contents of txtChans as a double


% --- Executes during object creation, after setting all properties.
function txtChans_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtChans (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
%set(0,'defaults')
%colordef black;
%colormap('default')

function txtWave_Callback(hObject, eventdata, handles)
% hObject    handle to txtWave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtWave as text
%        str2double(get(hObject,'String')) returns contents of txtWave as a double


% --- Executes during object creation, after setting all properties.
function txtWave_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtWave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function txtAmp_Callback(hObject, eventdata, handles)
% hObject    handle to txtAmp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtAmp as text
%        str2double(get(hObject,'String')) returns contents of txtAmp as a double


% --- Executes during object creation, after setting all properties.
function txtAmp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtAmp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on selection change in lstWaveType.
function lstWaveType_Callback(hObject, eventdata, handles)
% hObject    handle to lstWaveType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns lstWaveType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from lstWaveType


% --- Executes during object creation, after setting all properties.
function lstWaveType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lstWaveType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
set(hObject, 'String', {'SINEWAVE', 'SQUAREWAVE', 'TRIANGLE'});



% --- Executes during object creation, after setting all properties.
function stopbtn_CreateFcn(hObject, eventdata, handles)
% hObject    handle to stopbtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called




% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over startbtn.
function startbtn_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to startbtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


Contact us