Part2 Expr03
Part2 Expr03
Experiment
C O E 3 0 5 L A B M A N U A L
Generating Timing
Sequences
Objective
The aim of this lab experiment is to generate timing sequences using software delays
and programming 8253 Programmable Interval Timer (PIT) chip.
Equipment
Flight 8086 training board, the Application Board, PC with Flight86 software,
download cable
Tasks to be Performed
Generate time delays using software delays and 8253 PIT chip.
Use generated delays to turn ON/OFF LEDs for specific amounts of time
Interface a simple relay driver circuit to 8255 port and switch ON/OFF a
device for a specific amount of time.
37
C O E 3 0 5 L A B M A N U A L
3.1 Background
It is often necessary to control how long certain actions last. This can be achieved
using software delays, or more accurately by the use of a timer (i.e. 8253 PIT chip). In
this experiment, you will learn how to generate time delays using both software delays
and 8253 PIT chip. Also, you will learn how to use time delays to control the operation
of some devices (e.g. LEDs and Relays), and to generate periodical waveforms of
different frequencies.
Example 3.1: Calculate the total time taken by the following loop.
From the 8086 data sheets, we find that DEC CX requires 2 clock cycles and
JNZ requires 16 clock cycles. Thus, the total number of clock cycles required
by these two instructions is 18 clock cycles.
Since the FLIGHT-86 board is running at 14.7456/3 MHz, 1 clock cycle will
take 3/14.7456 microseconds, and 18 clock cycles will take 54/14.7456
microseconds. Thus, the total time taken by the loop is 32768 × (54/14.7456
× 10-6) = 0.12 seconds
The previous loop requires 0.12 seconds. Thus, this loop needs to be executed almost
8 times to generate a delay of 1 second. The following example shows how to use this
loop inside a program to turn ON/OFF an LED for specific amounts of time.
38
C O E 3 0 5 L A B M A N U A L
Example 3.2: Write a program to turn ON an LED for 3 seconds, then turn it
OFF for another 3 seconds, and repeat this cycle.
Delay PROC
L1: MOV CX, 8000h
L2: DEC CX
JNZ L2
DEC DL
JNZ L1
RET
Delay ENDP
COMSEG ENDS
END Start
Run the above program on the FLIGHT-86 board and estimate the ON/OFF time of
LED 0. What you conclude about the accuracy of the software delays?
39
C O E 3 0 5 L A B M A N U A L
Each one of the previous registers has a unique address, and can be accessed using
I/O operations (i.e. IN and OUT). Table 3.1, shows the addresses assigned to four
registers in the FLIGHT-86 board.
1. Latch Counter: allows you to latch the current register count, and
then read the counter value ‘on the fly’
40
C O E 3 0 5 L A B M A N U A L
Mode 0 - Interrupt on Terminal Count: The output goes low after the mode set
operation, and remains low while counting down. When the count decrements to zero,
the output goes high and remains high until then next mode is set or a new count is
loaded. See Figure 3.3 (a).
Mode 2 - Rate Generator: A divide by N counter. The output is low for one input
clock period and then high for N clock periods. This cycle repeats until a new mode is
selected. See Figure 3.3 (b).
Mode 3 - Square Wave Rate Generator: Similar to Mode 2, except that the output is
high for the first half of the count and goes low for the other half. See Figure 3.3 (c).
Mode 4 - Software Triggered Strobe: The output goes high once the mode is set,
and remains high while the counter is decremented. When the counter decrements to
zero, the output goes low for one clock cycle and then goes high again. The output will
remain high until a new mode or count is loaded. See Figure 3.3 (d).
41
C O E 3 0 5 L A B M A N U A L
42
C O E 3 0 5 L A B M A N U A L
In order to program any one of the three counters in a certain mode, you need to do
two things. First, send a control word to the Control Word Register. Then, load a
proper value into the counter register. These two steps are illustrated in the following
example.
(2) Since the counter clock input is connected to PCLK (14.7456/6 MHz), it will
be decremented every 6/14.7456 microseconds. Hence, we need to load the
counter with the value (0.025 × 14.7456 × 10-6)/6 = 61440 = F000h.
The following code will load the required control word (i.e. 30h) into the Control
Word Register, and will load Counter0 with F000h.
MOV AL, 30h ; load the control word into AL
OUT 0Eh, AL ; and send it to the Control Register
43
C O E 3 0 5 L A B M A N U A L
interrupt pointer to enable the correct Interrupt Service Routine (ISR) to be executed.
The 8259 PIC chip can be programmed to perform a number of modes of operation,
which may be changed dynamically at any time in the program. Programming the 8253
PIC chip is not covered in this experiment. Instead, you will be given the necessary
code to set the chip in a proper mode of operation.
The 8086 processor uses the 8-bit pointer to fetch the address (i.e. offset and segment) of
the corresponding ISR from the Interrupt Vector Table (IVT). This is done as
follows. Suppose that the 8-bit pointer is n, then the 8086 will fetch four bytes starting
from the address 0000:n*4. The first two bytes contain the offset of the ISR, while the
next two bytes contain the segment of the ISR.
Illustrative Example
The following example illustrates how to program the 8253 PIT and 8259 PIC chips to
generate time delays.
Example 3.4: Write a program to turn ON an LED for 3 seconds, then turn it
OFF for another 3 seconds, and repeats this cycle. Do not use software delays.
44
C O E 3 0 5 L A B M A N U A L
55 Return:IRET
56 COMSEG ENDS
57 END start
45
C O E 3 0 5 L A B M A N U A L
In the previous program, lines 6 and 7 set the ES segment to 0000h, which is the base
address of the IVT. Lines 9 and 12 load the starting address of the ISR
(IR6_ROUTINE) into the IVT. This routine will handle any request on IR6. Lines 16
and 17 initialize the 8255 PPI chip. Lines 19 to 26 initialize the 8259 PIC chip. Lines 29
and 20 initialize the 8253 PIT chip. Lines 32 to 35 load the Counter0 with the value
F000h. This will generate an interrupt every 25 ms (120 interrupts every 3 seconds).
The main routine starts by setting all LEDs off by sending 00h to port B (Lines 40 to
42), and waits for an interrupt on IR6 (Line 43). Upon receipt of the interrupt, the
control is transferred to IR6_ROUTINE (Line 47). This routine toggles LED0 every
120 interrupts (i.e. every 3 seconds).
Exercises
MOV CX, Y
L1: DEC CX
JNZ L1
What value of Y makes the loop executes in 0.225 seconds?
3.2. Modify the program in Example 3.2 such that Counter0 is set in Mode 0
a. 100 KHz
b. 10 KHz
c. 1 KHz
3.4. Interface a simple relay driver circuit to 8255 port, and write a program
switch ON/OFF a lamp every 10 seconds.
46