Beruflich Dokumente
Kultur Dokumente
n
Every programmer is sometimes confronted with the problem of implementing certain delays
into a prcgram, for example to move a point slowly on-screen. Often, wdummyn loops of the
e
following form are employed:
t.
5,
But such delay loops have a significant disadvantage: they rely on the processor speed. The
,I
delay time can therefore be very different on a 50 MHz i486 and on the ancestor 4.77 MHz 8083
!,
: (namely by a factor of 100). Old computer games in particular make use of such loops, and the
result is well known: while it is possible to xflys on a FC with a 4.77 MHz 8088 with a flight
sbnulator with no problems, the plane has crashed on a 50 MHz i486 before the pilot can operate
a single key.
But the problem of generating exactly defined time intervals also occurs in the system itself.
DOS, for example, provides the time and date for every file, and the control of electric motors
in floppy drives requires exactly defined signals. In bath cakes, time intervals defined by means
of program loops are not suitable. Thus, the PC’s designers have implemented one (PC/XT and
’ most ATs) or sometimes two (some new ATs or EISA) pmgmmmable interval timers (PITS).
.
1
_
.
8253/8254 Programmable Interval Timer 683 .
count registers CR, CQ. The 8254 additionally implements a status latch by means of which
(and the read-back command) the 8254 outputs status information. When the CPU writes count
values, the passed bytes are tmnsferred into the count registers CR, and CR, first. Afterwards,
the control logic transfers the two bytes simultaneously into the E-bit counting element.
8253 and 8254 coincide in their pin assignments and the meaning of the applied and output
signals. The only difference between them is that the 8254 implements an additional command
for reading the prczgramred status (read-back command). Pigwe 27.3 shows the pin assignment
far the 8253/8254.
, AO, Al (I)
Pins 19, 20
<The signals at these address pins indicate the number of the counter or the control register that
the CPU accesses for data reading or writing. A0 and Al are usually connected to the address
bus of the system. The possible combinations of (Al, AO) have the following meanings:
cs (I)
Pin 21
If this chip select Pin is on a low level, the CPU can read data from or write data to the internal
8233/8254 registers via the data bus D7-DO using the E and m signals. Thus the E signal
enables the PIT for read and write processes. For the counting operation and the signals sup-
plied by the PIT, a has no meaning.
D7-DO (I/O)
Pins 1-8
These eight connwtions form the bidirectional data bus tbmugh which the 8253/8254 receives
and pmvides data and instructions from or to the’CPU.
These pins are supplied with the gate signals for the counters 0, 1 and 2, respetively, to enable
the cwunters.
These connections supply the output signals of the counters 0, 1 and 2, respectively
wii (1)
Pill23
If the signal at this write pin is at a low level, the CPU writes data into the internal 8253/82%
registers via the data bus D7-W.
m (I)
Pin 22
If the signal at this read pin is at a low level, the CPU can read data from the internal 82531
8254 registers via the data bus D7-DO
,. VCC
Pin 24
l
. ‘*The
. supply voltage (usually +5 V) is applied to this pin.
GND
Pin 12
me 8253 control register is write-only; no data can be read. For the 8254 a new comma,,d is
uilable, the read-back command, with which certain control information can be determined.
vgramming one of the three 8253/8254 counters is begun by writing a control word via port
:3h (1st PITI or 04bh (2nd PIT). Figure 27.4 shows the control word format.
686 Chapter 27
When reading a counter you should not use the first option but transfer the current state of the
counting element (CE) into the output latches Ol, and OL, and latch there using the counter
latch or read-back command. Latch OL then holds the high-order byte, and latch Ol. the low-
order byte of the counting element. One or two successive. IN instructions for the port address
of the counter conceme.i then read these latches. If only the loworder (RW = Olb) or high-order
byte (RW = lob) was written when the counter was loaded with the initial counting value, then
read the current counting value of the initially written byte by a single IN inshuction. A suc-
ceeding IN instruction fetches the non-latched value of the low-order or high-order counter byte
at the time of the IN instruction and not the correspondixq semnd byte of the &bit counting
element. This is only possible if you have previously written the low-order as well as the high-
order counter byte. In this case you need to read the current counter value by means of two IN
inst~ctions. The PIT returns the Low-order byte of the %-bit counter with the first IN instnx-
tion, and then the high-order byte with the second IN instruction.
The processor doesn’t access the counting element (CE) directly, only the output latches. If the ,
cmtent of CE has been transferred once by a counter latch command into the output latches, ’
j! then this value is held there until the PU executes one or two IN instrwtions, or until the *
i corresponding wunter is repmgrammed. Successive counter latch commands are ignored if the
’ output latches haven’t been read before. The counting element, however, also continues to aunt
:_ after a counter latch command; the counter latch makes only a *snapshot* of the counting .
element CE. If you read the counter in the mode RW = CQb directly without the counter latch
!, command by means of two IN inshuctions, then the munter value may have changed already
when you issue the second IN instruction. Thus the high-&x byte of the counting element CE
t read second doesn’t fit the low-order byte read first. The determined values do not coincide
p with the actual values. A;an intermpt or a memory refresh may occur between two successive
1 IN instmctions, for example, the period between the execution of two IN instructions is not
predictable. Therefore, you should always issue a counter latch command or determine the
munter value by means of a read-back command. Figure 27.5 shows the format of the control
word for the counter latch command.
.
688 Chapter 27
To determine the current counter value you have to output a counter latch command for the
counter to read via ports 043h or 04bh. According to the programmed mode, you issue one or
two IN instructions for the counter concerned afterwards. But ensure that in all cases the coun-
ter has been pmgrmuned with one or two bytes before. The read Shit or X-bit value then
indicates the current counter value at the time of the counter latch command in the programmed
counting mode (binary or BCD). If the thus determined 8-bit value is the high-order counter
byte, then you need to multiply it (at least in your head) by 256 to get the weal. value that the
counting element CE in the PIT uses.
Example: rmLelli.m .al”a Of cour.ar 3‘ rha cc”aarez ha. iniri.llY bur 1- *itb la-or&r
and hLlh-onl0.r byte*.
lmv .1. lOOOOOOOb I load counter hrch v tar aoullter 3 into a1
OUT 4%. .I , arrpur. cc.unt~r l.Wh copynd to ccmrm1 redstAr
In 0. 4% , read lox-0rd.r SGumr.r wt. ir.to -1
TP ah. 4Ih , rua hi@h-ord.r -t.r byt. into ah
i thus u-ah. a1 ccmrain, the NIT*nf IS-hit co”rser value
The tlrst I” insrrucrion ?.r~*ac. ma laro*r tyte ot t_b counung el-r
vx,, *hi& i. h.M by the latch CL. into til. ha.t .iymi*car.L byte a1 of. the
.C-l.t.r~ *I fhe aecocd 1” In.truction l_ the hish-oder byte heId In tb
I.kCh cz, illto thm m2.t .ior.iEcar.r ac-lator bYt. ah. Thu., ax contain. the
16-bif CnunLer value after the wo In..
Unfortunately, there is no possibility of determinin g the initial value of a counter directly. This
would be useful, for example, for investigating the refresh rate of counter 1. The only option
is to read the counter concerned often, and to regard the maximum read value as the initial
value. Another disadvantage is that on the 8253 the pmgmmmed counting made cannot be
determined. However, the interpretation of the read counter value is very different depending
upon whether the wunter operates with binary or BCD numbers. Further, it cannot be deter-
mined whether the munter has been loaded with a lowader and a high-order byte or only
with one of them. This is necessary, though, to determine the counting rate of a PIT that is
programmed in an unknown way. Without knowing the counting mode you can only spxulate
when interpreting the read values.
The newer PIT model, the 82.54 (used first with the AT), implements the possibility of also
reading the counMs mode by means of a read-back command as a significant advance. Addi-
tionally, with the read-back command you can determine the current counter value. You issue
the read-back command via the control register (ports 43h or 04bh). Figure 27.6 shows the
format of this new 8254 command.
I_ The two nwst significant bits define the read-back command with their value Ilb (compare with
Figure 27.4). m indicates that the value and 3 that the counting mode of a counter is to be
determined. Note that m and $? are complementary signals, which issue the intended action
. if you set the bit concerned to 0, and not I as usual. The bits C&C2 define the counter whose
S <a& or mode is to be determined.
With the read-back command you can issue several counter latch commands in parallel by indicat-
ing several counters simultaneously with the bits CO-CZ. The 8254 then behaves as if you had
issued several counter latch commands (Figure 27.5) individually, andtransfers the individual
8253i8254 Programmable Interval Timer 689
..
is also possible to determine the value as well as the mode of a counter by meam of the mad-
ck command. If you only want to determine the value of a counter then set TT = 0 and 57 = I.
this case the read-back command is equivalent to one (if you set only one single G = I; x = 0,
2) or more counter latch commands (if you set several Cx= 1). For the number and interpre
ion of the IN instructions the same applies as for a normal counter latch m-ild.
however, you want to determine the programmed mode of a certain counter, then set 3 = o
d m = 1. Also, in this case, it is possible to select several countem simultaneously. YOU o”ly
ed to set several Cx to 1. The read-back command latches the current mode and supplis a
tus byte at the port address of the counter(s) concerned. You can fetch this status byte with
IN inst~ction. If you issue a new read-back command to de&e the mode without
ving read out the counter concerned in advance, then this second command is ignored. The
ches further contain the mode at the time of issuing the first command. Figure 27.7 shows the
tus byte format.
!e pin bit indicates the current status of the concerned counter’s OuTx pin. if pin = 1 then the
unter provides a high-level signal (+5 V), otherwise it supplies a low-level signal (0 V). The bit
D shows whether the last written counter value has already been transferred to the counting
690 Chapter 27
element CE by latches CR. and CR* Depending upon the programmed counting mode, this may
last some time. Not before zero = 0 is it meaningful to read back the wunter value. Before this
the PIT returns a value that further reflects the old state. The remaining six bits RW, mode and
BCD return the values with which they have been loaded for the counter concerned during the
last write of the control register (Figure 27.4). Thus you can determine, for example, whether
you need to read the low-order or high-order byte with a single IN instruction only, or whether
you must issue two IN instructions to get the current value of a counter.
Example: mrarmina aSr.Linp tic of cmlr&** 0.
Mov al, 1T100010b i load as-laror ritb reaa-hlck ma ior med.; E-1. Es0
OUT 43h. a1 / arrpur read-tack - to control reL.i*ter
IN al, (Oh , gef mc.a *ia sort Of counter 0
It is assumed that the status byte in the accumulator al has the value 00110100b. Thus the OUT
pin is on a 0 V level, the counter has been already loaded with the latest passed value, the low-
order and high-order bytes are used, the axmting mode is equal to 2, and the counting proceeds
in a binary fashion with 16 bits. This mode is used, for example, to issue the interrupt 08h that
updates the internal system clock.
In the read-back command you may also combine the detem?ination of counter mode and value.
set m as well as 3 to 0 in this case. All counters specified by means of C&C2 then return
information concerning the counting mode and the current count value. With the first IN in-
struction referring to a selwted counter you get the status byte; with the second or second and
third IN instructions, the PIT returns the low-order and/or high-order byte of the currently
latched counter value. All further IN instructions pass non-latched counter values as they are
taken from the counting element (CEl but no more status bytes. Ensure that all counters selwted
with bits C&C2 are read completely by means of two or three IN b&nxtions; otherwise.
further counter latch or read-back commands may be ignored.
If you output a counter latch command and later a &d-back command to determine the count-
ing mode without having read the counter value before, then the PIT first supplia the statuS
byte with the first IF instructions and only afterwards the byte(s) that indicate the county
value. Thus the order of passing latched bytes is always the sane. If you have read the counting
value in advance of the read-back command, then the IN instruction after the read-back corn-
mand of course returns the latched status byte.
You may write new counting values into one of the counters at any time, but you have to
observe the last programmed mode Oow-order and/or high-order counter byte, etc.). The new
values, however, become effective at different times in the various modes. If a counter has
reached the value 0, then it does not stop to count in the non-periodical modes 0, I, 4 and 5,
i
but continue with ffft% @CD = 0) or 9999 @CD = Il. The OUT pin, however, is not reset, and
the counting operation only orm~s internally without any external consequences.
In some modes the GATE signal executes the counting operation: if GATE = 1 then the 8253/
8254 continws countin& otherwise it keeps the currentValue without any change. In other
modes a low-high transition or a high level at the GATE input starts the counting opeation; the
GATE pulse acts as a trigger. Even if GATE then returns to a low level, the counter continues
to work. The entity N in the following description names the initial counter value.
a new count value is written into a counter it is loaded upon the next CLK pulse, and the
5 Counter continua the counting operation with the newly loaded value. Also, if GATE=0 the
$Z muter can be loaded, but the counting operation doesn’t start until GATE rises to a high
p level.
692 Chapter 27 8
Mode 0 is mostly used to issue a hardware interrupt after the elapse of a certain time period. P
The MI uses mode 2 for the periodic timer interrupt as mode is not periodic. k
tt
Mode 1 (Programmable Monotlopl c<
”
After writing the control register and the initial count value, the output OUT is on a high level
(+S V) for the moment. A trigger (transition low-high) at the GATE input loads the counter. A
Upon the next CLK pulse, OUT drops to a low level (0 W and remains at that level until the a
counter has reached the value 0. Then OUT rises to a high level. Not until one CLK pulse after
T
the next trigger does OUT fall again to a low level. Thus, in mode 1 the PIT generates a triggered
d
one-shot pulse with a duration of N CLK cycles and a low level.
b
If another trigger pulse appears during the course of‘s count operation (that is, while OUT is at h
a low level), the PIT reloads the counter with the initial value. Thus the OUT pin is at a low level b
N CLK cycles after the last trigger pulse. Unlike mode 0, the PIT can be triggered in this mode. d
si
If you write a new count value while the PIT is o+ing in mode 1, the new value is not
i!
effective for the current process. Not until the next irigger pulse at the GATE input is the new
a
count value transferred to the counter. The trigger pulse can occur, of course, when the PlT has
not yet completed the current counting operation. The single pulse with a low level lasts in this
case until the PIT has counted the new count value down to 0.
1. pin is also on a low level immediately raises OUT to a high level. A rise from a low to a high
level at the GATE input (bigger pulse) loads the counter with the initial count value and starts
the counting operation. Thus the PIT can be synchmnized by hardware in mode 2. After the
control nzgister and the initial count value have been written, the 8253/8254 lwds the counter
upon the next CLK pulse. Thus, the PIT can also be synchronized by software.
A new count value supplied during the course of an active counting operation doesn’t affect the
current process. At the end of the current half cycle the PIT loads the new value.
The length of the low and high states differs for odd and even values of N. As a CLK cyde
defines the smallest possible time resolution, no time periods with half the CLK cycle length can
be generated. With an odd N the OUT pin is initially at a high level. The PIT loads the value
N - 1 (that is, an even number) into the counter, and begins fo decrement this value (in steps)
by two. Once the CLK pulse after the counter has reached a value of 0, the potential at OUT
drops to a low level and the counter is reloaded with N - 1. Thfs value is decremented (again in
steps) by two. If the value of 0 is reached, then OUT rises to a high level and the whole pmcess
is repeated. Thus the signal at the OUT pin is at a high level.for (N + I)/2 CLK cycles and at
a low level for (N - 1)/2 CLK cycles, that is, the signal is low somewhat longer than it is high.
With even N values, N is loaded unaltered into the counter and dwemented in steps by two.
Jf the value 0 is reached, then OUT drops to a low level and the initial value N is reloaded
immediately. After the counter has counted downwards in steps of two, OUT r&s again to a
high level and the whole pmcw h repeated. Thus, even with N values, the phases with high
and low levels are equal in time. In both cases, the period of the squar~wave signal lasts N CLK
cycles. The initial value must be at least equal to 2.
The generated square-wave signal can be wed, for example, to transmit data via serial inter-
faces. The Pm then operatff as a baud rate generator. In the PC, counters 1 and 2 are operated
in mode 3 to drive the memory refresh and the speaker, respectively.
the counter upon the next CLK cycle, and the counting process starts. If the value 0 is reached,
OUT drops for a single CLK cycle to a low level, and immediately afterwards rises to a high
level again. Thus, OUT drops to a low level N + 1 CLK cycles after a trigger pulse at the GATE
input. If a trigger pulse occurs during a counting operation, the PIT reloads the initial value into
the counter and continues counting with the initial value. Thus in mode 5 the 8283/82!% can be
triggemd by hardware.
If you write a new count value during the course of an active counting operation then this OP”
ation is not affwted. Only after the next trigger pulse does the PIT load the new initial value.
In the following sections the application of the various counten in the PC are discussed.
Main Memory
-
The output OUT0 of counter 0 in the 825318254 PIT is connected to the‘lnput FRO (correspond-
ing to IRQOl of the first (or only) 8259A PIG. The counter 0 operates in mode 2 (rate generator)
void di..hYfir,roid,
t#tNcf aostiret tima,
ior ,i,)S
_do._g.ttir WYi~~‘ /war Lima *,
,. d*mplay Lil in the fO_f hourl linur., sacad* lwrh sr0r.d .,
_~*rbxtsa.*C*.m (1, 1,. prinrt~‘%i:\f:U:.i .,
rim.hour, LiEe.minute. Cba.*ecd, tiu.h~cond,‘
it c&hit,)> , ,* retvIm on keyboard hit *,
r.teh, ) i
re”r.3,
,
,
,
seer mu outme Of conwc.~ word OOllOlODb r*a pare ox,3 t.2 the cDnr?cd rwi.t.r i. rat
*.ol”r*ly n.c..sary *s the a*ad d. rued nor be altare& it is s.J**cient to mw.QlY as
hml-or&r d hi*bora.r eou?Aeer bytes via port 0x40.
Instead of mode 2, the PIT counter 0 may also be operated in mode 3, because in this case the
transition from low to high of the square-wave &al also issues a hardware interrupt via
the 8259A PIC. In this case, the control word WllOIMlb has to be changed to 001101lOb. As the
other 8253/8254 mcx& 0, I, 4 and 5 are not periodic, the internal clock stops if you operate
the PIT in one of these modes. Try it if you like. Later, after a boot process by means of Ctrl-
Ah-Del, the BIOS sets up the original mode again.
the
“ia
the
ate
WI-
PC
ied
:lt-
ess
:sh
ext
dSl
‘IT.
698 Chapter 27
You are really free to experiment; set various refresh values and check their effects on computer
performance using benchmark programs. The effect is not very significant, but you get some
feeling for the *buttons* to turn to in a computer so that the performance is enhanced. After all,
ten times 5% is 50%! But for permanent operation you should proceed carefully. What’s the gain
of 0.1% p&omunce enhancement if your PC crashes when you are editing an important text
or pmgram? And this danger is always present while doing such experiments.
A signal rise from low to high at the OUT0 pin generates a non-ma&able intermpt. This is
useful if all hardware interrupts are intentionally blocked, or because of a program error which
leads to an incorrect CL1 instruction or interrupt masking in the PICs, and the computer is
looping. Then the PC responds neither to a keyboard hit nor to another external request, except
d &dware resef - the computer hangs. Only the NM1 issued by the second PIT can *<free* the
CPU, hence the name failsafe timer. Especially for multitasking systems such as OS/2 or UNIX.
such a last resort is useful if, despite all the care taken and the use of protected mode, a program
error hangs up the computer and all other tasks are affected. While developing an operating
system this happens frrquently, and the system programmer will certainly be very thankful for
the presence of such a failsafe device.
8253/8254 Programmable Interval Timer 699
c
The NMI handler (interrupt 2) CM, of course, determine whether the source of the interrupt is
counter 0 of the second PIT or a parity error when data is read from a memory chip. In the latter . *-
case, a serious hardware malfunction has wanred. The PC mwt be shut down immediately, or
at least as quickly as possible to avoid extensive data loss or damage.