Sei sulla pagina 1di 10

function daqfcngen_APF1(varargin) %% DAQFCNGEN Example function generator for the Data Acquisition Toolbox.

% % NOTE: This can only be run using the 32-bit version of MATLAB and % Data Acquisition Toolbox. To learn more about using data acquisition % devices on other platforms, see Session-Based Interface. % % DAQFCNGEN creates a function generator window which can be used with % the Data Acquisition Toolbox's analog output objects. % % The function generator window is divided into three sections. % % The top section contains a popup menu which displays the analog output % objects that currently exist in the data acquisition engine. The % selected analog output object's channels are listed in the listbox. % % The bottom section consists of a popup menu that has a list of the % supported waveforms. These waveforms include sin, sinc, square, % triangle, sawtooth, random, and chirp. Waveform-specific information % such as frequency or amplitude can be entered for each waveform. If % values are not entered, the default values displayed are used. % % The right section consists of three buttons which start/stop the % selected analog output object, reset the function generator window % to its original state and close the function generator window. % % The selected waveform can be sent to multiple channels of the same % analog output object. Channels of the same object cannot be connected % to different waveforms. % % Information on DAQFCNGEN and the Data Acquisition Toolbox are % available through the Help menu. % % The daqfcngen window can be closed either by selecting the File menu % and then selecting the Close Function Generator menu or by selecting % the "x" close button. When the daqfcngen window is closed, the analog % output object will be stopped, if it is running, and deleted. % % See also DAQSCOPE % 20 de Dezembro de 2012(Version Alberto P. Francisco) %% % Based on the number of input arguments call the appropriate % local function. switch nargin case 0 % Create figure. data = localInitFig; hFig = data.handle.figure; case 1 % Initialize variables.

action=varargin{1}; hFig=varargin{2}; data=get(hFig,'UserData'); % Call the appropriate function based on the action. switch action case 'waveform' data = localWaveForm(data); case 'start' data = localStart(data); case 'reset' data = localReset(data); case 'close' localClose(data); case 'sampleRate' data = localSampleRate(data); case 'Sine' data = localSine(data); case 'Square' data = localSquare(data); case 'Triangle' data = localTriangle(data); end case 2 action = varargin{3}; hFig = varargin{4}; data = get(hFig, 'UserData'); switch action case 'stopaction' data = localStop(data); end otherwise error('Wrong number of input arguments for FCNGEN'); end %*********************************************************************** % Create the figure window. function data = localInitFig %% % Initialize variables. waveforms = {'Sine';'Square';'Triangle'}; fgColor=get(0,'DefaultUIControlForegroundColor'); bgColor=get(0,'DefaultUIControlBackgroundColor'); %% % Information for all handle graphics objects.m genInfo.HandleVisibility='off'; genInfo.Interruptible='off'; genInfo.BusyAction='queue'; %% % Position the GUI in the middle of the screen screenUnits=get(0,'Units'); set(0,'Units','pixels'); screenSize=get(0,'ScreenSize');

set(0,'Units',screenUnits); figWidth=560; figHeight=178; figPos=[(screenSize(3)-figWidth)/2 (screenSize(4)-figHeight)/2 figWidth figHeight]; %% % Create the figure window. hFig=figure(genInfo,... 'Color' ,bgColor ,... 'IntegerHandle' ,'off' ,... 'DeleteFcn' ,'daqfcngen(''close'',gcbf)' ,... 'MenuBar' ,'none' ,... 'Name' ,'An Example Function Generator' ,... 'Tag' ,'Function Generator' ,... 'NumberTitle' ,'off' ,... 'Units' ,'pixels' ,... 'Position' ,figPos ,... 'Resize' ,'off' ,... 'UserData' ,[] ,... 'Colormap' ,[] ,... 'Pointer' ,'arrow' ,... 'Visible' ,'off' ... ); genInfo.Parent = hFig; %% % Information for all uicontrols. uiInfo=genInfo; uiInfo.BackGroundColor=bgColor; uiInfo.ForeGroundColor=fgColor; %% % Create the frames. framePos = {[8 100 443 70],[115 7 336 90],[457 7 92 163]}; for i = 1:3 uicontrol(uiInfo,... 'Style' ,'frame' ,... 'Units' ,'pixels' ,... 'Position' ,framePos{i}); end

...

%% % Get adaptor information: % adaptorStr = list of adaptors for the popup = {'winsound0' 'nidaq1'}. % ai = analoginput object constructed from adaptorStr{1}. % names = corresponding channel names to ai. [adaptorStr, ao, names] = daqgate('privateGetAdaptor', 'analogoutput'); %% % Create the analog output object popup. h(1) = uicontrol(uiInfo,... 'Style' ,'popupmenu' ,... 'Units' ,'pixels' ,... 'Position' ,[131 145 150 20] ,...

'BackGroundColor' 'String' 'Tag' 'Value' 'Callback'

,1-get(0,'DefaultUIControlForegroundColor'),... ,adaptorStr ,... ,'ao_objects' ,... ,1 ,... ,'daqfcngen(''adaptor'',gcbf)');

%% % Create the listbox for the analog output channels. h(2) =uicontrol(uiInfo,... 'Min' ,1 ,... 'Max' ,3 ,... 'BackGroundColor' ,1-get(0,'DefaultUIControlForegroundColor'),... 'String' ,names ,... 'Style' ,'listbox' ,... 'Units' ,'pixels' ,... 'Position' ,[286 113 150 53] ,... 'Tag' ,'ao_channels' ,... 'Callback' ,'daqfcngen(''changeChannel'',gcbf);'); %% % Create the popupmenu for the different waveforms. h(3) = uicontrol(uiInfo,... 'Style' ,'popupmenu' ,... 'BackGroundColor' ,1-get(0,'DefaultUIControlForegroundColor'),... 'Units' ,'pixels' ,... 'Position' ,[9 75 100 22] ,... 'String' ,waveforms ,... 'Tag' ,'waveforms' ,... 'Callback' ,'daqfcngen(''waveform'',gcbf);'); %% % Create the SampleRate display. sampleRate = get(ao, 'SampleRate'); h(4) = uicontrol(uiInfo,... 'Style', 'edit',... 'Units', 'pixels',... 'Enable', 'off',... 'Position', [131 110 50 20],... 'HorizontalAlignMent','left',... 'String', num2str(sampleRate),... 'Callback', 'daqfcngen(''sampleRate'', gcbf);'); h(5) = uicontrol(uiInfo,... 'Style', 'CheckBox',... 'String', 'SampleRate (Hz)',... 'Units', 'pixels',... 'Position', [18 110 100 20],... 'HorizontalAlignment', 'left',... 'Value', 0,... 'Callback', 'daqfcngen(''checkbox'', gcbf);'); %% % Create the label for selecting an analog output channel. h(6) = uicontrol(uiInfo,... 'Style' ,'text' ,... 'Units' ,'Pixels' ,...

'Position' ,[18 130 100 34] ,... 'Max' ,3 ,... 'HorizontalAlignment' ,'left' ,... 'String', 'Select an analog output channel:'); %%%%%%%%%% Aqui ser a entrada do Osciloscpio %%%%%%%%%%%%%%%% %% % Create the edit text boxes. ypos = [68 42 16]; initVal = [500 1 0]; for i = 1:3 hedit(i) = uicontrol(uiInfo,... 'Style' ,'edit' ,... 'BackGroundColor' ,1get(0,'DefaultUIControlForegroundColor'),... 'Callback' ,'daqfcngen(''resubmit'', gcbf);' ,... 'Enable' ,'on' ,... 'HorizontalAlignment' ,'left' ,... 'Max' ,1 ,... 'Min' ,1 ,... 'String' ,num2str(initVal(i)) ,... 'Visible' ,'off' ,... 'Units' ,'pixels' ,... 'Position' ,[124 ypos(i) 60 20]); %#ok<AGROW> end initVal = [0 0 0]; for i = 1:3 hedit(i+3) = uicontrol(uiInfo,... 'Style' ,'edit' ,... 'BackGroundColor' ,1get(0,'DefaultUIControlForegroundColor'),... 'Callback' ,'daqfcngen(''resubmit'', gcbf);' ,... 'Enable' ,'on' ,... 'HorizontalAlignment' ,'left' ,... 'Max' ,1 ,... 'Min' ,1 ,... 'String' ,num2str(initVal(i)) ,... 'Visible' ,'off' ,... 'Units' ,'pixels' ,... 'Position' ,[279 ypos(i) 60 20]); %#ok<AGROW> end %% % Create the static text boxes. textLabels = {'Frequency (Hz)';'Amplitude';'Methods'}; for i = 1:3 hText(i) = uicontrol(uiInfo,... 'Style' ,'text' ,... 'HorizontalAlignMent' ,'left' ,... 'String' ,textLabels{i} ,... 'Visible' ,'off' ,... 'Units' ,'pixels' ,... 'Position' ,[189 ypos(i) 80 20]); %#ok<AGROW> end

textLabels = {'DC Offset';'Phase (deg)';'Phase (deg)'}; for i = 1:3 hText(i+3) = uicontrol(uiInfo,... 'Style' ,'text' ,... 'HorizontalAlignMent' ,'left' ,... 'String' ,textLabels{i} ,... 'Visible' ,'off' ,... 'Units' ,'pixels' ,... 'Position' ,[344 ypos(i) 104 20]); %#ok<AGROW> end set(hText([1:2 4:5]), 'Visible', 'On'); set(hedit([1:2 4:5]), 'Visible', 'On'); %% % Create the axes. hAxes = axes(genInfo,... 'Units' 'Position' 'Box'

,'pixels' ,[9 15 100 45] ,'On');

,... ,...

%% % Plot a sine wave with amplitude one. t = 0:1/64:6*pi; hPlot = line('Parent', hAxes,... 'XData', t,... 'YData', sin(t),... 'Color', [0 0 1]); set(hAxes,... 'XLim', [0 6*pi],... 'XTick', [],... 'YLim', [-1.25 1.25],... 'YTick', []); %% % Create the pushbuttons. ButtonStr = {'Start','Reset','Close'}; ButtonPos = {125,95,65}; for i = 1:3 callbackStr = ['daqfcngen(''' lower(ButtonStr{i}) ''',gcbf);']; hpush(i) = uicontrol(uiInfo,... 'Style' ,'pushbutton' ,... 'Units' ,'pixels' ,... 'Position' ,[473 ButtonPos{i} 60 20] ,... 'String' ,ButtonStr{i} ,... 'Tag' ,lower(ButtonStr{i}) ,... 'Callback' ,callbackStr); %#ok<AGROW> end %% % Create the menubar. h1 = uimenu(genInfo,'Label', 'File','ForegroundColor',fgColor); h2 = uimenu(genInfo,'Label', 'Help','ForegroundColor',fgColor); h1(2) = uimenu(h1(1),... 'ForegroundColor',fgColor,...

'Label' ,'Close Function Generator' ,... 'Callback' ,'daqfcngen(''close'', gcbf);'); %#ok<NASGU> h2(2) = uimenu(h2(1),... 'ForegroundColor',fgColor,... 'Label' ,'Function generator Help' ,... 'Callback' ,'daqfcngen(''helpFcngen'', gcbf);'); h2(3) = uimenu(h2(1),... 'ForegroundColor',fgColor,... 'Label' ,'Data Acquisition Help' ,... 'Callback' ,'daqfcngen(''helpdaq'',gcbf);'); %#ok<NASGU> %% % Create the data structure. data.ao = ao; data.channel = 1; data.allchannel = []; data.CurrentWaveForm = 'Sine'; data.handle.figure = hFig; data.handle.axes = [hAxes hPlot]; data.handle.button = hpush; data.handle.edit = hedit; data.handle.text = hText;data.handle.config = h; data.SampleRate = sampleRate; data.State = 0; % (Waiting to) Start. data.winsound2 = 0; data.value.frequency = '500'; data.value.amplitude = '1'; data.value.offset = '0'; data.value.phase = '0'; data.value.cycle = '50'; data.value.width = '0.75'; data.value.seed = ''; data.value.method = 'Linear'; data.value.ttime = '1'; data.value.initfreq = '0'; data.value.ttimefreq = '100'; if ~isempty(data.ao) data.allchannel = daqhwinfo(data.ao, 'ChannelIDs'); end %% % Store the data matrix and display figure. set(hFig,'Visible','on','UserData',data); %% %% ************************************************************************* % Calculate the waveform. function out = localWaveFormCalc(wave, varargin) %% % Initialize variables. out = []; switch wave

case 'sawtooth' % Initialize variables. t = varargin{1}; width = varargin{2}; % Check the given width. if (width > 1) || (width < 0), error('WIDTH parameter must be between 0 and 1.'); end % Calculate the waveform. rt = rem(t,2*pi)*(1/2/pi); i1 = find( ((rt<=width)&(rt>=0)) | ((rt<width-1)&(rt<0)) ); i2 = 1:length(t(:)); i2(i1) = []; out = zeros(size(t)); out(i1) = ( ((t(i1)<0)&(rt(i1)~=0)) + rt(i1) - .5*width)*2; if (width ~= 0), out(i1) = out(i1)*(1/width); end out(i2) = ( -(t(i2)<0) - rt(i2) + 1 - .5*(1-width))*2; if (width ~= 1), out(i2) = out(i2)*(1/(1-width)); end case 'square' % Initialize variables. t = varargin{1}; duty = varargin{2}; if any(size(duty)~=1), error('Duty parameter must be a scalar.') end end %% *********************************************************************** % Displays a warning dialog if the parameter is empty. function localDispWaveFormEmptyWarning(name) tagValue = ['wave' name]; hWarn = findobj(findall(0), 'Tag', tagValue); if isempty(hWarn) hWarn = warndlg(['A waveform ' name ' must be specified.'],... 'Data Acquisition Warning'); set(hWarn, 'Tag', tagValue); else figure(hWarn(1)); end %% *********************************************************************** % Displays a warning dialog if the parameter is less than zero.

function localDispWaveFormLessWarning(name) tagValue = ['wave' name 'less']; hWarn = findobj(findall(0), 'Tag', tagValue); if isempty(hWarn) hWarn = warndlg(['The ' name ' must be greater than zero.'],... 'Data Acquisition Warning'); set(hWarn, 'Tag', tagValue); else figure(hWarn(1)); end %% *********************************************************************** % Displays a warning dialog if the parameter is outside of valid range. function localDispWaveFormRangeWarning(name, min, max) tagValue = ['wave' name 'range']; hWarn = findobj(findall(0), 'Tag', tagValue); if isempty(hWarn) hWarn = warndlg(['The ' name ' must be range between ' num2str(min) ' and ' num2str(max) '.'],... 'Data Acquisition Warning'); set(hWarn, 'Tag', tagValue); else figure(hWarn(1)); end %% *********************************************************************** % Displays a warning dialog if the parameter is not a number function bool = localValidityWarning(data) switch data.CurrentWaveForm case 'Chirp' startVal = 2; otherwise startVal = 1; end % switch for i = startVal:6 if ischar( sscanf((get(data.handle.edit(i), 'string')), '%d') ) if strcmp(get(data.handle.edit(4), 'string') ,'') ||... isempty(get(data.handle.edit(4), 'string')) bool = 1; return; end inputWarnMsg = sprintf('%s is not a valid input value. Please insert a valid number',... get(data.handle.edit(i), 'string')); hError = findobj('Tag', 'inputWarnMsg'); if isempty(hError) hError = errordlg(inputWarnMsg,'Validity error'); set(hError, 'Tag', 'inputWarnMsg'); bool = -1;

return; end % if end % if end % for bool = 1;

Potrebbero piacerti anche