unit MainUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls; type TMainForm = class(TForm) RangeCombo: TComboBox; Label1: TLabel; GoButton: TButton; ADPollTime: TTimer; CountTrack0: TTrackBar; VoltLabel0: TLabel; CalLabel: TLabel; CountTrack1: TTrackBar; VoltLabel1: TLabel; CountTrack2: TTrackBar; VoltLabel2: TLabel; CountTrack3: TTrackBar; VoltLabel3: TLabel; CountTrack4: TTrackBar; VoltLabel4: TLabel; CountTrack5: TTrackBar; VoltLabel5: TLabel; CountTrack6: TTrackBar; VoltLabel6: TLabel; CountTrack7: TTrackBar; VoltLabel7: TLabel; CountTrack8: TTrackBar; VoltLabel8: TLabel; CountTrack9: TTrackBar; VoltLabel9: TLabel; CountTrackA: TTrackBar; VoltLabelA: TLabel; CountTrackB: TTrackBar; VoltLabelB: TLabel; CountTrackC: TTrackBar; VoltLabelC: TLabel; CountTrackD: TTrackBar; VoltLabelD: TLabel; CountTrackE: TTrackBar; VoltLabelE: TLabel; CountTrackF: TTrackBar; VoltLabelF: TLabel; DiffCheck: TCheckBox; CalEdit: TComboBox; StopButton: TButton; procedure FormCreate(Sender: TObject); procedure RangeComboChange(Sender: TObject); procedure GoButtonClick(Sender: TObject); procedure ADPollTimeTimer(Sender: TObject); procedure StopButtonClick(Sender: TObject); protected { Private declarations } public { Public declarations } end; var MainForm: TMainForm; implementation {$R *.DFM} uses AIOUSB, DetectUnit; type TUSBAI1616Config = packed record ChannelRange: array[$0..$F] of Byte; CalibMode: Byte; TrigMode: Byte; StartStopCh: Byte; Oversample: Byte; StartStopChEx: Byte; //added for compatibility with the AI-MUX family end; var DeviceIndex: LongWord; Config: TUSBAI1616Config; VoltLabel: array[$0..$F] of TLabel; CountTrack: array[$0..$F] of TTrackBar; procedure TMainForm.FormCreate(Sender: TObject); var DetectForm: TDetectForm; Status, PID, ConfigBufSize: LongWord; begin DeviceIndex := diOnly; //If you only have the one board, QueryDeviceInfo() with diOnly will succeed //with the right PID. If you have more than one board, or haven't plugged it //in, it will fail, and this code will pop up the detector form so the user //can pick a board or cancel. Status := QueryDeviceInfo(DeviceIndex, @PID, nil, nil, nil, nil); if (Status <> ERROR_SUCCESS) or not (((PID >= $8040) and (PID <= $8044)) or ((PID >= $8140) and (PID <= $8144))) then begin DetectForm := TDetectForm.Create(Self); if DetectForm.ShowModal = mrOK then begin DeviceIndex := DetectForm.DeviceIndex; DetectForm.Free; end else begin DetectForm.Free; Application.Terminate; Exit; end; end; VoltLabel[$0] := VoltLabel0; VoltLabel[$1] := VoltLabel1; VoltLabel[$2] := VoltLabel2; VoltLabel[$3] := VoltLabel3; VoltLabel[$4] := VoltLabel4; VoltLabel[$5] := VoltLabel5; VoltLabel[$6] := VoltLabel6; VoltLabel[$7] := VoltLabel7; VoltLabel[$8] := VoltLabel8; VoltLabel[$9] := VoltLabel9; VoltLabel[$A] := VoltLabelA; VoltLabel[$B] := VoltLabelB; VoltLabel[$C] := VoltLabelC; VoltLabel[$D] := VoltLabelD; VoltLabel[$E] := VoltLabelE; VoltLabel[$F] := VoltLabelF; CountTrack[$0] := CountTrack0; CountTrack[$1] := CountTrack1; CountTrack[$2] := CountTrack2; CountTrack[$3] := CountTrack3; CountTrack[$4] := CountTrack4; CountTrack[$5] := CountTrack5; CountTrack[$6] := CountTrack6; CountTrack[$7] := CountTrack7; CountTrack[$8] := CountTrack8; CountTrack[$9] := CountTrack9; CountTrack[$A] := CountTrackA; CountTrack[$B] := CountTrackB; CountTrack[$C] := CountTrackC; CountTrack[$D] := CountTrackD; CountTrack[$E] := CountTrackE; CountTrack[$F] := CountTrackF; if ADC_QueryCal(DeviceIndex) <> ERROR_SUCCESS then begin //this board doesn't have calibration CalLabel.Visible := False; CalEdit.Visible := False; end; //Set the combo box to the first A/D range, then call RangeComboChange() to //set all the channels to that range. RangeCombo.ItemIndex := 0; RangeComboChange(nil); Config.CalibMode := 0; //Take actual data, not internal calibration sources. Config.TrigMode := $05; //Scan selected channels each counter rising edge. Config.Oversample := 0; //No oversample. ConfigBufSize := SizeOf(Config); ADC_SetConfig(DeviceIndex, @Config, ConfigBufSize); end; procedure TMainForm.RangeComboChange(Sender: TObject); var I: Integer; RangeCode: Byte; bNewVisible: Boolean; begin //The ranges in the combo box are listed in order, so that the index is equal //to the range code. RangeCode := RangeCombo.ItemIndex; if DiffCheck.Checked then RangeCode := RangeCode or $08; for I := $0 to $F do Config.ChannelRange[I] := RangeCode ; for I := $0 to $F do begin if (Config.ChannelRange[I and $F] and 1) = 0 then CountTrack[I].Min := 0 else CountTrack[I].Min := -1000 ; CountTrack[I].Max := 1000; if (Config.ChannelRange[I and $F] and 2) = 0 then begin CountTrack[I].Min := CountTrack[I].Min * 2; CountTrack[I].Max := CountTrack[I].Max * 2; end; if (Config.ChannelRange[I and $F] and 4) = 0 then begin CountTrack[I].Min := CountTrack[I].Min * 5; CountTrack[I].Max := CountTrack[I].Max * 5; end; CountTrack[I].Position := I; end; bNewVisible := not DiffCheck.Checked; for I := 8 to 15 do begin VoltLabel[I].Visible := bNewVisible; CountTrack[I].Visible := bNewVisible; end; end; procedure TMainForm.GoButtonClick(Sender: TObject); var Status: LongWord; ConfigBufSize: LongWord; begin if DiffCheck.Checked then Config.StartStopCH := $70 //Select all 8 differential channels, from 0 to 7. else Config.StartStopCH := $F0 //Select all 16 channels, from 0 to 15. ; ConfigBufSize := SizeOf(Config); ADC_SetConfig(DeviceIndex, @Config, ConfigBufSize); if CalEdit.Visible then begin Status := ADC_SetCal(DeviceIndex, PChar(CalEdit.Text)); if Status <> ERROR_SUCCESS then begin Application.MessageBox(PChar('Calibration Error ' + IntToStr(Status)), 'Cal Error', MB_ICONEXCLAMATION); Exit; end; end; CalEdit.Enabled := False; RangeCombo.Enabled := False; GoButton.Enabled := False; StopButton.Enabled := True; DiffCheck.Enabled := False; ADPollTime.Enabled := True; end; procedure TMainForm.ADPollTimeTimer(Sender: TObject); var Status: LongWord; Volts: array of Double; Channel: Integer; begin SetLength(Volts, 16); Status := ADC_GetScanV(DeviceIndex, @Volts[0]); if Status <> ERROR_SUCCESS then begin StopButton.Click; Application.MessageBox(PChar('Error ' + IntToStr(Status) + ' from ADC_GetScanV.'), 'ADC_GetScanV Error', MB_ICONEXCLAMATION); Exit; end; for Channel := 0 to 15 do begin CountTrack[Channel].Position := Round(Volts[Channel] * 1000); VoltLabel[Channel].Caption := Format('Ch %d: %.4f V', [ Channel, Volts[Channel] ]); end; end; procedure TMainForm.StopButtonClick(Sender: TObject); begin ADPollTime.Enabled := False; GoButton.Enabled := True; StopButton.Enabled := False; RangeCombo.Enabled := True; CalEdit.Enabled := True; DiffCheck.Enabled := True; end; end.