{***************************************************************************** * SAMPLE2.PAS : TIMER DRIVEN INTERRUPTS * * * * This is a demonstration program to be used with the AD12-16 A/D * * board. The program will display sixteen channels using timer driven * * interrupts. The program demonstrates initializing the driver(task 0), * * setting the timers(task 17),setting the scan limits(task 1), performing * * interrupt driven conversions(task 5), checking for completion of a back * * ground task(task 8) and converting the readings returned by the board * * into channel and counts(task 9). * * * * The board should be set as follows: * * -- IRQ5 is used for interrupts * * -- Polarity set to bipolar * * -- DMA channel 1 * * -- On board clock set to 1 MHz * * * * LAST MODIFICATION: 2/3/98 * * * *****************************************************************************} program startup; {$L a16drv} {$F+} USES crt; type param_array = array[1..5] of integer; dat_array = array[1..200] of integer; pnt_array = array[1..200] of word; { all of the parameters passed to the A16DRV driver must be declared globally, including the buffers which have their offset passed inside the params array } var task,status : integer; params : param_array; ch : char; pntbuf :pnt_array; datbuf :dat_array; procedure a16drv(t_off:word;p_off:word;st_off:word);external; 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 } {*************************************************************************** * PROCEDURE: call_driver -- local routine * * * * PURPOSE: Performs the call to the driver package. * * * * INPUT: None. * * * * CALLS: a16drv - entry point to driver package. * * * * OUTPUT: None. * * * ***************************************************************************} procedure call_driver; var t_off,p_off,st_off : word; begin { this section extracts the offset of the parameters that we will pass to the assembly driver } t_off := ofs(task); p_off := ofs(params[1]); st_off := ofs(status); status := 0; a16drv(t_off,p_off,st_off); { this section checks for an error code } if status > 0 then begin WriteLn('A status error code of ',status,' was detected.'); WriteLn('Program terminated.'); end; end; { call_driver } {***************************************************************************** * FUNCTION: setup -- local routine * * * * PURPOSE: Sets up the driver package to read all 16 A/D channels. * * * * INPUT: None. * * * * CALLS: call_driver - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************} procedure setup(Address:word); var I :integer; begin { initialize the board } task := 0; params[1] := Address; { starting board address } params[2] := 5; { IRQ5 } params[3] := 1; { DMA channel 1 } call_driver; { call routine to call ext module } if status <> 0 then exit; { Set up the timers to divide the 1MHz clock by 1000 } task := 17; params[1] := 100; { set counter 2 to divide by 100 } params[2] := 10; { set counter 1 to divide by 10 } call_driver; { call routine to call ext module } if status <> 0 then exit; { if status > 0 then error } end; {procedure setup } {***************************************************************************** * FUNCTION: get_readings -- local routine * * * * PURPOSE: Reads the 16 A/D channels and displays them on the screen. * * * * INPUT: None. * * * * CALLS: call_driver - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * *****************************************************************************} procedure get_readings; var I :integer; begin { set the scan limits from channel 0 to 15 } task := 1; params[1] := 0; { lower scan limit } params[2] := 15; { upper scan limit } call_driver; { call routine to call ext module } { set up to do timer driven interrupts. The timer will start conversions at a 100Hz rate. The destination segment could be any unused area of memory. Make sure it is unused! } task := 5; params[1] := 16; { number of points to gather } params[2] := $7000; { destinstion register } params[3] := 1; { use timer for start ATD trigger } params[4] := 0; { one pass only } call_driver; { call routine to call ext module } if status <> 0 then exit; { if status > 0 then error } { repeatedly check status of background task until the driver indicates that it is done } ClrScr; task := 8; repeat call_driver; { call routine to call ext module } until (params[2] = 0); { we now can convert the values taken by task 5 into channel and counts and store in seperate arrays. This needs to be done because the board returns a conversion with the count in the upper 12 bits of a 16 bit integer and the channel number in the lower 4 bits. } { calculate the offset to the buffers so the driver can return the data. datbuf will contain the data read from the channels and pnt buffer will contain the channel info. } task := 9; params[1] := 16; { number of transfers } params[2] := $7000; { task 5 destination segment } params[3] := 0; { start with first value in segment } params[4] := ofs(datbuf[1]); { offset to data buffer } params[5] := ofs(pntbuf[1]); { offset to channel bufffer } call_driver; { call routine to call ext module } if status <> 0 then exit; { if status > 0 then error } WriteLn; WriteLn(' CHANNEL VALUE'); WriteLn(' ------- ------'); for I := 1 to 16 do WriteLn(pntbuf[I]:10,datbuf[I]:12); end; { get_readings } {************************************************************************** * PROCEDURE: main -- local routine * * * * PURPOSE: Controls program flow, detects when user is ready to exit. * * * * INPUT: None. * * * * CALLS: setup - set up program and drivers. * * get_readings - read the A/D channels and display. * * * * OUTPUT: None. * * * **************************************************************************} var Address:word; begin ClrScr; WriteLn(' SAMPLE2.PAS : Timer Driven Interrupts'); WriteLn; WriteLn(' This is a demonstration program to be used with the AD12-16 A/D'); WriteLn(' board. The program will display sixteen channels using timer driven'); WriteLn(' interrupts. The program demonstrates initializing the driver(task 0),'); WriteLn(' setting the timers(task 17),setting the scan limits(task 1), performing'); WriteLn(' interrupt driven conversions(task 5), checking for completion of a back'); WriteLn(' ground task(task 8) and converting the readings returned by the board'); WriteLn(' into channel and counts(task 9).'); WriteLn; Address := AskForBaseAddress('350'); ClrScr; WriteLn; WriteLn; WriteLn; WriteLn('Board Configuration:'); WriteLn; WriteLn(' -- IRQ5 is used for interrupts (required)'); WriteLn(' -- Polarity set to bipolar (required)'); WriteLn(' -- DMA channel 1 (required)'); WriteLn(' -- On board clock set to 1 MHz (required)'); WriteLn(' -- All remaining jumper settings are irrelevant.'); WriteLn; WriteLn; WriteLn; WriteLn('Press any key to continue, or press E to exit.'); ch := ReadKey; setup(Address); { set up program and the driver } while (status = 0) and (ch <> 'E') and (ch <> 'e') do begin { display current values for the 8 channels } get_readings; if status = 0 then begin { if status > 0 then error } { check for program exit } WriteLn; WriteLn('Press E to exit the program. Press any other key to rescan the data...'); ch:=ReadKey; { wait for key press } end; end; end. { main program }