(***************************************************************************** * SAMPLE3.C : COUNTER/TIMER * * * * This program will set counter 0 to output 10,000 Hz, counter 1 * * to output 1000 Hz, and will set counter 2 to output 100 Hz. * * * *****************************************************************************) program sample3; uses crt; const DEFAULT_ADDRESS='340'; COUNTER_OFFSET =$0C; 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(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; Success := 0; 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; var base:integer; temp, control : word; BEGIN ClrScr; WriteLn('Sample 3'); WriteLn; WriteLn('This sample loads all three counters and continuously displays'); WriteLn('the values that are currently in them.'); WriteLn; base:= AskForBaseAddress( DEFAULT_ADDRESS); { Turn off interrupt request generation and set the A/DC trigger to software only.} temp:=port[base]; { read the status reg., clr IRQ} { set to software start conversion, disabled irqs, read of base+3 to start conversions, enable internal clock, counter 1, and counter 2 } port[ base]:=$DF; CtrMode( base+COUNTER_OFFSET, 0, 3); CtrMode( base+COUNTER_OFFSET, 1, 3); CtrMode( base+COUNTER_OFFSET, 2, 3); CtrLoad( base+COUNTER_OFFSET, 0, 100); CtrLoad( base+COUNTER_OFFSET, 1, 1000); CtrLoad( base+COUNTER_OFFSET, 2, 10); clrscr; gotoxy(1, 15); writeln('Press a key to exit...'); while( not keypressed) do begin gotoxy( 1,4); writeln('decrementing between 100 and 0'); writeln('counter 0 = ', CtrRead( base+COUNTER_OFFSET, 0):7); gotoxy( 1,7); writeln('decrementing between 1000 and 0'); writeln('counter 1 = ', CtrRead( base+COUNTER_OFFSET, 1):7); gotoxy( 1,10); writeln('decrementing between 10 and 0'); writeln('counter 2 = ', CtrRead( base+COUNTER_OFFSET, 2):7); delay(250); end; GotoXY(1, 20); END.