(***************************************************************************** * * * SAMPLE5.PAS : COUNTER/TIMER * * * * This is a demonstration program to be used with the A/DC card. * * * *****************************************************************************) program sample5; uses crt; const DEFAULT_ADDRESS='340'; COUNTER_OFFSET =$0C; (* returns the 'busy' bit, high = conversion in progress *) function CheckForBusy(base : word) : boolean; begin CheckForBusy := ((port[base+2] AND $80) = $80); end; procedure CtrMode(addr : word; cntr, mode:byte); var ctrl:byte; begin ctrl := (cntr shl 6) or $30 or (mode shl 1); port[addr+3] := ctrl; end; procedure CtrLoad(addr : word; c, val:word); begin port[addr+c] := lo(val); port[addr+c] := hi(val); end; function CtrRead(addr : word; c : integer) : word; var temp : word; begin port[addr+3] := c shl 6; temp := port[addr+c]; CtrRead := temp + (port[addr+c] shl 8); end; (**************************************************************************** * FUNCTION: AskForBaseAddress * ****************************************************************************) function AskForBaseAddress(OldOne : String) : Word; const Msg : string[4] = '0'; var NewOne, Success, Dummy, Error : Word; AddrInputPosX, AddrInputPosY : Word; begin if (OldOne = 'OLD') then OldOne := Msg; WriteLn('Please enter the Base Address (0000-FFFF) for your card (in hex)'); WriteLn('or press ENTER for ', OldOne, '. '); Write('>'); AddrInputPosX := WhereX; AddrInputPosY := WhereY; repeat GotoXY(AddrInputPosX, AddrInputPosY); ClrEol; Readln(Msg); Val('$' + Msg, NewOne, Error); if (error=0) then begin Success := 1; Dummy := NewOne; end else if (Msg = '') then begin GotoXY(AddrInputPosX, AddrInputPosY); WriteLn(OldOne); Msg := OldOne; Success := 1; Val('$' + Msg, Dummy, Error); end; until (Success = 1); AskForBaseAddress := Dummy; end; { end of AskForBaseAddress } (**************************************************************************** * * * FUNCTION: main program - global routine * * * * INPUT: None. * * * * CALLS: None. * * * * OUTPUT: None. * * * ****************************************************************************) var data:word; timeout:longint; chan:word; base:integer; ch:char; BEGIN chan:=0; ClrScr; WriteLn(' Pascal SAMPLE #5: SAMPLE5.PAS '); WriteLn(' '); WriteLn(' This sample will read data from all channels on the A/DC card using'); WriteLn(' timer/counter #2 to time and start conversions.'); WriteLn; WriteLn; base:= AskForBaseAddress( DEFAULT_ADDRESS); CtrMode( base+COUNTER_OFFSET, 0, 2); (* all counters in mode 2 *) CtrMode( base+COUNTER_OFFSET, 1, 2); CtrMode( base+COUNTER_OFFSET, 2, 2); (* counter zero without count, it won't increment, cause we don't need it.*) CtrLoad( base+COUNTER_OFFSET, 1, 255); CtrLoad( base+COUNTER_OFFSET, 2, 255); port[base+2]:=chan; (* setup channel and gain (gain code 0, gain of 1) *) port[base+0]:=$E2; (* allow counter to start conversions *) while not keypressed do begin timeout:=655354; (* wait for start of conversion *) while (NOT CheckForBusy(base) AND (timeout > 0)) do dec(timeout); (* wait for end of conversion *) while (CheckForBusy(base) AND (timeout > 0)) do dec(timeout); data:=(portw[base+6] SHR 4) AND $0FFF; (* read data from conversion *) gotoxy(10,10+chan); WriteLn('Channel: ',chan,' Data Read: ',data:4); if (timeout = 0) THEN BEGIN WriteLn('A/D Timeout Error'); WriteLn; END else ClrEol; INC(chan); chan := chan MOD 8; port[base+2]:=chan; (* setup next channel *) END; ch := readkey; gotoxy(1,20); END.