%% USB-AIO Sample for MATLAB (unsupported / free) fprintf("MATLAB AIOUSB USB-AIO Sample\n----------------------------\n"); ACCESUSB = NET.addAssembly('c:\Windows\System32\AIOUSBNet.dll'); import AIOUSBNet.AIOUSB.* %use methodview(AIOUSBNet.AIOUSB) to see available functions/signatures AIOUSB_ReloadDeviceLinks(); fprintf("DIO SAMPLE\n----------\n"); % CONFIGURE Digital I/O Groups' directions numIOgroups = 2; % adjust for your board numDIOcontrolBytes = ceil(numIOgroups / 8); DIO_Control = NET.createArray( 'System.Byte', numDIOcontrolBytes); numDIObytes = 2; % adjust for your board DIO_Data = NET.createArray( 'System.Byte', numDIObytes ); DIO_Data(1) = 255; %set value to output on I/O Group 0 when it becomes an outputs DIO_Data(2) = 255; %set value to output on I/O Group 0 when it becomes an outputs DIO_Control(1) = bin2dec( '00'); % I/O Group 0, 1 set as INPUT status = DIO_Configure( -3, false, DIO_Control, DIO_Data ); if status ~= 0 fprintf("Error calling DIO_Configure(), status: %d\n", status); else fprintf("Successfully set both I/O Groups as INPUT\n"); end % READ Digital Data status = DIO_ReadAll(-3, 2, DIO_Data); % if status == 0 DIO_Data holds input bits from card if status ~= 0 fprintf("Error calling DIO_ReadAll(), status: %d\n", status); else data07 = dec2bin( DIO_Data(1) ); % bits 0-7 data815 = dec2bin( DIO_Data(2) ); % bits 8-15 fprintf('Read %08s from bits 0-7. Read %08s from bits 8-15\n', data07, data815); end % Configure as OUTPUT (both I/O Groups) DIO_Control(1) = bin2dec( '11'); % I/O Group 0, 1 set as output status = DIO_Configure( -3, false, DIO_Control, DIO_Data ); if status ~= 0 fprintf("Error calling DIO_Configure(), status: %d\n", status); else fprintf("Successfully set both I/O Groups as OUTPUT\n"); end % WRITE Digital Data DIO_Data(1) = 64+32+16+8+4+2+1; % turn bits 0-6 ON, bit 7 OFF DIO_Data(2) = 128+1; % turn bits 15&8 ON, bit 9-14 OFF status = DIO_WriteAll(-3, DIO_Data ); % write all bits in parallel if status ~= 0 fprintf("Error calling DIO_WriteAll(), status: %d\n", status); else fprintf("Wrote %08s to bits 0-7, and wrote %08s to bits 8-15\n", dec2bin(DIO_Data(1)), dec2bin(DIO_Data(2))); end % READBACK the Digital Outputs status = DIO_ReadAll(-3, 2, DIO_Data); % if status == 0 DIO_Data holds input bits from card if status ~= 0 fprintf("Error calling DIO_ReadAll(), status: %d\n", status); else data07 = dec2bin( DIO_Data(1) ); % bits 0-7 data815 = dec2bin( DIO_Data(2) ); % bits 8-15 fprintf('Read %08s from bits 0-7. Read %08s from bits 8-15\n', data07, data815); end % WRITE Digital Data (one byte) ByteIndex = 0; % 0 to (numDIOBytes-1) are valid; ACCES drivers use 0-based indices status = DIO_Write8(-3, ByteIndex, DIO_Data(ByteIndex+1) ); % write one byte only if status ~= 0 fprintf("Error calling DIO_Write8(), status: %d\n", status); else fprintf("Successfully wrote %08s to bits %d-%d\n", dec2bin(DIO_Data(ByteIndex+1)), ByteIndex*8, ByteIndex*8+7); end % INIT DAC status = DACSetBoardRange(-3, 1); % configure unipolar and enable; use "1" for bipolar ranges if status ~= 0 fprintf("Error calling DACSetBoardRange(), status: %d\n", status); else fprintf("Successfully configured and/or enabled the DAC\n"); end fprintf("DAC SAMPLE\n----------\n"); % ENABLE Whichever section below matches your DAC RANGE % WRITE DAC 0 ±10V DACIndex = 0; Vmin = -10; % bipolar 5V range has a minimum output voltage of "-5" Vspan = 20; % bipolar 5V range has a 10V span Vdesired = -9.99; DACcounts = floor((Vdesired - Vmin) * 65536 / Vspan); status = DACDirect(-3, DACIndex, DACcounts ); if status ~= 0 fprintf("Error calling DACDirect(), status: %d\n", status); else fprintf("Successfully wrote to DAC#%d %04x counts (%f Volts)\n", DACIndex, DACcounts, Vdesired); end % WRITE DAC 1 ±10V DACIndex = 1; Vdesired = 9.99; DACcounts = floor((Vdesired - Vmin) * 65536 / Vspan); status = DACDirect(-3, DACIndex, DACcounts ); if status ~= 0 fprintf("Error calling DACDirect(), status: %d\n", status); else fprintf("Successfully wrote to DAC#%d %04x counts (%f Volts)\n", DACIndex, DACcounts, Vdesired); end % WRITE DAC 0-5V (remember to pass "0" to DACSetBoardRange if you're using unipolar ranges) % DACIndex = 0 % Vmin = 0 % Vspan = 5 % Vdesired = 2.5 % DACcounts = (Vdesired - Vmin) * 65536 / Vspan % DACDirect(-3, DACIndex, DACcounts ) fprintf("\nADC Sample\n----------\n"); %ADC Configuration configArray = NET.createArray( 'System.Byte', 21); for configByte = 1:16 configArray(configByte)=1; %configure all 16 channels for ±10V input range, single-ended end configArray(17) = 0; %not calibration modes configArray(18) = 5; %counter-driven scan-start mode configArray(19) = hex2dec("F0"); %a scan is from channel 0 through channel F configArray(20) = 0; %no ADC Oversamples configArray(21) = 0; %no channel high-nybbles (only useful for >16 input models of USB-AIx or DPK-AIx) status = ADC_SetConfig(-3, configArray, 21); if status ~= 0 fprintf("Error calling ADC_SetConfig(), status: %d\n", status); else fprintf("Successfully configured the ADC via ADC_SetConfig()\n"); end ADCdata = NET.createArray( 'System.Double', 16); status = ADC_CSA_InitFastScanV(-3); if status ~= 0 fprintf("Error calling ADC_CSA_InitFastScanV(), status: %d\n", status); else fprintf("Successfully setup ADC_CSA_GetFastScanV\n"); end CTR_StartOutputFreq(-3, 0, 10000.0); status = ADC_CSA_GetFastScanV(-3, ADCdata); if status ~= 0 fprintf("Error calling ADC_CSA_GetFastScanV(), status: %d\n", status); else fprintf("Successfully scanned the ADC\n"); for i=1:16 fprintf(" CH%d=%f\n", i, ADCdata(i)); end end fprintf("----------\nSAMPLE END\n\n");