Uses Crt; var BUFFER : array[0..256] of word; Const BaseAddress : Word = $300; MAXCH : Word = 7; ERR_CHANNEL_RANGE : Word = 1; ERR_CHANNEL_ORDER : Word = 2; Procedure STARTCONVERSION(BASE : Word); Begin portw[BASE + 16]:= $0001; { Set Start/Stop bit high } End; Procedure RESET(BASE : Word); Begin portw[BASE + 16]:= $0002; { send RESET to chip } End; Procedure CALIBRATE(BASE : Word); Begin portw[BASE + 16]:= $0008; { calibrate } End; Procedure SETCHANNEL(BASE : Word; FIRSTCH : Word; LASTCH : Word); Var i:word; offset:word; test:word; Begin i := 0; offset := 0; test := 0; { portw[BASE + 0]:=0; portw[BASE + 2]:=$0004; portw[BASE + 4]:=$0008; portw[BASE + 6]:=$000C; portw[BASE + 8]:=$0010; portw[BASE + 10]:=$0014; portw[BASE + 12]:=$0018; portw[BASE + 14]:=$001D;}{loop} for i := FIRSTCH to LASTCH do begin test := $0000 or ( i shl 2); { set channel } offset := (i-FIRSTCH) * 2; { increment even base addresses } portw[BASE + offset]:= test; { send pointlist } end; portw[BASE + offset]:= test or $0001; { set loop bit on last instruction } End; function WAITFOREOC(base : word) : boolean; begin WAITFOREOC := ((portw[base+16] AND $0001) = $0001); End; { Call GETADDATA --- Parameter list 1 : Base address 2 : First channel 3 : Last channel 4 : Number of times to scan channels 5 : Buffer to store collected data } function GETADDATA(BASE : Word; FIRSTCH : Word; LASTCH : Word; SCANS : Word):word; var i:word; x:word; timeout:word; conv:word; begin i := 0; x := 0; if FIRSTCH > MAXCH then GETADDATA := ERR_CHANNEL_RANGE; { define constants } if LASTCH > MAXCH then GETADDATA := ERR_CHANNEL_RANGE; if FIRSTCH > LASTCH then GETADDATA := ERR_CHANNEL_ORDER; RESET(BASE); { reset RAM pointer } CALIBRATE(BASE); SETCHANNEL(BASE,FIRSTCH,LASTCH); { set channels in point list } RESET(BASE); { send RESET to chip } STARTCONVERSION(BASE); for x := 0 to (SCANS-1) do begin timeout := 65535; while (WAITFOREOC(BASE) AND (timeout > 0)) do dec(timeout); { while not done sampling} for i := 0 to LASTCH - FIRSTCH do BUFFER[x * (LASTCH - FIRSTCH + 1) + i] := portw[BASE + 24]; { read data into buffer} end; { end x for loop } GetADData:=0; End; Procedure ExitProgram(errorCode : Integer); Begin ClrScr; WriteLn('A fatal error occured, program halted!'); if errorCode = 1 then WriteLn('First and last channel must be less than 8'); if errorCode = 2 then WriteLn('First channel must be less than or equal to last channel'); End; function SetFirstChannel : Word; var Channel:Word; begin WriteLn('Enter the first channel to read, 0-7. '); WriteLn('>'); gotoxy(whereX+1,whereY-1); ReadLn(Channel); SetFirstChannel := Channel; end; function SetLastChannel : Word; var Channel:Word; begin WriteLn('Enter the last channel to read, 0-7. '); WriteLn('>'); gotoxy(whereX+1,whereY-1); ReadLn(Channel); SetLastChannel := Channel; end; function SetScans : Word; var Scans:Word; begin WriteLn('Enter the number of times to read the channels each time the driver is called. '); WriteLn('>'); gotoxy(whereX+1,whereY-1); ReadLn(Scans); SetScans := Scans; end; 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 } Var error, i, x : Word; LastCh,FirstCh,Scans : Word; Key : Char; inst : word; ch : word; Begin FirstCh := 0; LastCh := 7; Scans := 5; ClrScr; BaseAddress := AskForBaseAddress('300'); { Prompt for base addr } FirstCh := SetFirstChannel; LastCh := SetLastChannel; Scans := SetScans; ClrScr; WriteLn('Pascal Language Sample 1 for PAD128.'); WriteLn; WriteLn; WriteLn('This program will read data from selected channels on the PAD128.'); WriteLn; WriteLn('A/D Card setup:'); {WriteLn(' Card is at base address ', BaseAddress, ' Hex.');} WriteLn; WriteLn('First channel: ', FirstCh,' Last channel: ',LastCh); WriteLn('Press any key to continue... '); WriteLn; Key := ReadKey; ClrScr; WriteLn; WriteLn('Inst Number Channel Counts '); while not keypressed do (* loop until key pressed *) begin error := GetADData(BaseAddress,FirstCh,LastCh,Scans); if (error <> 0) Then Begin ExitProgram(error); Halt(error); End; For x := 0 To (Scans-1) Do begin WriteLn; For i := 0 To (LastCh - FirstCh) Do{ display data for number of channels } begin { NOT DONE--Convert counts to volts} { inst number for debug, remove later } inst := BUFFER[x * (LASTCH-FIRSTCH+1) + i] shr 13; ch := BUFFER[x * (LASTCH-FIRSTCH+1) + i] and $0fff; WriteLn(' ',inst,' ', FirstCh+i, ' ',ch); { WriteLn(' ', FirstCh+i, ' ', ((BUFFER[i] * (10.0 / 4096.0)) - 5.0):6:3); { display in volts } { fix equation } {WriteLn;} end;{ end printing loop } WriteLn; WriteLn('Press any key to exit...'); gotoxy(whereX,whereY-(4+LastCh-FirstCh));{test with any number of channels} delay(100); end; { end Scans loop } End; { end keypressed loop } Key := ReadKey; { grab keystroke } WriteLn('Exiting Sample 1 Program...'); End.