{ ****************************************************************************** Pascal Language Sample #3 : SAMPLE3.PAS This program will set up counter 2 to decrement from 101 hex sending out a pulse when reaching zero. The value of the counter will be read and displayed until zero is reached. Because the counter is extremely fast, the program will rarely catch it at zero, so execution will end soon after that point is reached. ****************************************************************************** } USES Crt; CONST Address : WORD = $350; { The Base address of the watchdog board. } Com_Adr : WORD = $2A8; { The Base Address of the UART chip on the board. } { ****************************************************************************** PROCEDURE set_counter Purpose: This procedure performs the setup operations on the 8253-5 counter/ timer chip. Counter two will be set for two byte load/read, mode one, binary. ****************************************************************************** } PROCEDURE set_counter; VAR control_word : BYTE; BEGIN control_word := $B0; { Set counter 2, Read/Load Least Sig Byte then Most Sig Byte, Mode 0, binary count } port[Address+3] := control_word; { Write control word to the counter control register. } port[Address+2] := $01; { Write 101 hex to counter 2 register. } delay(5); { Provide a brief pause between writes. } port[Address+2] := $01; { Write 101 hex to counter 2 register. } delay(25); END; { End set_counter } { ****************************************************************************** FUNCTION read_counter Purpose: This function reads the value of the counter. The value is read in two bytes, least significant byte first. The function will then combine the two and return that value. ****************************************************************************** } FUNCTION read_counter : WORD; VAR read_back, counter_value : WORD; BEGIN port[Address+3] := $80; { Counter Latching: Allows for the reading of both bytes without halting the counter.} read_back := port[Address+2]; { Read Least Significant Byte } counter_value := read_back; { Move low byte into storage } read_back := port[Address+2]; { Read Most Significant Byte } counter_value := counter_value + (read_back shl 8); { Shift and store the high byte } read_counter := counter_value; END; { End read_counter } 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 a, counter_value, last_value, home : WORD; read_back : BYTE; ch : CHAR; default, BA : string; c : integer; BEGIN counter_value := $101; { Set initial value to full scale so that loop will execute properly. } ClrScr; WriteLn(' SAMPLE 3.PAS : Use Of Counter/Timer Chip'); WriteLn; WriteLn('This program demonstrates how to program the 8253-5 counter/timer'); WriteLn('chip on the Watchdog Timer board. If the card has the optional relay,'); WriteLn('the chip will send a pulse that will trip the relay. This should cause'); WriteLn('two audible clicks, one for when the counter is enabled, and one for'); WriteLn('the pulse. There will be a delay of about five minutes between these'); WriteLn('occurences. The watchdog circuit can be disabled by installing a'); WriteLn('jumper at location marked WDOG. A keystroke will stop the program.'); GotoXY(1, 11); WriteLn('COM ADDRESS'); Com_Adr := AskForBaseAddress('2A8'); GotoXY(1, 16); WriteLn('WATCHDOG ADDRESS'); Address := AskForBaseAddress('350'); ClrScr; GotoXY(1,4); WriteLn('Board Configuration:'); WriteLn; WriteLn(' -- All remaining jumper settings are irrelevant.'); WriteLn; WriteLn; WriteLn('Press a key to continue'); ch := readkey; ClrScr; port[Com_Adr+4] := 4; { The output to the relay is controlled by a bit in the UART. Setting this bit opens the line to the relay and causes the first click. Send new value to the UART.} set_counter; { Set counter and start the countdown. } ClrScr; GotoXY(1,20); WriteLn('Press any key to quit...'); repeat last_value := counter_value; counter_value := read_counter; GotoXY(1,1); WriteLn('Value read from counter: ',counter_value); until (keypressed OR (counter_value > last_value)); if (keypressed) then ch := readkey; { Strip keystroke from buffer. } END.