Chapter 6 - 10
Chapter 6 - 10
This chapter consists of two major sections. The first ADD AL. BL : Result: AL = 0110 1110 = 6EH. which
section isa dictionary of all the 808618088 Instructions. is incorrect BCD
For each instruction. we give a detailed description of AAA Now AL = 00000100. unpacked BCD 4.
its operation, the correct syntax for the instruction, and CF = I indicates answer is 14 decimal
the flags affected by the instruction. Numerical examples
are shown for those instructions for which they are NOTE: OR AL with 30H to get 34H. the ASCII
appropriate. Instead of putting the binary codes for the code for 4. If you want to send the result back to
instructions here, we have listed them alphabetically in a CRT terminal. The I in the carry flag can be
Appendixcs A and B. Putting the codes together in a rotated into the low nibble of a register. ORed with
table makes them easier to find if you are hand coding 30H to give the ASCIi code for 1. and then sent to
a program. the terminal.
Thc second major section of this chapter is a dictionary
of commonly used 8086 assembler directives. The direc- The AAA Instruction works only on the AL register.
tives described here are those defined for the Intel The AAA instruction updates AF and CF. but OF. PF,
8086 macro assembler, the Microsoft macro assembler SF, and ZF are left undefined.
IMASM). and the Borland Turbo Assembler (TASMI. If
you are using some other assembler, it probably has
similar capabilities, but the names may be different. AAD—BCD-to-Binary Convert before Division
You will probably use this chapter mostlyas a reference
to get the details of an Instruction or directive as you AAD converts two unpacked BCD digits in AU and AL
write programs of your own or decipher someone else's to the equivalent binary number in AL. This adjustment
programs. However, you should skim through the chap- must be made before dividing the two unpacked BCD
ter at least once to give yourself an overview of the digits in AX by an unpacked BCD byte. After the division,
material It contains. You should not try to absorb all of AL will contain the unpacked BCD quotient and AU will
contain the unpacked HCD remainder. PF. SF. and ZF
this chapter at once. Many of the Instructions described
are used and discussed In various example programs are updated. AF, CF. and OF are undefined after AAD.
throughout the book. For these instructions, we have
included references to the appropriate sections in the EXAMPLE;
text.
AX - 0607H unpacked L3CD for 67 decImal
CU 09H. now adjust to binary
INSTRUCTION DESCRIPTIONS AAD : Result; AX 0043 = 4311 6' ......
DIV CII - Divide AX by unpacked DCI) iii
AAA—ASCII Adjust for Addition Quotient; AL 07 unpacked DCI
Numerical data coming into a computer from a terminal Remainder; AH 04 unpacked DCI)
is usually in ASCII code. In this code, the numbers 0 to Flags undefined after DIV
9 are represented b y the ASCII codes 30H to 39H. The
8086 allows y ou to add the ASCII (-odes for two di-cuual NOTE If an attempt Is made to divide b y 0. the
8086 will do a t ype 0 Interrupt. The type 0 Interrupt
digits without maskini off the 3 in the upper nibble
of each After the addition, the AAA instruction Is used response is described in Chapter 8.
to ritaki' sure the result is the correct unpacked UC[). A
si mplt- nit tncrical cxarttple will show how this works. AAM—BCD Adjust after Multiply
fiefore you can multipl y two ASCII digits. you must first
I
mask the upper 4 hits of each. This eaves unpacked
Assume Al. - 0011 0101. ASCII 5 DCL) lone F3CD dtit per b\leJ in each h1e After the two
[II. - 0011 1001. ASCII 9 unpacked [ICE) digits are multiplied, the Instr-iic-
-19 131
tion is used to adjust the product to two unpacked BCL) ADC—Add with Carry—ADC Oestination,Soutce
digits In AX.
ADD—Add—ADD Destination,Source
AAM works only after the multiplication of two un-
packed BCD bytes, and it works oniy on an operand in These instructions add a number from some source to
AL. AAM updates PF. SF. and ZF, but AF. CF. and OF a number from some destination and put the result in
are left undefined. the specified destination. The Add with Carry instruc-
tion, ADC, also adds the status of the carry flag into the
EXAMPLE result. The source may be an immediate number, a
register, or a memory location specified by any one of
AL 00000101 = unpacked BCD S the 24 addressing modes shown in Figure 3-8. The
BI-I = 00001001 = unpacked BCD 9 destination may be a register or a memory location
MULBH ;ALXBH;resuitinAX specified by any one of the 24 addressing modes in Figure
AX = 0000000000101101 OO2DH 3-8. The source and the destination in ari instruction
AAM ; AX = 00000100 00000101 = 0405H, cannot both be memory locations. The source and the
which is unpacked BCD for 45. destination must be of the same type. In other words.
If ASCII codes for the result are they must both be byte locations, or they must both be
desired. use next instruction word locations. If you want to add a byte to a word, you
OR AX.30301-1 Put 3 in upper nibble of each byte. must copy the byte to a word location and fill the upper
:AX = 001 10100 001 101Ot = 3435H. byte of the word with 0's before adding. Flags affected:
which is ASCII code for 45 AF, CF. OF, PF, SF, ZF.
AND word in DS at offset ISII CALl. WORD l'TR IBX) An indirect within-segment
with word in CX register near or intrasegment call. Offset of first Instruction of
AND CX.ISU Result In CX register procedure is In two memory addresses In DS. Replaces
contents of IP with contents of word memory location
AND 8l-1.CL : AND byte in CL with b yte in WI in DS pointed to by DX.
Result In BR
CALL SMART_DIVIDE : A direct call to another seg.
AND word in DX with immediate ment—far or Intersegment call. SMART_DIViDE Is the
AND BX.00FFI-j : OOFFFI. Masks upper b yte, leaves name of the procedure. The procedure must be declared
nwer b y te unchanged far with SMART_DIVIDE PROC FAR at Its start (see
CLC EXAMPLES:
This instruction resets the direction flag to 0. No other Compare byte in CL with
flags are affected. If the direction flag is reset, SI and DI CMPBH.CL byte in BU
will automatically be incremented when one of the string
instructions, such as MOVS. CMPS. or SCAS. executes. Compare word in DS at
Consult the string instruction descriptions (or examples displacement TEMP_MIN
of the use of the direction flag. CMP CX,TEMP_MIN with word in CX
The variable-port IN instruction has the advantage that I. Decrement the stack pointer by 2 and push the flags
the port address can be computed or dynamically deter- onto the stack.
mined in the program. Suppose, for example, that an
8086-based computer needs to input data from 10 2. Decrement the stack pointer by 2 and push the
terminals, each having Its own port address. Instead of contents of CS onto the stack.
having a separate procedure to input data from each 3. Decrement the stack pointer by 2 and push the
port, we can write one generalized input procedure and offset of the next instruction after the INT number
simply pass the address of the desired port to the instruction on the stack.
procedure in DX. The IN instructions do not change any
flags. 4. Get a new value for IP from an absolute memory
address of 4 times the type specified In the instruc-
tion. For an INT 8 instruction, for example, the new
I NC—Increment—I NC Destination IP will be read from address 00020H.
The INC instruction adds 1 to a specified register or to 5. Get a new value for CS from an absolute memory
a memory location specified in any one of the 24 ways address of 4 times the type specified in the instruc-
NOTE: The RET struction should not normally ADD AL,BL : Add two bytes, if result within
be used to return Interrupt procedures be- JNC OK : acceptable range. continue
140 CHAPTER 5l
jB/JC/jNAE—Jump if Below/jump does not contain all 0's, execution will simply proceed
if Carry/jump if Not Above or Equal to the next instruction. Note that this instruction does
These three mnemonics represent the same instruction. not look at the zero flag when it decides whether to Jump
The terms above and below are used when referring to
or not. The destination label for this Instruction must
beintherangeol - 128to + I2lbyteefromtheaddreee
the magnitude of unsigned numbers. The number 0111 of the Instruction after the JCXZ instruction. JCXZ
is above the number 0010. II. after a compare or some affects no flags.
other instruction which affects flags, the carry flag is a
1, this instruction will cause execution to Jump to a
label given in the instruction. If CF isO. the instruction EXAMPLE:
will have no effect on program execution. The destination
label for the Jump must be in the range of -128 bytes JCXZSK11LOOP ;IfCX = 0. skIp the process
to + 127 bytes from the address of the instruction NXT:SLJB [81(1,0713 Subtract 7 from data value
after the JB. JB/JC/JNAE affects no flags. For further INC BX Point to next value
explanation of Conditional Jump instructions, see LOOP NXX' Loop until CX 0
Chapter 4. SKlP_LOO: Next instruction
EXAMPLES:
JE/JZ—Jump if Equal/Jump if Zero
CMP AX.4371H Compare (AX - 4371H)
JE RUN_PRESS Jump to label RUN_PRESS If These two mnemonics represent the same instruction.
AX below 4371H If the zero flag Is set, then this instruction will cause
execution toJwnp to a label given in the instruction. If
ADD BX,CX Add two words and jump the zero flag is not 1. then execution will simply go on
JC ERROR.YIX to label ERROR_FIX if CF = 1 to the next instruction after JE or JZ. The destination
label for the JEIJZ inMructlon must be in the range of
AX.4371H - 128to +l27bytesfromtbeaddoftheco
CMP Compare (AX - 4371H) after the JE/JZ instruction. JFIJZ affects no flag..
JNAE RUN_PRESS Jump to label RUN_PRESS if
AX not above or equal to 4371H
EXAMPLES:
JBEJJNA—Jump if Below NXT:CMJ BX.DX Compare (BX-DX)
or Equal/jump if Not Above JE DONF Jump to DONE if BX - DX
These two mnemonics represent the same instruction. SUB BX.AX Ele subtract AX
The terms above and below are used when referring to INC CX Increment counter
the magnitude of unsigned numbers. The number 0111 JMP NXT Check again
is above the number 0010. If. after a compare or some DONE:MOV AX,CX Copy count to AX
other instruction which affects flags, either the zero flag
or the carry flag is 1. this in3tructlon will cause execution IN AL,8FH Read data from port 8FH
tojump to a label given in the instruction. If CF and ZF SUB AL.30H Subtract minimum value
are both 0. the instruction will have no effect on program JZ START_MACHINE Jump to label if result of
execution. The destination label for the jump must be subtraction was 0
in the range of -128 bytes to + 127 bytes from the
address of the instruction after the JBE. JBE/JNA affects
no flags. For further explanation of Conditional Jump ! G/J NLE_Jump if Greater/Jump
instructions, see Chapter 4. i Not Less Than or Equal
EXAMPLES: These tWo mnemonics represent the same instruction.
The terms greater and less are used to refer to the
CMP AX.437lH relationship of two signed numbers. Greater means
: Compare (AX - 437lH) more positive. The number 00000111 is greater than
JBE RUN_PRESS : Jump to label RUN_PRESS If AX the number 11101010. because in signed notation the
below or equal to 437111 second number Is negative. This instruction is usually
CMP AX.43711-1 used after a Compare instruction. The instruction will
Compare (AX - 437111) cause a jump to a label given in the instruction if the
JNA RUN_PRESS Jump to label RUN_PRESS if AX
not above 43711-I zero flag is 0 and the carry flag Is the same as the
overflow flag. The destination label must be in the range
of -128 bytes to + 127 bytes from the address of the
ICXZ-..-Jump if the CX Register Is Zero instruction after the JG/JNLE instruction. If the jump
is not taken, execution simply goes on to the next
This instruction will cause a jump to a label given in instruction after the JO or JNLE instruction. JGIJNLE
the instruction if the CX register contains all 0's. If CX affects no flags.
instruction. This type of jump is referred to as direct register or memory location. The JNO instruction will
because the displacement of the destination or the cause the 8086 to jump to a destination given in the
destination itself is specified directly in the instruction. Instruction If the overflow flag is not set. The destination
must be In the range of - 128 bytes to + 127 bytes from
JMP BX Replace the contents oT IP with the contents the address of the instruction after the JNO instruction.
of BX. BX must first be loaded with the offset of the If the overflow flag is set, execution will simply continue
destination instruction In CS. This is a near jump. It is with the next instruction after JNO. JNO affects no
also referred to as an indirect jump because the new flags.
value for IP comes from a register rather than from the
Instruction itself, as in a direct jump.
EXAMPLE:
JMP WORD PTR IBXI Replace II' with a word from a
ADD AL,BL Add signed bytes in AL and BL
memory location pointed to by BX in DS. This is an
JNO DONE Process done if no overflow
indirect near jump.
MOV AL.00H Else load error code in AL
DONE: OUT 24H.AL Send result to display
JMP DWORD PTR IS!] Replace IP with a word pointed
to by SI in DS. Replace CS with a word pointed to by SI
+ 2 In DS. This Is an Indirect far jump. JNP/JPO—jump if No Parity/Jump if Parity Odd
If the number of l's left in the lower 8 bits of a data
JNA—See Heading JBE word after an instruction which affects the parity flag
Is odd, then the parity flag will be 0. The JNP/JPO
JNAE—See Heading JB instruction will cause execution to jump to a specified
JNB—See Heading JA[ destination address if the parity flag isO. The destination
address must be in the range of - 128 bytes to + 127
JNBE—See Heading JA bytes from the addres of the instruction after the JNP/
JPO instruction. If the parity flag Is set, execution will
INC—See Heading JAE
simply Continue Ofl to the instruction after the JNP/JPO
JNE/JNZ—Jump if Not Equal/Jump if Not Zero instruction. The JNP/JPO instruction affects no flags.
These two mnemonics represent the same instruction.
If the zero flag is 0, then this instruction will cause EXAMPLE:
execution to jump to a label given in the instruction. If
the zero flag is 1. then execution will simply go on to IN AL,OF8H Read ASCII character from UART
the next insu-uction after JNE or JNZ. The destination OR AL.AL Set flags
label for the JNE/JNZ instruction must be in the range of JPOERROR1 : Even parity expected, send error
- 128 to + 127 bytes from the address of the instruction message if parity found odd
after the JNE/JNZ Instruction. JNE/JNZ affects no flags.
JPE—See Heading JP
LEA—Load Effective Address—LEA
JPO—See Heading JNP Register,Source
JS.—Jump if Signed (Jump if Negative) This instruction determines the offset of the variable or
memory location named as the source and puts this
This instruction will cause execution to Jump to a offset in the indicated 16-bit register. LEA changes no
specified destination If the sign flag is set. Since a I In flags.
the sign flag indicates a negative signed number, you
can think of this instruction as saying "jump if negative'
or "Jump if minus." If the sign flag is 0, indicating a EXAMPLES:
positive signed result, execution will simply go on to the
next instruction after JS. The destination for the Jump LEA BX,PRICES : Load BX with offset of
must be in the range of —128 bytes to + 127 bytes from PRICES in OS
the address of the instruction after the JS. JS affects
ho flags. LEA BP,SS:STACK_TOP : Load BP with offset of
STACIL.TOP in SS
EXAMPLE LEA CX.IBXIDfl Load CX with EA =
(BX) + (DI)
ADD BL.DH Add signed byte in PH to Signed
byte iii 81,. A program example will better show the context in which
JS TOO_.COLD Jump to label TOO_COLD if result this instruction is used. If you look at the program in
of addition is negative number Figure 4-2 Ic, you will see that PRICES is an array of
MOV IBXI.AL Put result back In array and ZF = 1. The destination address for the jump must
INCBX be in the range of —128 bytes to + 127 bytes from the
LOOP NEXT Repeat until all elements address of the instruction after the LOOPNE/LOOPNZ
adjusted instruction. LOOPNE/LOOPNZ affects no flags. See
Chapter 4 for further discussion and examples of the
LOOPNE/LOOPNZ Instruction.
LOOPEJLOOPZ—Loop While
CX * 0 and ZF = 1
EXAMPLE:
LOOPE and LOOPZ are two mnemonics for the same
Instruction. This instruction is used to repeat a group MOV BX,OFFSET ARRAY Point BX to Just
of instructions some number of times or until the zero DEC BX before start of array
flag becomes 0. The number of times the instruction MOV CX, 100 Put number of array
sequence is to be repeated is loaded into CX. Each time elements in CX
the LOOP instruction executes. CX is automatically NEXT: INC BX Point to next
decr.mented by 1. If CX ^ 0 and ZF = 1. execution element in array
will Jump to a destination specified by a label in the CMP IBXI.ODH Compare array
instruction. If CX = 0 after the autodecrement or if element with ODH
ZF = 0. execution will simply go on to the next instruc- LOOPNE NEXT
tion after LOOPE/LOOPZ. In other words, the two ways
to exit the loop are CX = 0 or ZF = 0. The destination NOTE: When the LOOPNE instruction executes.
address for the jump must be in the range of - 128 CX will be decremented by I. If CX 0 0 and ZF =
bytes to + 127 bytes from the address of the instruction 0. execution will go to the label NEXT. If CX = 0
after the LOOPE/LOOPZ instruction. LOOPE/LOOPZ or ZF = I. execution will go on to the next
affects no flags. See Chapter 4 for further discussion instruction after LOOPNE. If CX 0 and ZF = 0
and examples of the LOOPE/LOOPZ instruction. on exit. 0011 was not found in the array. If CX 0
0 on exit from the loop, then BX points to the first
element which Contains ODH. If CX = 0 and ZF =
EXAMPLE:
1 on exit from the loop, the last array element was
ODH.
MOV BX,OFFSET ARRAY Point BX to Just
DEC BX before start of array
MOVCX,l0O Put number of array LOOPNZ—See Heading LOOPNE
elements in CX
LOOPZ—See Heading LOOPE
NEXT: INC BX Point to next
element in array MOV—Copy a Word or Bvte—MOV
CMP (BXI,OFFH Compare array Destination,Source
element with FFH
LOOPE NEXT The MOV Instruction copies a word or byte of data
from a specified source to a specified destination. The
NOTE: The next element Is thecked if the element destination can be a register or a memory location. The
equals FEE! and the element was not the last one source can be a register, a memory location, or an
In the array. If CX 0 and ZF I on exit, all immediate number. The source and destination in an
elements were equal to FFH. If CX 0 0 on exit from instruction cannot both be memoiy locations. The
the loop, then 13X points to the first element that source and destination in a MOV instruction must both
was not FFH. If CX = 0 and ZF = 0 on exit, then be of type byte. or they must both be of type word. MOV
/ the last element was not FFH. instructions do not affect any flags.
MOV RESULTS(BPj,AX; Copy AX to two memory loca- After the move, SI will be 1 greater than the offset of the
tions—AL to the first location, AH to the second. EA of last byte in the source string. DI will be I greater than
the first memory location is the sum of the displacement the offset of the last byte in the destination string. CX
represented by RESULTS and contents of BP. Physical will be 0.
address = EA + SS.
Example showing a byte multiplied by a word also be used to hold a place in a program for an
instruction thst will he added later.
MOV AX.MULTIPLICAND_16 : Load 16-bit
multiplicand into AX
MOV CL,MtJLTIPLIER_8 Load 8-bit multiplier NOT—Invert Each Bit
into CL of Operand—NOT Destination
MOV CH.00H Set upper byte of CX The NOT instruction inverts each bit (forms the l's
to au 0's complement) of the byte or word at thc specified destina-
MUL C) AX times CX. 32-bit tion. The destination can be a register or a memory
result in DX and AX location specified by any one of the 24 addressing modes
shown In Figure 3-8. No flags ax-c aflécted by the NOT
NEC—Form 2's Complement—NEC Destination instruction.
'this host! uction simply uses up three clock cycles and OR SJ.BP BP ORed with SI. result In SI.
Increments the instnwtlon pointer to point to the flex' BP not changed
Instruction. NOP affects no flags. The NOP Instruction
can be used to increase the delay of a delay loop, as OR BL.80H BL ORed with Immediate 80H.
shown in Figure 4-27a. When hand coding. a NOP can Set MSB of BI. to a I
The "C" in the middle of the mnemonic should help you CF - MSB - LSB
remember that CF is in the rotated loop and help
distinguish this instruction from the ROL instruction.
For multibit rotates, CF will contain the bit most recently
rotated out of the MSB. The "C" in the middle of the mnemonic should help you
The destination operand can be in a register or in a remember that CF is in the rotated loop and should help
memory location specified by any one of the 24 ad- distinguish this instruction from the ROR instruction.
dressing modes shown in Figure 3-8. If you want to For multibit rotates. CF will contain the bit most recently
rotate the operand one bit position, you can specify this rotated out of the LSB.
by putting a I in the count position of the instruction. The destination operand can be in a register or in a
To rotate more than one bit position, load the desired memory location specified by any one of the 24 ad-
number into the CL register and put "CL" in the count dressing modes shown in Figure 3-8. If you want to
position of the instruction. rotate the operand one bit position, you can specify this
by putting a 1 in the count position of the instruction.
NOTE: The 80186. 80286. 80386. etc., allow you To rotate more than one bit position, loa the desired
to specify a rotate of up to 32 bit positions with number into the CL register and put "CL" In the count
either an immediate number in the instruction or position of the instruction.
a number in CL.
NOTE: The 80186, 80286, 80386, etc.. allow you
RCL affects only CF and OF. After RCL. CF will contain to specify a rotate of up to 32 bit positions with
the bit most recently rotated out of the MSB. OF will be either an immediate number in the instruction or
a I after a single-bit RCL if the MSB was changed by a number In CL.
the rotate. OF is undefined after a multibit rotate.
The RCL instruction is a handy way to move CF into RCR affects only CF and OF, After RCR. CF will c'ntain
the LSB of a register or memory location to save it after the bit most recently rotated Out of the MSB. OF will be
addition or subtraction. a I after a single-bit RCR if the MSB was changed by
the rotate. OF will be undefined after multibit rotates.
EXAMPLES (SYNTAX):
EXAMPLES (CODING):
RCL DX,1 Word in DX 1 bit left, MSB to
CF. CF to LSB RCR BX. I Word in BX right 1 bit
CF to MSB, LSB to CF
MOV CL.4 Load number of bit positions to
rotate into CL MOV CL,04F1 Load CL for rotating
RCL SUMIBXI.CL : Rotate byte or word at effective 4 bit positions
address SUMIBXI 4 bits left RCR HYFE PTR IBXI ; Rotate byte at offset IBXI in
Original bit 4 now in CF. original DS 4 bit positions right
CF now in bit 3 CF = original bit 3. Hit 4
= original CF
;CF=0,BH= 10101110
ROLBH,l ;Result:CF,OF= l.BH = 01011101 EXAMPLE S (NUMERICAl.):
SAL'SHL—Shift Operand Bits Left, Put Zero SAL BYTE PTR IBXI. 1 Shift byte In DS at offset
in LSB(s)—SAL/SHL Destination.Count IBX4
1 bit position left. 0 in
SAL and SElL are two mnemonics for the same instruc- [SB
tion. This instruction shifts each bit in the specified
destination some number of bit positions to the left. As Example of SAL
a bit is shifted out of the LSB position, a 0 is put in the instruction's
LSB position. The MSB will be shifted into CF. In the use to help pack BCD
case of multiple-bit shifts. CF will contain the bit most IN AL,COUNTER_DIGIT Unpacked BCE) from
recently shifted in from the MSB. Bits shifted into CF counter to AL
previously will be lost. See the following diagram. MOV CL,04H Set Count for 4 bit
positions
SAL AL,CL : Shift BCE) to upper
CF't— MSB( LS8-0 nibble,
0's in lower nibble. Ready
toOR
The destination operand can be a byte or a word. It can another BCE) digit into
be in a register or in a memory location specified by any lower nibble of AL
one of the 24 addressing modes shown In Figure 3-8.
If the desired number of shifts is one, this can be
specified by putting a 1 in the count position of the EXAMPLE (NUMERICAL):
instruction. For shifts of more than 1 bit position, the
desired number of shifts is loaded Into the CL register, ;CFO.BX= 11100101 11010011
and CL is put In the count position of the instruction. SAL BX. 1 : Shift 13X register oontents I bit p1tion left
The advantage of using the CL register is that the :CF I,BX= 11001011 10100110
number of shvfta can be dynamically calculated as the ;0F0,PF= ?.SF 1,ZFO
program executes.
MOV CL,02H Load desired number of The flags are affected as follows: CF contains the bit
shifts In CL most recently shifted In from the [SB. For a count of
SAL BP,CL : Shift word In BP left (CL) one, OF will be a I If the two MSBs are not the same.
bit After a multibit SAR. OF will be 0. SF and ZF will be
positions. 0's In 2 LSBs updated to show the condition of the destination. PF
MOV CL,02H Load desired number of SUB AX,3427H Subtract Immediate number
shifts In CL 34271! from AX
SAR WORlD PTR IBPLCL Shift word at offset IBPI
in stack segment right SBB BX,(3427H( Subtract word at displacement
two bit positions. Two MSE3s 3427H in DS and contents
are now copies of of CF from DX
original MSB
SUB PRICES) BXI.04H Subtract 04 from byte at effective
EXAMPLES (NUMERICAL): address PRICESIBXI if PRICES declared with DB. Sub-
tract 04 from word at effective address PRICES)BXJ If
;AL = 00011101 = + 29 decimal CF 0 PRICES decarcz '!'. DW.
SAR AL.I Shift signed byte in AL right
to divide by 2 SBB CX,TABLEIBXI S.ibtract word from effective ad-
AL = 0000! 110 = + 14 decimal. CF = 1, dress TABLE[BXI and status of CF from CX.
:OF=0,PF= 0,SF=0.ZFO
SBB TABLE)BX],CX Subtract CX and status of CF from
BH = 11110011 - 13 decimal word In memory at effective address TABLEIBXI.
SAR BH, 1; Shift signed byte In BH right to
divide by 2 EXAMPLES )NUMERICAL):
:BH = 11111001 = - 7decimal.CF
:OF = 0,PF= l,SF= I. ZF=0 Example subtracting unsigned numbers
CL = 10011100 = 156 decimal
:BH 00110111 = 55 decimal
SBB—Subtract with Borrow—SBB SUB CL, BH : Result: CF,AF.SF.ZF = 0, OF.PF = 1
Destintion,Source CL = 01100101 = lOt decimal
SUB—Subtract—SUB Destination,Source
First example subtracting signed numbers
These instructions subtract the number in the Indicated CL = 00101110 + 46 decimal
source from the number in the Indicated destination BH = 01001010 + 74 decimal
and put the result In the indicated destination. For SUB CL. BH : Results: AF,ZF 0. PF
subtraction, the carry flag (CF) functions as a borrow CL 11100100 = —28 decimal
flag. The carry flag will be Set after a subtraction If the CF = I. borrow required
number in the specified source is larger than the number SF = 1, result negative
in the specified destination. In other words, the carry/ OF = 0. magnItude of result fits in 7 bIts
1 54 CHAPTER SIX
Second example subtracting NOTE: Scanning is repeated as long as the bytes
signed numbers are not equal and the end of the string has not
CL 10100001 - 95 decimal been reached. If a carriage return ODH is found.
:BH=01001100 +76decimal ZF = 1. and Dl will point at the next byte after the
SUB CL. BH Results: CF.ZF = 0. AF.PF 1 carriage return In the string. If a carriage return
CL 01010101 = + 85 decimal is not found, then CX = 0 and ZF 0. The
SF 0. result positive! assembler uses the name of the string to determine
OF 1, invalId result whether the string is of type byte or type word.
Instead of using te name, you can tell the assem-
bler the type of string directly by using the mne-
The overflow flag being set indicates that the magnitude monic SCASB for a byte string and SCASW for a
of the expected result, —171 decimal, is too large to fit word string.
in the 7 bits used for the magnitude in an 8-bit signed
number. If the Interrupt on Overflow Instruction, INTO.
has been executed previously, this error will cause the SHL-See Heading SAL
8086 to perform a software Interrupt procedure. Part of SHR-.---Shift Operand Bits Right, Put Zero
this procedure is a user-written subroutine to handle in MSB(s)—SIIR Destination.Coun(
the error.
This instruction shifts each bit in the specified destina-
NOTE: The SBB instruction allows you to sub- tion some number of bit positions to the right. As a bit
tract two multibyte numbers because any borrow is shifted right out of the MSB position, a Ols pufin its
produced by subtracting less significant bytes is place. The bit shifted Out of the LSB position goes to
Included in the result when the SBB instruction CF. In the case of a multiple-bit shift. CF will contain
executes. Although the preceding examples were the bit most recently shifted in from the LSB. Bits
for 8-bit numbers to save space, the principles are shifted into CF previously \wili be lost. See the following
the same for 16-bit numbers. For 16-bit signed diagram.
numbrs, however, SF isa copy of bit 15. and the
least significant 15 bits of the number are used to
represent the magnitude. Also. PF and AF function O—SMSB >LSB—i'CF
only for the lower 8 bits.
STD is used to set the direction flag to a 1 so that SI TEST—AND Operands to Update Flags-_TEST
and/or DI will automatically be clecremented to point Destination,Source
to the next string element when one of the string This instruction ANDs the contents of a source byte or
instructions executes, tithe direction flag is set. SI and' word with the contents olthe specified destination word.
or DI will be decremented by I for byte strings, and by Flags are updated, but neither operand is changed. The
2 for word strings. STD affects no other flags. Please TEST instruction is often used to Set flags before a
refer to Chapter 5 and the discussIon of the REP prefix Conditional Jump instruction.
in this chapter forexamples of the use of this instruction, The source operand can be an immediate number.
the contents of a register, or the contents of a memory
STI—Set Interrupt Flag (IF) location specified by one of the 24 addressing modes
shown in Figure 3-8. The destination operand can be in
Setting the interrupt flag to a 1 enables the INTR a register or in a memory location. The Source and the
interrupt input of the 8086, The instruction will not destination cannot both be memory locations in an
take effect until after the next instruction afte STI. instruction. CF and OF are both 0's after TEST. PF. SF.
When the INTR input is enabled, an interrupt signal on and ZF will be updated to show the results of the
this Input will then cause the 8086 to interrupt program ANDing. PF has meaning only for the lower 8 bits of the
execution, push the return address and flags on the destination. AF will be undefined.
stack, and execute an interrupt service procedure. An
IRET instruction at the end of the interrupt Service EXAMPLES (SYNTAX):
procedure will restore the flags which were pushed Onto
the stack, and return xcciition to the interrupted TEST AL.B1-( AND 8)-I with AL. no result stored.
program. STI does not affect any other flags. Update PF. SF. ZF
Please refer to Chapter 8 for a thorough discussion of
interrupts. TEST CX.000IH : AND CX with immediate number
00011-I. no result stored.
STOS/STOSB/STOSW__Store Byte Update PF. SF. ZF
or Word in String
TEST BP.IBXIDI) : AND word at offset IBXIDII in
The STOS instruction copies a byte from AL or a word OS with word in HP. no result
from AX to a memory location in the extra. segment stored. Update PF. SF. and ZF
As we showed you in Chapter 2, a microcomputer you need to write programs. Now we will look at the bus
consists of a CPU. memory, and ports. These parts are signals. timing, and circuit connections of an 8086 and
connected together by thiee major buses: the address an 8088. In a later chapter we will show the hardware
bus, the control bus, and the data bus. In Chapters 3 connections for the 80286 and 80386 microprocessorS.
through 6, however, we made little mention of the
hardwate of a microcomputer because we were mostly System Overview
concerned in these chapters with how a microcomputer
is programmed. In this chapter we come back to take a Figure 7-1 a shows a block diagram of a simple 8086-
closer look at microcomputer hardware. based microcomputer. This diagram is a closer look at
the generalized microcomputer in Figure 2-5. First, find
the 8086 CPU, the ROM; and the RAM in Figure 7-la.
OBJECTIVES Next, look for the ports, represented by the block labeled
MCS . 80 PERIPHERAL. As we discuss in detail later.
At the conclusion of this chapter. you should be able to: there .is a wide variety of port devices available. Some
examples are parallel port devices such as the 8255A.
I. Draw a diagram showing how RAMs, ROMs, and serial port devices. special port devices which interface
ports are added to an 8086 C pu to make a simple with CRTs. port devices which interface with keyboards.
microcomputer. and port devices which Interface with floppy disks.
2. Describe how addresses sent Out on the 8086 data Next, find the control bus, address bus, and data bus
bus are demultiplexed. in Figure 7-la. The basic control bus consists of the
signals labeled M/lO, PD, and WR at the top of the figure.
3. Describe the 2lgnal sequence on the buses as a If the 8086 is doing a read from memory or from a port.
simple 8086-based microcomputer fetches and exe- the RD signal will be asserted, If the 8086 is doing a
cutes an Instruction. write to memory or to a port, the WR signal will be
4. Describe how a logic analyzer is connected to micro- asserted. D urinL a read from memory or a write to
computer signal lines and how it Is used to make memory, the MflO signal will be high, and during port
state and timing measurements. operations the M/lO signal will be low. As we show you
in detail later, the RD. WR, and M/1O signals are used
5. Describe how address decoding circuitry gives a to enable addressed devices.
specific address to each device in a system and The address bus and the data bus are shown separately
makes sure that only one device is enabled at a time. on the right side of Figure 7-la. but where they leave
the 8086, the two buses are shown as a single bus
6. Calculate the access time required for a memory
device or port to work correctly in an 8086 microcom- labeled ADDR/DATA. The reason for this is that, in.
order to save pins, the lower 16 bits of addresses are
puter system.
multiplexed on the data bus. Here's an overview of how
7. List a series of steps you might take to troubleshoot this works.
a malfunctioning microcomputer system that once As a first st.ep in any operation where It accesses
worked. memory or a port. the 8086 sends out the lower 16 bits
of the address on the data bus. External latches such
as the 74LS373 octal devices shown in Figure 7-la are
ASIC 8086 MICROCOMPUTER SYSTEM used to 'grab" this address and hold it during the rest
of the operation. To strobe these latches at the proper
Introduction time, the 8086 outputs a signal called Address Latch
In previous chapters we worked with what is often called Enable or ALE. Once the address is stored on the outputs
of the latches, the 8086 removes the address from the
the programmer's model of the 8086. This model shows
features such as internal registers. number of address address/data bus and uses the bus for reading or writing
lines, number of data lnes. and port addresses, which data.
163
—23
^ IsssvctocxI I w.I._-
I—•lcu( iiwiI
RIASY iiitA
NWT I
T
GIlD oTiI--, =
'w I-----,
r—
I
i
I ALE
I
lSOPU GNO-
745373
GENERATOR AOtAD1 AOOR/DATA OCTAL
ADDRESS
I Al-AlE LATCH
(2 OR 3)
L__J 11111
53..,I
TRANSCEIVER
(2)
OPTIONAL cso, E 00
FOR INcMAEED
DATA 5(5 DRIVE 2)42 RAM (41 2716-2PR014(2) II ISCS-lO
I I PERIPHERAL
1) (2)
XE I IKXI 2KXS J 2KXS
(a)
FIGURI 7-1 (a) Block diagram of a simple 8066-based microcomputer. (See
also ne.d page.)
Another section of Figure 7-Ia to look at briefly Is the bus Is being used for other operations. For example, you
block labeled 8286 Transceiver. This block represents certainly don't want data bus buffer outputs enabled
bidirectional three-state buffers. For a vely small system Onto the data bus while the 8086 Is putting out the
these buffers are not needed, but as more devices are lower 16 bIts of an address on these lines. The 8086
added to a system, they become necessary. Here's why. asserts the DEN signal to enable the three-state outputs
Must of the devices—such as ROMs, RAMs, and ports— on data bus buffers at the appropriate time In an
connected on microprocessor buses have MOS inputs. operation.
so on a dc basis they don't require much current. The final section of Figure 7-la to look at is the 8284A
However, each Input or output added to the System data clock generator in the upper left corner. This device uses
bus. for example, acts like a capacitor of a few picofarads a crystal to produce the stable-frequency clock signal
connected to ground. In order to change the logic State which steps the 8086 through execution of its iñstruc-
on these signal lines from low to high, all this added tions In an orderly manner, The 8284A also synchronizes
capacitance must be charged. To change the logic state the RESET ilgnal and the READY signal with the clock
to a low, the capacitance must be discharged. If we so that these signals are applied to the 8086 at the
connect more than a few devices on the data bus lines. proper times. When the RESET input Is asserted, the
the 8086 outputs cannot supply enough current drive 8086 goes to address FFFFOH to get Its next Instruction.
to charge and discharge , the circuit capacitance fast The first Instruction of t)e system start-up program Is
enough. Therefore, we add external high-current drive usually located at this address. So asserting this signal
buffers toö The Job. is a way to boot, or start, the system. We will discuss
Buffers used on the data bus must be bidirectional the use of the READY input In the next section.
because the 8086 sends data out on the data bus and Now that you have an overview of the basic system
also re*ds data In on the data bus. The Data Transmitl connections For an 8086 microcomputer. let's take a
Recetue stgnal. DT/. from the 8086 sets the direction look at the signal present on the buses as an 8086 reads
In which data will pass through the buffers. When data from memory or from a port.
OuR Is asaerted iigh. the buffers will be set up to trans-
mit data from the 8086 to ROM. RAM, or ports. When
D/R Is asserted low, the buffers will be set up to allow 8086 Bus Activities During
data toT.come Into the 8086 from ROM, RAM. or ports.
a Read Machine Cycle
The buffers used on the data bus must have three- FIgure 7-lb shows the sIgnal activities on the 8086
state outputs so the outputs can be floated when the microcomputer buses during simple read and write
164 CHAPTER SEVEN
I- (4+N.7)-T,
13 13 TWMT 1. T T 13
F
ALE ___r-___r-
MnoJ(
XI __
A0DR/STA1'US
ADOR/DATA
READY
OuR
DEN
(b)
___I..
FIGURE 7-1 (continued) (b) Basic 8066 system timing. (Intel Corporation)
operations. Don't be overwhelmed by all the lines on this tion cycle. An instruction cycle consists of one or more
diagram. Their meanings should become dear to you as machine cycles.
we work through the diagram. To summarjze this, an instruction c ycle is made un
The first line to look at in Figure 7-lb is the clock of machine cycles, and a machine cycle is made up of
waveform. CLK. at the top. This represents the crystal. states. The time (or a State Is determined by the fre-
controlled clock signal sent to the 8086 from an external quency of the clock signal. In this section we discues
clock generator device such as the 8284 shown in the the activities that occur on the 8086 mIcrocomputer
top left corner of Figure 7-la. One cycle of this clock is buses during a read machine cycle.
called a state. For reference purposes, a state is measured The best way to analyze a timing diagram such as the
from the (ailing edge of one clock pulse to the (ailing one in Figure 7-lb is to think of time as a vertical line
edge of the next clock pulse. The time intervAl labeled moving from left to right across the diagram. With this
T in the figure is an example of a state. Different technique you can easily see the sequence of activities
versions of the 8086 have maximum clock frequencies on the signal lines as you move your Imaginary time line
of between 5 MHz and 10 MHz, so the minimum time across the waveforms.
for one State will be between 100 and 200 es depending During T 1 of a read machine cycle the 8086 first
on the part used and the crystal used. asserts the M/lO signal. It will assert this signal high if
A basic microprocessor operation such as reading a it is going to do a read from memory during this cycle.
byte from memory or writing a byte to a port is called a and it will assert M/lO low If it Is going to do a read from
machine cycle. The times labeled T In Ffgure 7-lb are a port during this cycle. The timing diagram in Figure
examples of machine cycles. As you can see in the figure. 7-lb shows two crossed waveforms for the MilO signal
a machine cycle consists of several states. because the signal may be going low or going high for
The time a microprocessor requires to fetch and a read cycle. The point where the two waveforms cross
execute an entire instruction is referred to as an Inst ruc- indicates the time at which the signal becomes valid for
look for the control bus, gnaJ pins. Pin 32 of the 8086
Introduction
in Figure 7-2 is labeled RD. This signal will be asserted It is difficult to observe microprocessor bus signals with
low when the 8086 is reading data from memory or from a standard scope because you can only look at two signal
a port. Pin 29 has the labels WR and LOCK next to it lines at a time. A logic analyzer such as the Tektronix
because it has two functions. The function of this pin 1230 shown in Figure 7-3 allows you to observe and
and the functions of the other pins between 24 and 31 make measurements on 16 to 64 signal lines at once.
depend on the mode in which the 8086 is operating. Tht- least expensive version of the 1230 allows you to
The operating mode of the 8086 is determined by the look at up to 16 signals at once, but expansion boards
logic level applied to the MN/MX input, pin 33. If pin 33 is can be added to increase the number of input signal
asserted high, then the 8086 will function in minimum lines to 32, 48. or 64. Hewlett-Packard, Gould, and
mode, and pins 24 through 31 will have the functions several other companies make comparable stand-alone
shown in parentheses next to the pins in Figure 7-2. In logic analyzers.
minimum mode, for example, pin 29 will function as Personal computers can be adapted to function as
WR. which will go low any time the 8086 writes to a port logic analyzers by installing plug' In units such as the
or to a memory location. The RD. WR. and M/lO signals
form the heart of the control bus for a minimum-mode,
8086 system. The 8086 is operated in minimum mode
In systems such as the SOK-86 where it is the only
microprocessor on the system buses.
If the MN/MX pin is asserted low, then the 8086 is in
maximum mode. In this mode, pins 24 through 31 wIll
have the functions described by the mnemonics next to
the pins in gure 7-2 In this mode, the control bus
signals (SO. SI. and S21 are sent Out in encoded form
on pins 26. 27, and 28. An exterçial bus controller device
decodes these signals to produce the control bus signals
required for a system which has two or more micropro-
cessors sharing the same buses. In Chapter Ii we
discuss 8086 maximum-mode operation and show its
use in multiple-microprocessor systems.
Another important pin on the 8086 is pin 21. the
RESET input. If this input is asserted and then released,
the 8086 will, no matter what it was doing. fetch its
next instruction from physical address FFFFOH. At this FIGURE 7-3 Tektronix 1230 logic analyzer. (Courtesy
address then, you put the first instruction you want of Tektronix Inc.)
EXTERNAL CLOCK I
INPUTS
CLK
ADJUSTABLE DISPLAY
THRESHOLD MEMORY SCAN CRT
COMPARATORS -
CIRCUIt DISPLAY
TRIGGER
WORD
COMPARATOR
AND
TRIGGER WORD TRIGGER
SELECTION CIRCUITRY
SWITCHES
(a)
t' D T) 0
DEDrFis KEYBOARD
.EfI .-L. DISPLAY
CONTR0LLcR
n /
PARALLEL
PORTS
FIGURE 7-6 Intel SDK-86 microprocessor Oevelopment board. (Intel
Corporation).
oE
C
. ,—'
-C
. -—
. C
•" 0
,- .
N
.
- .
o.9
-
e LI It-...
E
I/)
_c
. . •, 0
C
I I/I
.0
- - a =
' a
t- •IO
11 , C
'In CC
0 r -
174 /
8086- As you can see in Figure 7-7, considcrable circuitry are sockets for another four 2142.. The initial four
is connected to the RDY I input so that several conditions devices occupy the address space from 00000H to
can cause a WAIT state to be inserted in a machine cycle. OO7FFH. If four more 2142. are added, they will be
The structure labeled W27 through W34 above the WAIT- In the address space 00800H—OOF?FH. Another 3625
state generator in Figure 7-7 represents wire-wrap pins PROM is used here as a RAM decoder. As with the PROM
which can be jurnpered to specIf r the number of WAIT decoder, the purposes of this device are to turn on a
states desired in a machine cycle. We will discuss this memory device which corresponds to a particular ad-
in detail later. dress sent Out Ofl the address bus and to make sure that
By this time you may have noticed that the symbols only one device at a time Is outputting data on a data
for the 8284. 8086, and WAIT-state generator each have bus line. The 8086 can read or write a byte or It can
a small box containing a 2 in their lower right corner. read or write a word. Therefore, 16 data lines are
This number tells you that the detailed schematic for connected to the RAM block.
these parts will be found on sheet number two of the Now lets find the system ports In the block diagram
set of schematics. Figure 7-8 on pages 176-184 shows in Figure 7-7. Two 8255As at the top of the page
the complete schematic set for the SDK-86 board, so give the system programmable parallel ports. The term
you can check this out if you wish. programmable in this case means that, as part of your
The next parts to look for in the block diagram of the program. you send the 8255A a control byte. The control
SDK-86 are the address latches, which you know are byte tells the 8255A whether you want a particular group
needed to grab address information during Ti of a of lines on the device to function as outputs or as inputs.
machine cycle. The box Just below the 8086-in the In Chapter 9 we show you how to make up and send
diagram Indicates that three 74S373s are used for these control words. The two 8255As in this system can
address latches. ADO—ADI5, A16—A19, and BHE are be used individually to input or output parallel bytes.
connected to the inputs of these latches. As expected. They can also be used together to input or output words.
ALE is used to enable the latches. The information held For byte input or output operations, onjy ,, one of the
on the of the latches after ALE goes low is AO— devices will be turned on by asserting its CS input low.
A19 and BHE. The /20 after A0—Al9 on the output of For word input or output operations, both 8255As will
the latches indicates that there are 20 lines In this be turned on by asserting their CS inputs low. The high
group. A heavy black line is used to distinguish the byte of a word to be output, for example, will then be
demultiplexed address bus from the data bus. sent to one of the ports In the PORT I 4vice. The low
Next. follow the address lines to the right on the byte of the word to be output will go to the corresponding
diagram to find the ROM In the system. The box labeled port in the PORT 2 device. To be more specific. If the
PROM indicates that four 2316 or 276 devices are used high byte of an output word goes to port P1A. then the
for ROM in the s"stem. Each of these devices holds 2 low byte of that word will go to port P2A, In a later
I(bytes of memory. Also indicated in the PROM box in section of the chapter. we show hw the addresses work
the diagram are the absolute addresses where these out for these ports.
devices are located. Two of the EPROMs occupy the Most systems need a serial port so they can communi-
address space from FE000H to FEFFFH, and the other cate with CRT terminals, modems, and other devices
two occupy the address space from FF0001! to FFFFFH. which require data to be sent and received in serial
The 3625 PROM decoder connected to these EPROMs form. As shown in the lower left corner of Figure 7-7.
has two related purposes. The first is to produce a signal the SDK-86 uses an 8251A as a serial port. The letters
which turns on the desired ROM when you send out an USART on this device stand for universal synchronous)
address in the range assigned to that device. The second asynchronous receiver transmitter, which is quite a
purpose Is to make sure that only one device is out- mouthful. Chapter 13 discusses the Initialization and
putting signals onto the data bus at a time. We discuss use of the 8251A. For now, just think of this device
in detail later how address decoders are connected to as two back-to-back shift registers. One shift register
give a desired address to a particular device in a system. accepts a parallel byte from the system data bus and
Note that the enablepuL CS2, of the decoder PROM shifts it out the TxD output in serial form. The other
is connected to the RD signal from the 8086. This is shift register shifts in serial data from the RxD input
done so that the PROM decoder will be enabled only if and Converts It to parallel bytes which can be read by
the 8086 Is doing a read operation. Can you see why the 8086 on the system data bus. The 825 IA has only
you would not want a ROM to be turned on If you eIght data inputs, so data can only be written to or read
accidentally sent out, an address in its range during a from the 8251A a byte at a time. Therefore, only the
write operation? The answer Is that attempting to write lower 8 bits of the data bus are connected to it. Each of
to the outputs of a ROM can burn out both the ROM the shift registers in the 825 IA requires a clock signal
and the buffer outputs. The 1A26) in the PROM decoder with a frequency of 16 or 64 times the rate at which you
box of the block diagram. incidentally, indicates that want to shift data bits in or out. The clock for the
the 3625 IC will be numbered A26 on the schematic transmit shift register is called TxC, and the clock for
sheet where it is found. the receive shift register is called RxC on the block
Follow the address bus to the upper right corner of diagram. These are tied together because you usually
the block diagram in Figure 7-7 to find how RAM is want to send and receive data at the same rates. The
implemented in this system. The board comes with 2 clock for these inputs Is produced by dividing the 2.45.
Kbytes of static RAM contained in four 2142s. but there MHz PCLK signal from the 8284 clock generator. Wire-
7. I 6 j 5 4 1 I 2
Al -' 07
ES 07
*2 51 06
oe
A3 A2 05
05
3ZC2 *4 04
53 04
I -
AS .04 03
03 U
AS AS O2
Al
\6 10 Di o
(AS
Ag
3ZB2 AiS __________________________________________________________
-
"1
8 A??
0S JJ
9 2716
All
19 .,•
32C2 AS 18 . VPP 21
3ZA2
20
45V
12
F0000-FDFFF 7.2K AS 07
41 Al 16
15
FCO55-FCFFF I I *2 05
14
I 00-07 2ZC3
_____________ I *3 04
13
5 L - A4 03-
1Al2 — AS
5 4 3 2 A5 02
11 OS-DIS 2Z83
A13—.-.--6 Al 14 19
A14— *2 01 AS 0I C
I
Al5—..i *3
3Z82 Al6 02 13
2
Al 00
— A4 12 k5
*8 A36
All 03 22 *9 2716 5v
A5 19
1 11 CSY MO 21
*18 AS 04 18
17 CE --
OE
A19 — Al ic
2ZA3 u,i - A8 A26 - 20
15 3625 8 1/ DIS
22A3 RD *0 01 8255A
CS2 7 76 26 7
Al 06
-
CSI 6 IS 8251A 26 4
*7 05 -
5 14 8284 ¶8 9
*3 04 -
4 13 8279 46
P7 Pt A4 03
_____________________ 3 010 2116 74
A5 02
11 ¶2
2 I/O 2747 70 ¶0 B
j4j82 Cl--CS, AS 01
CS-Cl 7. C26-C26. I *1 08 18 9
C53 00 3625
* C28-C32
22, 10% j'C34C38
23
*8 8286 20 10
6 84 22 A30
ISV *9 2716 14L500 4 7
C40-052
GNO J4I1j I 9
410
IS --
CE OS
V
J20
2/ -
__J 7±ii±
74LSIO 14 7
8 1 /4LS14 14 1
A
12 lO 0/
7 16 74LS20 ¶4 7
Al 06
15 74530 - 14 7
A2 05
*3 oo 741S74 14 7
A4 74101*6 16 8
jJi2 V 3
03
II
AS 02- 74L5I64 14 7
INPUT 2
66 01 /415244 20 10 A
I 9
*INPUT 7 00 74LS393 14 7
23
*8 *3/ /445 16 8
A9 2716 40133 16
AlO 2/ '40313 20 10
-12 V IREFI 78
II 01
4 ALL DIODES 1N9148
3. ALL TRANSISTORS 0712905 -'----------___ - 20
2 ALL CAPACITANCE VALUES ARE IN 08, 80 20% , 50 V
I. ALL RESISTANCE VALUES ARE IN OHMS 5% 1/4 WATT
NOTES. UNLESS OTHERWISE SPECIFIED
FIGURE 7-8 SDK-86 complete schematics; see also pages 177-184. Sheet 11)1
9. (Intel Corporation)
176 CHAPTER SEVEN
6 4
I
5V
APIt API
22 K *75
2ZAB M/i i-?4LS1O LS4
52*3 OFF BOARD j)l2 ii 74L$
13 I, ___________ ____________
. __________ 64 62 64
• W27
64 64 1 MI
1W34
2.2 K 14.7446 MHZ
c ____ ______ 55 57 59 61 63 65 61? 691 WAIT STATE
I I SELECTOR
I C7...L
IPF_l'
LI4 J I Li1
bA 08 OC GD GE Q 00 Gui
F'''4_1: X2 14LS164
0 AS
.STM
H 1NESET RDY1
111 t iE 13
Ci8 CLA 114* INS I •sv
RPS I UP ._! EFI
2.2K T 1
osc Aii
1Z.9Z POLK 701K 8264CSVNC It -
1 -
— - API
744.S4 j A9 22K
RDY2 CLK 8__82 2
_E° W41 13 74LS20.
- ROY RST
W468 _____
15 III
5208 ______ _
7206 RESET OUT
9208 19 r'"
119
Ji
I v
2I 211 I
ROY RST CLK
A0_
4ZA7
09
______________
+5 V MN/MX AD ' , DI
AD: 07
AD 'I _________ _______________ 03
12C2, 3ZC5. 4Z05,
5ZC7, SZA2. 7ZD8,
AD 04 9ZD8
1 *21 AO 05
Rfl tR2. 74LS14. A03 11 _______ ____________ 06
NMI AD - 07
AL - ALE 3ZC5. 4ZA8
S2 Lc33
INTA "T'IUF HLD ., HLDA 3Z05.4ZA6
8 ________ _____________
AD 09
ADI 010
*01' - . IZC2.3ZB5.4Z6.
a *oi 012 SZC7.62A2
J2 it ADi; .- 013
INTR ADi-: 014
TEST ADI 015
HOLD A16/S AI8fS3'
*17/S *17/54 ' 3ZA5.4ZAO
W37J, Al6/S Al0/55 J
us *19/S A19/S8"I
• -8HE/S
34 - - )3ZA5
OHE,S1J
28 __________ _________________ - 1ZC7, 2208, 4286.
Mill) - N/b SZA7. 6Z87, 1288
- _________ _______________ h- IZB7, 4266, 5201,
8666 MC D 6287, 72C8, 9206
All
- 29
W - - 4Z68, 5Z01, 6Z87,
WA 7208, 9208
A INTA OT/r' 27 _________________________ DT/RZ86
4282
FIGURE 7.8 (continued) Sheet 2 of 9. 8086 SYSTEM CONNECTIONS, TIMING, AND TROUBLESHOOTING 177
S 4 3 I 2 I
fl
09 AG l207.6ZD7,7ZC8
Dl Al 1ZDJ, 5Z07, 6ZC7, 7ZCS, 9208
02 A2 1Z07.5Z07,6ZCI
D3 A3 'I
2ZC3 1ZD7,6ZC7.7ZC8 C
04 A4 .3'
05 AS ') 14 J3
06 A6 1207, 52C8, 6ZB8. 7ZC8
Dl Al)
2283 ALE
2Z83 HLDA
08 A6
09 A g ' 1ZO7,5281,67B7,7ZC8
010 MO)
Dli All 1ZO1, 5ZB1. 6207, 7288
2Z83 012
013
Al2 ',, 1ZC7, 5Z8?, 6207, 7288 T B
Al 3)
014 A14
12C7, 5287, 6ZC7, 7ZB8
015 MS }
Al 6/53 A16
Al 7/S4 All
IZC7,5ZA7,6ZC7
2283 A18/S5 A18
Al 9/S6 A19J
BHE/S7 ii 1Z07. 6207, 7ZCB
/
I A
wrap jumper pins. W19-W25. allow you to select the time per bit Is 416 s. for example, then the baud rate
desired TxC and RxC frequency from a divider chain in Is 2400 baud. Common baud rates for serial data
the 74LS393 baud rate generator. Baud rate is a way transmission are 300. 600, 1200. 2400. 9600, and
of specifying the rate at which data bits are shifted in 19,200.
or out of a serial device. Baud rate for a device such as The final port device to discuss here Is the 8279 in
the 825 IA is defined as I over the time per bit. lithe the bottom center of the SDK-86 block diagram (Figure
1 78 CHAPTER SEVEN
4 3 2
5 I I.
J2
808
Dl
—.-.-1A6 101
Dl
02 -1 A5 IS 102
1A4 141 103
0 03
2ZC3 104
04
SOS
D5
Dl 5DB
07 507
Al
.12
2^A3 BUFFER ON ii
12 508
08 lB
Dl J—ID9
BIl
019
Dli 24 B011
2ZB3 28 B012
012
D13 28 8013
014 Al I 5014
Al 32 BDIS
015
•5 V 1 8288
- Al
9 A25
ã 5Z87
.14
BMIO
(O BRO/
I RD BWR/
2ZA3'
DEN !.I Al 48 BDEN/
OuR 9A1
3I9
ULOA 2Z58
74L514
- 2A3 1Y3 44 BHLOA
- 2Z03 13 2A2 22 Se BALE J3
17 - 2A4 3 8$NTAI
2ZC3 INTA 2v4
1AI lvi 18 - 8S3
(Ai6/S3
1A2 i'7 16 - 954
2ZB3 ' AI71S4
I A18/S5 \A3 1Y3 14 - 8S5
1A4 1v4
A8 9
A - i 2A1 211 -
A8
16 26 74LS244
11 1
FIGURE 7-8 (continued) Sheet 4 of 9.
7 6 S 3
J6 J6
2ZC8 RESET OUT P1A036
RESET o
PIA1 40
(RD
6—
WR
1 2 2 P1A2 4
3
9A0 I.. Al
f PtA5
39 P1A642
7Z02 LOW PORT SEL 637 13
P1A7
015 27 07 14
26 17
014 2806 0 15 P1C1 24
19
013 D5
1
1116
217
P1C2 22
21
04 pica
2ZB3 012 20 23
011 313 Pc 13 P1C4 28
25
010 32 12 PiGS
02 28 27
09 Dl P1C6 32
I 6 P1C7
29
08 34D0 31
07 18 l'0 16
33
D6 19 P191 12
05 1 20 P182 C
04 2 21 P183
22C3 PB 22 P194 6
28
03 41
02 23 RIBS 10
43
DI 8255A P166 14
A35 6 24 4
00 25 P187
+5 V
iS i5
RESET 4 P2AO
AS P2A1 28
3ZC2' A6 -0
113 P22 42
5
A7 8 P2A3
A8 PA1340 P2A4
A9 539 P2AS
AlO P2A640
13 B
All 27 07 1 62837 P2A7
3282
Al2 2806 14 P2CO 24
17
A13
A14
1
1 15
PIC1 2
19
P2C2 4
2 16 21
A15 31 03 J 17 P2C3 6
23
02 PC 13 P2C4 26
25
4ZB2 01 12 P2C5 28
27
34 P2C6
oa 29
Al2 I 6 10 P2C7 32
31
A13 18 P280
A14 0 19 P281 22
1 35
A15 20 P292
3ZB2 2 21 P283 14
A16 39
A17 PB 22 P284 16
41
A18 23 P285 20 A
43
A19 8255A 24 P286 12
A40 45
2ZA3 M/R 25 P287
7
3Zc2 Al
3ZA2 BUE
All
Al2
A13
A14
3Z02 A15
A16
A17
AIB
A19
C
Al
A2
A3
3Z2 A4
- A5
Al
Al
3ZB2 Al
All
AC
B 2ZA34
I_ u,ic
oil
Dl.
Dl
2Z13
014
013
012
07
Dl
Dl
04 2ZC3
A 03
02
DI
0S
01 UI 4O0uJi&Q 0
-_.1 00000000 0000000 0
U >
- I__i, (I'
30300000
* -
61
'I
1!E
a,
0
=
C
0
U
-
U UI U U P C
N Q
I'-
C U
]
182
0 V I
ci
)
. *
0U
_3O0O0 ___________
U-
183
1]1 [
!
•-
I- -U
x ,c )C )C
L
ed - - -
)- ,- - >.
- .- =
- - (_)
:i+
__'
-4f-1i ____________
•
-ri
-44----
_1
4
V
•
.-
• m
—
_J C,
—o----
I o
-ow---- am.
p____
• I p.s,
- ,- Ca
C-
Ca
_______________________
184
produces the port select signals from a port address sent upper left corner of the component side of a board. There
out by the 8086. may be several 2716s on the board, but only one will be
The final parts of the SDK-86 block diagram to take a labeled A36.
look at are the buffers along the tight-hand edge. The Inadditlon to ICs. another type of device often found
purpose of these devices is to buffer the data and control on microprocessor boards is a resistor pack. You can
bus lines so that they can drive additional ROM, RAM. find an example in zone CS of schematic sheet 1. As you
or ports that you might add to the expansion area of the can see from the schematic, this device contains foui-
board. Note that the address lines are already buffered 2.2-kfl resistors. Resistor packs may physically be thin.
by the 74S373 address latches. vertical, rectangular wafers, or they may be in packages
simIlar to small ICs. The advantages of resistor packs
are that they take up less printed-clrct.it-board space
A First Look at the SDK-86 Schematics and that they are easier to install than individual
Now that you have seen an overview of the SDK-86. the resistors.
next step is to take a first look at Figure 7-8, which Some other symbols to look at in the schematics are
shows the actual schematics for the board. At first the the structures with labels such as J2 and P1. You can
many pages of schematics may seem overwhelming to find examples of these In zones C7 and B7 of schematic
you, but if you use the 5'mlnutefreak-out rule and then sheet 1. These symbols are used to indicate connectors.
approach the schematics one part at a time, you should The number in the rectangular box specifies the pin
have no trouble understanding them. The schematics number on the connector that a signal goes to. The
simply show greater detail for each of the parts of letter P stands for plug. A connector is consIdered a plug
the block diagram that we discussed in the preceding If it plugs into something else. In the case of the SDK.
sections of the chapter. 86, the connector labeled Pits the printed-circuit-board
At this point we want to make clear that it is not the edge connector. The letter J next to a connector stands
purpose of this chapter to make you an expert on the forjack. A connector is considered a jack if something
circuit connections of an SDK-86 board. We use parts of else plugs into it. On the SDK-86 board the jacksJl
these schematics to demonstrate some major concepts. through J6 are 50-pin connectors that you can plug
such as address decoding, and to show how the parts ribbon cable connectors into. These jacks allow the
are connected tog.ther to form a small but real system. address bus, data bus, control bus, and parallel ports
Even if you do not have an SDK-86 board, you can learn to be connected to additional clrcuitly.
a great deal from these schematics about how an 8086 One more point to notice on the SDK-86 schematics
system functions. Multlpage schematics such as these Is the capacitors on the power supply Inputs shown in
are typical for any microprocessor-based board or prod- zone B6 of sheet 1. A(it you can see there, the schematic
uct, so you need to get used to working with them. shows a large number of 0. 1-p.F capacitors in parallel
Before getting started on the next major concept, we with a 22-F capacitor. Most systems haveJlltering such
will discuss some of the symbols commonly used on as this on their power lines. You may wonder what is
microprocessor system schematics. First, take a look at the use of putting all these small capacitors In parallel
the numbers across the top and bottom of each sche- with one which is obviously many times larger. The
niatic and the letters along the sides of each. These are point of this is that the large capacitor filters out, or
ca1le zone coordinates, You use these coordinates to bypasses, low-frequency noise on the power lines, and
identi the location of a part or connection on the the small capacitors, spread around the board. bypas
schelPa. just as you might use similar coordinates on high-frequency noise on the power supply lines. Noise is
a ro,ur'ap to help you locate Bowers Avenue. For produced on the power supply lines by devices switching
exan. on sheet I of the schematics, find the lines from one logic state to another. If this noise is not
labelA I through A7 in the upper left corner. Next to filtered out with bypass capacitors, It may become great
the' .ines you should see 3ZC2. This indicates that enough to disturb system operation.
thc address lines come from zone C2 on sheet 3. To Glance through the SDK-86 schematics to get an idea
see what the lines actually connect to. first find sche- of where various parts are located and to see what
rustic sheet 3. Then move across the row of theschemat Ic additional information you can pick up from the notes
libeled C until you come to the column labeled 2. ThIs on them. In the next Section of this chapter. we discuss
zone is small enough that you should easily be able to how microcomputer systems address memory and ports.
find where these lines come from. The zone coordinates As part of the discussion, we cycle back to these schemat-
next to these lines on sheet 3 indicate the othesche- ics to see how the SDK-86 does it.
matle sheets and zones that these lines go to. For
practice. try finding where a few more lines connect Addressing Memo'y and Ports
from and to. in Microcomputer Systems
The next points to look at on the schematics are the
numbers on the ICs. in addition to a part number such
as 2716. each IC has a number of the form A36. This ADDRESS DECODER CONCEPT
second number is used to help locate the IC on the While discussing the block diagram of the SDK-86 board
printed circuit board. The number is commonly silk- earlier in this chapter, we mentioned that the 3625
screened on the board next to the corresponding IC, devices on the board serve as address decoders. One
Usually IC numbers are sequential and start from the function of an address decoder Is to produce a signal
8086 SYSTEM CONNECTIONS, TIMING, AND TROUBLESHOOTING 185
Al2
Do
*13
*14 DATA
BUS
Dl
A15
RD
+5 V
- 74LS138
which enables the ROM. RAM, or port device that you If the 74LS 138 is enabled by making its G2A and G2B
want enabled for a particular address. A second, related inputs low and its Gi input high, then only one output
function of an address decoder is to make sure that only of the device will be low at a time. The output that wlU
one device at a time is enabled to put data on the data be low Is detennijied by the 3-bit address applied to the
bus lines. C, B, and A select 'iputs. For exampfr, if CBA i 000.
It seems that every microcomputer system does ad- then the YO output will be low, and all the other outputs
dress decoding in a different way from every other will be high. This wlU assert the CS input of ROM 0. If
system. Therefore. instead of memorizing the method CBA is 001. the Yl output will be low and the ROM I
used In one particular system, it is important that you will be selected. If CBA is 1 11. then Y7 will be low, and
understand the concept of address decoding. You can only ROM 7 wIll be enabled. Now let's see what address
then figure out any system you have to work on. range these connections on the 74LS 138 wIll give each
of these ROMs in the system.
AN EXAMPLE ROM DECODER To determine the addresses of ROMs. RAMs, and ports
To start, look at Figure 7-9. This figure shows how eight in a system, a good approach in many cases is to use a
EPROMs can be connected in parallel on a common worksheet such as that in Figure 7-10. To make one of
address bus and common data bus. Just by looking at these worksheets, you start by writIng the address bits
the schematic you can see that these EPROMs output and the binary weight of each address bit across the top
bytes of data because each has eight outputs connected of the paper, as shown in the figure. To make it easier
to the system data bus. The number of address lines to convert binary addresses to hex, it helps if you mark
connected to each devie gives you an Indication of how off the address lines In groups of four, as shown. Next.
many bytes are stored ft it. Each EPROM has 12 address draw vertical lines which mark off the three address
lines lAO—Al I) connected to it. Therefore, the number lines that connect to the decoder select inputs (C. B.
of bytes stored In the device is 22 or 4096. If you have and A). For the decoder in Figure 7-9. address lines A14.
trouble with this, think of how many bits a Counter has Al.3. and Al2 are connected to the C, B. and A inputs
to have to Count the 4096 States from 0 to 4095 decimal, of the decoder, respectively. Then write under each
or 0000N to OFFFH. address bit the logic level that must be on that line to
Note that each 2732 in Figure 79 has a Chip Select. address the first location in the first EPROM.
CS. input. When this input Is asserted low, the addressed To address the first location in any of the EPROMs.
byte in a device will be output on the data bus. The the A0 through All address lines must all below, so put
74LS 138 in Figure 7-9 makes sure that the CS input of a 0 under each of these address bits on the worksheet. To
only one ROM device at a time is low. enable EPROM 0, the select inputs of the decoder must
DECODER
ADO RESS
INPUTS
FIGURE 7-10 Address decoder worksheet showing address decoding for eight
2732s in Figure 7-9.
be all 0's. Since address lines A14, Al3. and Al2 are blocks of memory. If you think of the address lines as
connected to these select Inputs, they must then all be the outputs of a 16-bit Counter, you can see how this
0's to enable EPROM 0. WrIte a 0 under each of these works. The end address for each EPROM has all l's in
address bits on the worksheet. Since address line A 15 address bits A0—A1 1. When you increment the address
is connected to the G2A enable input of the decoder, it to access the next byte In memory, these bits all go to
must be asserted low in order for the decoder to work 0, and a 1 rolls over into bits A14, A13. and Al2. This
at all. Write a0under the A15 bit on your worksheet. increments the count in these 3 bits by 1 and enables
Note that the RD signal from the microprocessor control the next highest 4096-byte EPROM. The count In these
bus is connected to the 02B enable input of the decoder. bits goes from binary 000 to ill.
The decoder then will only be enabled during a read
operation. This Is done to make sure that data cannot AN EXAMPLE RAM DECODER
accidentally be written to ROM. The 01 enable input of The system in Figure 7-9 contains only ROM. In most
the decoder is permanently asserted by tying it to + 5 V systems, you want to have ROM. RAM, and ports. To
because we don't need it for anything else in this circuit, give you more practice with basic address decoding, we
You can now read the starting address of EPROM 0 will show you now how you can add a decoder for RAM
directly from the woyksheet as 0000H. The highest to the system.
address In EPROM O,is that address where A0—Al I are Suppose that you want to add eight 2K x 8 RAMs to
all l's. If you put a I under each of these bits as shown the system, and you want the first RAM to start at
on the worksheet, you can see that the ending address address 8000H. just above the EPROMs, which end at
for EPROM 0 is OFFFH. Remember that Al2—Al4 have address 7FFFH.
to be low to select EPROM 0. A 15 has to be low to enable To start, make a worksheet similar to the one in Figure
the decoder. The addres, range of EPROM 0 is said to 7-10. Addressing one of the 2048 bytes (2") in each
be 0000H to OFFFH. a 4-Kbyte .b4ock. RAM requires II address lines. A0 through AlO. These
Now let's use the worksheet 'to determine the address lines will be connected directly to the address inputs on
range for EPROM 1. EPROM 1 is enabled when Al5 is each RAM, so draw a vertical line on the worksheet to
0. Al4 is 0. Al3 Is 0, and Al2Ia I. For the first address indicate this.
in EPROM I, address lines A0 through Al Imust all be The three address lines All, Al2. and Al3 will be
low. Therefore, the starting address of EPROM 1 Is used to select one of the eight RAMS, so write a 3-bit
10001-I. Its ending address, when AO through All are binary count sequence under these three columns In
all l's. Is IFFFH. If you look at the worksheet In Figure your worksheet.
7-10. you should see that the address ranges for the We want the RAM to Start at address 8000H. For this
other six EPROMs In the system are 2000H to 2FFFH, address, Al5 isa I and Al4 isa 0, so mark these values
3000H to 3FFFH, 4000H to 4FTflI, 5000H to 5FFFH. in the appropriate columns in your worksheet. Your
6000H to 6FFFH. and 7C)0H to 7FFFH. In this system, completed worksheet should look like the one In Figure
then, we use address lines A14, A13, and Al2 to select 7-I Ia. p. 188. Now, let's see how you can implement
one of eight EPROMs in the overall address range of this truth table with hardware.
00001-I to 7FFFH. Some people like to think of address Since you want to select one of eight RAM devices, you
lines Al4. Al3. and Al2 as "counting off" 4096-byte can use another 74L.S 138 such as the one we used lot'
DECODER
ADDRESS
INPUTS
(a)
DtCODER
SELECT
A5 A4 A3 INPUTS
(i lot
FIGURE 7-12 Adding a port device decoder. (a) Schematic for 741.5138
connections. (b) Address decoder worksheet.
dont matter when addressing ports. Again, we usually stored byte. As you know from previous chapters. when
use 0's for these bits to give the simplest address. you write a word to memory with an instruction such
Using a decoder which translates memory addresses as MOV DS:WORI) rrRt43m11}.BX. the word is actually
to chip select signals for port devices Is called memorr4- written into two consecutive memory addresses. Asum-
mapped I/O. In this system a port will be written to or ing that DS contains 0000. the low byte of the word is
read from in the same way as any other memory location. written Into the specified memory address. 0437A}1, nd
In other words, If this were an 8088 system, you would the high byte of the word Is written Into the next-higher
use an instruction such as MOV AL.DS:BYTE PTR address. 0437BH. To make it possible to read or write
00000H to read a byte of data from the first port to the a word with one machine cycle, the memory for an 8086
AL register Instead of using the MOV DX.00000H and is set up as two "banks" of up to 524,288 bytes each.
IN AL.DX Instructions. The advantage of memory- Figure 713a, p. 190, shows this in diagram form.
mapped 1/0 Is that any Instruction which references One memory bank contains all the bytes which have
memory can be used to input data from or output data even addresses such as 00000, 00002. and 00004. The
to ports. In a system such as this, for example, the single data lines of this bank are connected to the lower eight
Instruction ADD AL,DS:I3YTE VtROCOOOHJ could be data lines. DO through D7. of the 8O8. The other
used to input a byte of data from the port at address memory bank Contains all the bytes which have odd
C000H and add the byte to the AL register. The disadvan- addresses such as 00001. 00003. and 00005. The data
tage of memory-mapped 110 is that some of the system lines of this bank are connected to the upper eight data
memory address space is used up for ports and is lines. D8 through Dl5, of the 8086. Address line AO Is
therefore not available for memory. used as part of the enabling for memory devices In the
You can use memory-mapped 1/0 with any micropro- lower bank. An addressed memory device In this bank
cessor. but some microprocessors, such as those of the will be enabled when address line AD Is low, as it will be
8086 family, allow you to setup separate address spaces for any even address. Address lines Al through A19 are
for input ports and for output ports. You access ports used to select the desired memory device in the bank
In these separat address spaces dircctTy with (he IN and to address the desired byte in that device.
and OUT instructions. Having separate address spaces Address lines Al through A19 are also used to select
[or input and output ports Is called direct I/O. The a desired memory device In the upper bank and to
advantage uf direct [/Ois that none of the system memory address the desired byte in that bank. An additional
space is used for ports. The disadvantage Is that Only part of the enabling for memory devices In the u_2p.r
the specialized IN and OUT Instructions can be used to bank is a separate signal called bus high enable. BHE.
Input or output data. BHE is multlpiexed out from the 8086 on a signal line
in a later section of this chapter. we show how direct at the same time as an address is sent out. An external
I/O is done with the 8086, but first we will discuss how latch. strobcd by ALE, grabs the BIlE signal and holds
the 8086 addresses memory. It stable for the rest of the machine cycle. Just as is done
with addresses. Figure 7-13b shows you the logic level
that will be on the BHE and AO lines for different types
8086 and 8088 Addressing of memory accesses.
and Address Decoding If you read a b y te [ronl or Write a byte to an even
address such as 00000Ff. AO will be low and BHE will
8086 MEMORY BANKS
be higF. The lower bank will be enabled, and the upper
The 8086 has a 2Oblt address bus, so it can address bank will be disabled. A byte will be transferred to or
2° or 1.048,576 addresses. Each address represents a from the addressed location in the low bank on DO-
00002
00000H
I.)
DATA -
ADDRESS TYPE BUS DATA
______ CYCLES LINES USED
0000 BYTE 1 0 ONE 00-07
0000 WORD 0 0 ONE DO-015
0001 BYTE 0 1 ONE 07-015
0001 WORD 0 I FIRST 00-07
1 0 SECOND 07-015
Ib)
FIGURE 7-13 8086 memory banks. (a) Block diagram. (b) Signals (or byte and
word operations.
07. For an instruction such as MOV AH.DS:BYTE enabled. The byte will be transferred from memory
PTRI0000I. the 8086 will automatically transfer the byte address 0000IH in the high bank to the 8086 on lines
of data from the lower data bus lines to AH. the upper 08-0 15. The 8086 will automatIcally transfer the byte
byte of the AX register. You Just write the instruction of data from the higher eight data lines to AL. the low
and the 8086 takes care of getting the data in the right byte of the AX register. Note that address 0000IH Is
place. actually the first location in the upper bank.
Now. if the OS register contains 0000H and you use The final case in Figure 7-13b Is the one where you
an instruct ion such as MOV AX,DS:WORX) PTRI0000I want to read a word Irom or write a word to an
to read a word from memory into AX, both A0 and BHE odd address, The instruction MOV AX.DS:W0RD
will be asserted low. Therefore, both banks will be PTRI000IHI copies the low byte of a word from address
enabled. The low byte of the word will be transferred 00001 to AL and the high byte from address 00002H to
from address 00000H to the 8086 on 00-07. The high AN. In this case, the 8086 requires two machine cycles
byte of the word will be transferred from address 00001 H to copy the two bytes from memory. During the first
to the 8086 on 08-015. The 8086 memory, remember. machin , cle the 8086 will output address 0000lH,
is set up in banks so that words: which have their low assert RHE low, and assert A0 high. The byte from
byte at an even address, can be transferred to or from address 0000)H will be read into the 8086 on lines 08-
the 8086 in one bus cycie. When programming an 8086. Dl5 and put in AL. During the second machine cycle
then, It is important to start an array of words on an the 8086 will send out address 00002H. Since this is
even address for most efficient operation. If you are an even address, A0 will be low. However, Since we are
using an assembler, the EVEN directive is used to do accessing Only a byte. HHE will be high. The second byte
this. will be read into the 8086 on lines 00-07 and put in
When you use an Instruction such as MOV AL.DS AH. Note that the 8086 automatically takes care of
BYTE P1'RJOOOl Ito access Just a byte at an odd address. gettIng a byte to the correct register regardless of which
A0 will be high and BHE will be asserted low. Therefore. data lines the byte comes in on.
the low bank will be disabled, and the high bank will be The main reason that the A0 and BIlE signals function
the way they do is to prevent the writing of an unwanted In the system. you need to look next at the 3625 address
byte into an adjacent memory location when the 8086 decoder labeled A26 on sheet 1 of Figure 7-8.
writes a byte. To understand this, think what would A 3625 isa 1K x 4 bipolar PROM which functions as
happen if both memory banks were turned on for all ah address decoder, just as the 74LS 138 performs In
write operations and you wrote a byte to address 00002 Figures 7-9 and 7-11. Since a 3625 has open collector
with the instruction MOV DS:BYrE PTR!00021.AL. The outputs, a pull-up resistor to + 5 V is required on each
data from AL would be written to address 00002 as output. The dotted box around the four resistors on the
desired. However, if the upper bank were also enabled, schematic indicates the four are all contained In one
the random data on D8—D15 would be written into package, resistor pack 5 (RP5). The 3625 translates an
address 00003. Since the 8086 is designed so that BHE address to a signal which is used as part of the enabling
Is high during this byte write. theupper bank of memory of the desired device. Using a PROM as an address
is not enabled. This prevents the random data on D8— decoder, however, is for several reasons much more
D15 from being written to address 00003. powerful than using a simple decoder such as the
Now that you have an overview of address decoding 74LS 138. In the first place. the 3625 is programmable.
and of the 8086 memory banks, let's look at some which means that you can move the memory devices to
examples of how all this is put together in a small new addresses in memory by simply programming a new
system. PROM. Second. the large number of inputs on the PROM
allows you to select a specific area of memory without
using external gates. If. for example, you wanted the
ROM ADDRESS DECODING ON THE SDK-86 G2A input ofa 74LS138 to be asserted ifAl 1—A15 were
Sheet 1 of the SDK-86 schematics in Figure 7-8 shows all high. you would have to use an external NAND gate
the circuit connections for the EPROMs and EPROM to detect this condition. With a PROM, you can just
decoder. The 2716 EPROMs there are 2K x 8 devices. make this condition part of the truth table you use to
Two of the EPROMs have their eight data outputs burn the PROM.
connected in parallel to system data lines D0—D7. These Now, to analyze any address decoder circuit, first
two EPROMs then give 4 Kbytes of storage in the lower determine what signals are required to enable the de-
memory bank. The other two EPROMs have their data coder. The CS 1 enable input of the 3625 EPROM decoder
outputs connected in parallel to system data lines D8— is tied to ground. so it is permanently enabled. The CS2
D15 to give 4 Kbytes of storage in the upper bank of enable input is tied to the RD signal 1ron the 8086. so
ROM. that the decoder will only be enabled if the 8086 is doing
Eleven address lines arc needed to address the 2 a read operation. As explained previously, you don't
Kbytes in each device. Therefore, system address lines want to accidentally enable a ROM if you send out a
Al—All are connected to all the EPROMs in parallel. wrong address during a write operation.
Remember that A0 cannot be used to select a byte in The next step in analyzing a decoder circuit using a
the EPROMs because, as we described in the last section, PROM is to consult the manufacturer's manual for the
it is used to enable or disable the lower bank. system. You have to do this because, for a PROM, the
A 2716 has two enable inputs. CE and OE. In order relationship between the inputs and the outputs cannot
for the 2716 to output an addressed byte, both of these be determined directly from the schematic.
enable inputs must be asserted low. The CE inputs of Figure 7-14 shows the truth table for the PROM
the two devices In the lower bank are connected to from the SDK-86 manual. This truth table is just a
system address line A0, so the CE iflputs of these devices compressed form of writing an address decoder work-
will be asserted II A0 is low. The CE inputs of the two sheet such as those we used in the previous discussion
2716sin the upper bank are connected to the BE-IE line. of address decoding. From the truth table you can see
The CE irrni,is of these devices then will be asserted that in order for the 01 output of the 3625 to be asserted
whenever BHE is asserted low. To summarize, then, the low. M/I0 has to be high. This is reasonable, since this
two devices labeled A27 and A36 form the lower bank of decoder is enabling memory devices, not port devices.
EPROMs and the two devices labeled A30 and A37 form Also, address lines Al2 through A19 have to be high in
the , per bank of EPROMs in this system. To see how order for the 01 output of the PROM to be asserted low.
the OE enable input of each of these devices gets asserted Since the upper eight address bits must all be Fs for
and to determine the address that each device will have the 01 output lobe asserted, the lowest address which
PROM INPUTS
______________________ PROM
- OUTPUTS
I A14-A19 A13 I Al2
PROM ADDRESS
01 BLOCK SELECTED
M/IO 04 03 02
1 1 I 1 1 1 1 0 FFXIOH-FFFFFU
1 I 1 I o 1 1 0 I FEOU.FEFErH
1 I I I I 1 0 1 1 FDON-FDEFFH ICSXI
I 0 0 0 1 I I FCOH-FCFFFH ICSYI
ALLOTHEA STATES 1 1 1 I NONE
FIGURE 7-14 Truth table for an SDK . BE. ROM decoder PROM lATh).
FIGURE 7-15 Truth table for an SDK-86 RAM decoder PROM (A29).
and the upper-bank RAMs are enabled. According to the output operations, the 16-bit port address Is first loaded
first line of the PROM truth table In Figure 7-15, 01 and into the DX register with an instruction such as MOV
02 wIll both he asserted low if BHE and AO are both low. DX.OFFF8H. The Instruction IN AL,DX is then used to
Remember from Figure 7-13 that BHE and AO will both copy a byte from port FFF8H to the AL register. MOV
be low whenever you write a word to an even address or DX,0038H followed by IN AL.DX has the same effect as
read a word from an even address. This last case gives IN AL,38H.
the answer to the question we asked earlier about why Whenever the 8086 executes an IN or OUT instruction
A0 and BHE are connected to the address decoder PROM to access a port, none of the segment registers are
Inputs. The two Inputs are required to tell the PROM Involved in producing the physical address sent Out by
decoder to assert both 01 and 02 for a word read or the 8086. The port address Is sent out directly from the
write operation. 8086 on lines ADO—ADI5. and 0's are output on lines
The address range for the A38, A41, A43, and A45 A16-Al9.
RAMs is 000001-i to OO7FFH. Another look at the PROM In an 8086 system which uses direct I/O, the M/lO
truth table in Figure 7-15 should show you that RAMS signal Is used to enable a memory decoder or a port
A39, A42, A44. and A46 Contain 2 Kbvtes more In the decoder. Remember that the MJIO signal being high was
range 00800H to OOFFFH. Again, both banks of this one of the enabling conditions for the SDK-86 ROM and
additional RAM will be enabled If A0 and BHE are both RAM decoders we discussed in previous sections. As you
low, as they are for reading or writing a word to an even wIll see, a low on MJ1O Is used to enable a port decoder.
address, During the execution of an (N instruction, the RD
signal from the 8086 will be low, This signal can be
SDK-86 PORT ADDRESSING used to enable an addressed input port device. During
AND PORT DECODING execution of an OUT instruction the WR signal from the
In a previous section ol this chapter we described 8086 will be low. This signal can be used to enable an
memory-mapped inpuUoutput. In a system with memo- addressed output port device. Since the 8086 outputs
ry-mapped 110, port devices are addressed and selected up to a 16-bit address for direct 110 operations. It can
by decoders as If they were memory devices. The main address any one of 2' or 65.536 input ports and any
advantage of memory-mapped I/O Is that any Instruction one of 65.536 output ports.
which refers to memory can theoretically be used to read For an example of how direct I/O ports are addressed
from or write to a port. Thc single Instruction A1)D and sclectedin a real s ystem, we will again look at the
BH,DS BYTE PTRI437AIII could be used to read a byte SDK-86 schematics In Figure 7-8. sheet 7. Here another
from a memory-mapped port and add the byte read In 3625 PROM. A22, Is used to produce the chip select
to the BH register. The disadvantage of men.ary-rnapped signals for four I/O devices. The 01 output of the PROM
110 Is that the ports occupy part of the system memory Is used to enable the 8279 kevboardidisplay interface
space. This space Is then not available for storing data device, which we discuss in a section of Chapter 9. The
or instructions. 02 output of the PROM Is used to enable the 825 IA
To avoid having to use part of the system memory USART shown on sheet 9 of the schematics. The 825 IA
space for ports, 8086 family microprocessors have a allows communication with other systems in serial form.
separate address space for ports. Having a separate A Section in Chapter 14 discusses the operation of this
address space for ports is called direct I/O because this device. The 03 arid 04 outputs are connected to two
separate address space Is accessed directly with the IN 8255A parallel port des ices, shown on sheet 5 of the
and the OUI Instructions. schematics. These devices can be eoabled individually
Remember from previous chapters that the 8086 IN to Input or output bytes. The y can also be enabled
and OUT instructions each have two forms.Jtxed port together to input or output words A section iii Chapter
arid variable port. For fixed-port instructions, an 8-bit 9 shows you how to tell eac port in these devices
port address is written as part of the instruction. The whether you want it to be an in it or an output.
instruction IN AL,38H, for example, copies a byte from Take a look now .ii the i'25 . idcr PROM to deter-
port 38H to the AL register. For variable-port input or mine what conrtilion' cii ii shoub bud that
fi
Ib
FIGURE 7-16 Truth table and map for SDK-06 port decoder. (a) Truth table. (b) Map.
the CS2 enable input of the PROM will be asserted when selects one Internal address. and Al high selects the
M/lO Is low, as it is during an input or put operation. other Internal address. Al low gives system address
Furthermore, you should see that the CS! input will be FFE8H. and Al high gives system address FFEAH. These
asserted when All to A15 are all high. Now, to see are then the two addresses for the 8279 in this system.
what addresses cause each of the PROM outputs to be According to the truth table in Figure 7-16a. the 02
asserted, refer to thc truth table for the PROM in Figure output ol the decoder PROM will be asserted low when
7!6a. From this figure you can see that to assert the A4 through A15 arc high and A3 and AO arc low. BIlE
01 output low. A5 through A15 have to be high. A4has can be either low or high. but, since only the lower eight
to below. A3 has to be high, and A0 has to be low. I3HE data lines are connected to the 825 IA USART, data must
can be either high or low. Note, however, that only the be sent to or read from the device as bytes at an even
lower eight data lines. DO—D7. are connected to the address. Again, system address line Al is used to select
8279. Therefore, data must be Sent to or read from one of two internal addresses in the 825 IA (Figure 7-8.
the 8279 at an even byte address. In other words, data sheet 9). Al low selects one internal address and Al
must be sent as a byte to an even address or as the lower high selects the other internal address. Therefore, the
byte of a word to an even address. two system addresses for this device arc FFFOU and
The system base address for this device then is FFE8H. FFF2I-f.
System address line Al is connected to the 8279 to select Now, before discussing the 03 and 04 outputs of the
one of two internal addresses in the device. Al low decodcr PROM. we will take a brief look at the two 8255
parallel port devices they enable. These devices are of the data bus at the same time. When the 8086 on an
shown on sheet 5 of the schematics In Figure 7-8. Each SDK-86 board execut,ts the instructIon sequence MOV
of these devices contains three 8-bit parallel ports and DX,FFF8H—lN AX.DX. both A0 and BHE will be low
a control register. System address lines Al and A2 are during the IN instruction. As shown by the fifth line in
used to address the desired port or register in the device. the truth table, this will cause both the 03 and the 04
Just as lower address lines are used to address the outputs of the port decoder to be low. These signals will
desired internal location In a memory device. Note that enable both the A40 and A35 port devices. The byte of
the lower eight data lines. DO—D7. are connected to the data on port 2A will be input to the 8086 on the lower
A40 device, and the upper eight data lines are connected half of the data bus, arid the byte of data on port LA will
to the A35 device. This is done so that you have several be input to the 8086 on the upper half of the data bus.
input or output possibilities. You can read a byte from Note in the truth table in Figure 7-16a that the 3625
or write a byte to an even-addressed port in device A40. PROM decoder will enable a port device only when the
You can read a byte from or write a byte to an odd- specific address assigned to that device is sent out by
addressed port in device A35. You can read a word from the 8086. This is sometimes called comp fete decoding
or write a word to a 16-bit port made up from an 8-bit because all the address lines play a part in selecting a
port from device A40 and an 8-bit port from device A35. device and one of its internal ports or registers. As we
To input or output a word, both devices have to be show In Chapter 8. adding another decoder to produce
enabled. Now let's look at the decoder truth table to enable signals for more port devices is very easy In a
determine what addresses enable the various ports in system which uses this complete decoding.
these devices.
The A40 device will be enabled by the 03 output of THE SDK-86 "OFF-BOARD" DECODER
the 3625 decoder PROM if address lines A3 through A 15 Take a look at the off-board clrcuitrlj in zone AS on sheet
are high and A0 Is low. Al and A2 are used to select 5 of the SDK-86 schematics. The purpose of this circuitry
internal ports of the 8255A. Let's assume that these two is to produce the signal OFF BOARD whenever the 8086
bits are 0 for the first address in the device. To select sends out a memory or port address which does not
the A port in the A40 8255A. address lines Al and A2 correspond to a device decoded on the board, The 'F
have to be low. The system address that will enable this I3OARD signal will be asserted low if pin 4 of the A3
device and select the A port within it is FFF8H. Other NAND gate is low or if pin 5 of the A3 NAND gate is low,
values of A2 and Al will select one of the other ports or According to the truth table for the Al2 PROM in Figure
the control register in this device. Figure 7-16b shows 7-17. the 01 output will be low if the 8086 is doing a
the system addresses for the ports and control register memory operation and the address sent Out is not in
in this 8255. Note that the ports in this device (A40) are one of the ranges decoded for the onboard RAM or ROM.
identified as port 2A, port 2B. and port 2C. These all In order for pin 4 of the A3 NAND gate to be low, both
have even addresses because AO must be low for this pin 9 and pin 10 of the A3 NAND gate must be high.
device to be selected. Pin 10 will be high if the 8086 is doing an Input or
The A35 8255A, which contains port IA. port lB. and output operation llO/M from the 8286 inverting buffer
port IC, will be enabled by the 04 output of the decoder equals I). Pin 9 of the A3 NAND gate will be high if any
PROM if A3 through A15 are high and the BHE line is one of the A19 NAND gate inputs is low. Since system
low. If this 8255A is being enabled for a byte read or address lines A5 through A15 are connected to the
write, then the A0 Line will also be high. A2 and Al are Inputs of the 74LS 133 NAND gate. the signal to pin 9
again used to address one of the ports or the control of A3 will be high for any address less than FFEOH. in
register within the 8255A. A2 0 and Al = I will select other words, pin 4 of the A3 NAND gate will be asserted
port IA in this 8255A. As shown in Figure 7-I6b. then. low for any i/O operation in an address range not selected
the system address for port IA is FFF9H. Port lB will by the A22 port decoder.
be accessed with asystem address of FFFBH. port IC The OFF BOARD signal produced by the previously
will be accessed with a system address of FFFIDH. and discussed PROM and logic gates Is connected to an input
the internal control register will be accessed with a of a NANE) gate labeled A2 on sheet2of the schematics.
system address of FFFFH. If OFF BOARD is asserted low, or INTA Is asserted low.
As we said before. (he 8086 can input a 16-bit value or 1-ILIJA is asserted low, the output of this gate will be
in one operation by enabling a port device on the lower high. For now, all we are Interested in is the fact that If
hail of the data bus and a port device on the upper half OFF BOARD Is asserted low, a high will be applied to
Li
FFFFFH inputs. The 2716 data sheet also gives a chip enable to
output access time, t, of 450 ns. This means that If
an address is already present on the address inputs of
the 2716 and the output buffers are already enabled.
the 2716 will put valid data on its outputs no later than
450 ns after the CE input is asserted low. A third
00003 H
parameter given for the 2716 in the data book Is an
00002 H
output enable to output time, t 0 . of 120 ns maximum.
00001 H
This means that if the device already has an address on
C0000H ItS address inputs, and its CE input is already asserted.
valid data will appear on the output pins at most 120
ns after the OE pin is asserted low.
Now that you have these three parameters for the
FIGURE 7-18 8088 memory structure. 2716. the next step is to check whether each one of
M/IO
j /s7 AJS-A/S
ALE
I AD-AD0
AD
READ CYCLE
(NOTE II
(WR,INTAV0 ,I -
DT/R
DEN
FIGURE 7-19 8086 minimum-mode timing waveforms and parameters. (a) Read
waveforms. (See also next page.)
these times is short enough for the device to work with Now took further to the right on the ADI5-ADO lines,
a 4.9-MHz 8086. In other words, does the 2716 put Out You should see that valid data must arrive at the 8086
valid data soon enough after it is addressed and enabled from memory a time TDVCL before the failing edge of
to satisfy the requirements of the 8086? To determine the clock at the end of T 3 . TDVCL stands for time data
this, you need to look at both the 8086 timing parameters must be valid before clock goes low. The data sheet
and how the 2716 is addressed and enabled on the SDK- gives a value of 30 ns for this parameter.
86 board. The time between the end of the TCLAV interval and
To make it easier for you to find the important the start of the TDVCL interval is the time available for
parameters for these calculations, we show in Figure getting the address to the memory and for the tM of
7l9b a simplified version of the timing diagram in the memory device. You can determine this time by
Figure 7-19a. You should try to do this simplification subtracting TCLAV and TDVCL from the time for three
mentally whenever you are faced with a timing diagram. clock cycles. With a 4.9-MHz clock, each clock cycle
As shown by the timing ,, agram in Figure 7-19b. the will be 204 ns. Three clock cycles then total 612 os.
8086 sends Out MIlO. BI-IE. and an address during T Subtracting a TCLAV of 110 ns and a TDVCL of 30 ns
of the machine cycle. Note on the AD 15—ADO lines of the leaves 472 ns available for getting the address to the
timing diagram that the 8086 outputs this information 2716 and for its t- To help you visualize these times.
within a time labeled TCLAV after the failing edge ol the Figure 7-20a. p. 200, shows this operation in simplified
clock at the start of T TCLAV stands for ttme from diagram form.
clock low to address valid. According to the 8086 If you look at sheets I and3of the SDK-86 schematics,
column of the data sheet shown in Figure 7l9c. the you should see that the BHE signal and the A0—Al I
maximum value of this time is 110 ns. address information go from the 8086 through the
M.'IO
A19S6-A16/53
ALE
AD 1 5—ADO
RD
DT/ R
DEN
(b)
74S373 latches to get to the 2716s. The propagation ECE of the 2716 is 450 ns, you know that this parameter
delay of the 74S373s then must be subtracted from the is also acceptable for an SDK-86 operating with a 4.9-
472 ns to determine how much time is actually available MHz clock.
for the t of the 2716. The maximum delay of a 74S373 The final parameter to check Is t 0 of the 2716.
is 12 ns. As shown in Figure 7-20a. subtracting this According to sheet 1 of the SDK-86 schematics, the OE
from the 472 ns leaves 460 ns for the t of the 2716. signals for the 2716s are produced by the 3625 decoder.
Now, as we told you in a previous paragraph, the 2716 The signals comg to this decoder are Al2 through
has a maximum t . qf 450 ns. Since 450 ns is less than A19, MIlO. and RD. Look at the 8086 timing diagram
the 460 ns available, you know that the t of the 2716 in Figure 7-19b to see if you can determine which of
is acceptable for the SDK86 operating with a 4.9-MHz these signals arrives last at the 3625. You should find
clock. You still, however. must check if the values of tc that addresses and MJlO are sent out during T,. but RD
and t 0 for the 2716 are acceptable. is not sent >ut until T.As indicated by the arrow from
If you look at sheet I of the SDK-86 schematics, you the falling edge of the RD signal, RD going low causes
should see that the CE inputs of the 27 16s are connected the address decoder to send an OE signal to the 2716
either to AU or to L3HE. The timing for these signals is EPROMs. Since RD Is sent out so much later than
the same as that for the addresses in the preceding addresses, it will be the limiting factor for timing. RD
section. As shown in Figure 7-20a. the time available going low and the EPROM returning valid data must
for ke of the 2716 will be 460 ns. Since the maximum occur within the time of states T 2 and T Now, according
TCLRL of 165 ns and the TDVCL of 30 Cs. As shown by TIME AVAILABLE 106 2716
22•_ 03,9
the simple diagram in Figure 72Ob, this leaves 213 ns
(Al
available for the decoder delay and the tOE of the 2716.
Checking a data sheet for the 3625 would show you that fIGURE 7-20 Calculations of maximum allowable access
It has a maximum CS2 to output delay of 30 ns. Subtract times for 4.9-MHz 8086. (a) Time for tACO and 1611 . ( b)
this from the available 213 ns to see how much time is Time for t112;.
left for the t, of the 2716. The result of this subtraction
Is 183 ns.
As we Indicated in a previous paragraph, the 2716
has a maximum t 0 of 120 ns. Since this time is enough time to compensate for the buffer delay, so the
considerably less than the 183 ns available, the 2716 added 2716s will work correctiy.
has an acceptable t<IE value for operating on he SDK-86
board with a 4.9-MHz clock.
All three times for the 2716 are less than those
required by the 8086 for 5-MHz operation, so you know TROUBLESHOOTING A SIMPLE 8086-
that the devices will work correctly at 4.9 MHz without BASED MICROCOMPUTER
Inserting a WAIT state. You could use a logic analyzer
Now that you have some knowledge of the software and
as we described earlier in the chapter to verify the timing
the hardware of a microcomputer system, we can start
on an actual SDK-86 board. teaching you how to troubleshoot a simple microcom-
Here's a final point about calculating the time available
puter system such as an SDK-86 board. For this section
for tACC, t, and t of some device in a system. Suppose
assume that the microcomputer or microprocessor-
that you want to add another pair of 2716 EPROMs in
based instrument previously worked. Later sections of
the prototyping area of the SDK-86 board, and you want
this book will describe how the prototype of a micropro.
to enable the outputs of these added devices with the
cessor-based instrument is developed.
03 output of the 3625 ROM decoder on sheet I of the
The following sections describe a series of steps that
schematics. The timing for these added devices wU be
we have found effective In troubleshooting various mi-
the same as for the previously discussed 27l6s, except
crocomputer systems. The first point to impress on your
that the data from the added devices must come back
mind about troubleshooting a microcomputer is that a
through the 8286 buffers shown on sheet 4 of the SDK-
systematic approach is almost always more effecilve
86 schematics. According to an 8286 data sheet, these
than random poking, probing, and hoping. You don't,
buffers have a maximum delay of 30 ns. This 30 ns
for example, want to spend 2 hours troubleshooting a
must be subtracted from the times available for t, t,
system and finall y find that the only problem is that the
and tOE' If you look back at our calculations of the tIme
power supply is putting out only 3 V instead of 5 V. lJse
available for t in Figure 7-20a. for example, you will
the following list of steps or a list of your own each time
see that we ended up with 460 ns available for t.
you have to troubleshoot a microcomputer: (1) identify
Subtracting the 30 ns of buffer delay from this leaves
the sy mptoms. (2) make a careful visual and tactile
only 430 ns, which Is considerably less than the n6axi-
inspection. (31 check the power supply. (4) do a "signal
mum t M of 450 ns for the 2716. This tells you that,
roll call," (5) s ystematically substitute socketed (Cs, and
because of the buffer delay, the added 271 6s are not fast
(6) troubleshoot soldered-in ICs. The following para-
enough to operate on an SDK-86 board with a 4.9-MHz
graphs describe each step.
clock and no WAIT states. To take care of this problem,
the SDK-86 is designed so that any access to a memory
or t'O device "off board" will cause the selected number Identify the Symptoms
of WAIT atates to he inserted In the machine cycle. For
our example here, selecting one WAIT state with jumper Make a list of the symptoms that you find or those that
W28 on sheet 2 will give another 20' ns for the data to a customer describes to you. Find out, for example,
get from the 2716s to the 8086. This is more than whether the symplom Is present Immediately when the
.1
29. a. Why is the 8086 memory set up as 2-byte-wide 40. Describe how you can keep from mixing up ICs
banks? from a good system with those from a bad system
b. What logic levels would you find on BIlE and when substituting.
A0 when an 8086 is writing a byte to address 41. Write an 8086 rOutine to test the system RAM in
0427411? When ii is writing a word to 04274H addresses 0020011 through O7FFFH.
c Describe the 8086 bus operations required to
write a word to address 04373Ff. 42. Write a test routine to output alternating is and
Os to port FFFAH over and over. With this routine
30. how does the circuitry on the SDK-86 make sure running, you could check with an oscilloscope to
that von cannot accidentally write a byte or word see if the port device is getting enabled and is
to ROM? outputting data.
Most microprocessors allow normal program execution The third source of an interrupt is some error condi-
to be interrupted by some external signal or by a special tion produced in the 8086 by the execution of an
instruction in the program. In response to an interrupt. instruction. An example of this is the divide-by-zero
the microprocessor stops executing its current program interrupt. If you attempt to divide an operand by zero,
and calls a procedure which "services" the interrupt. An the 8086 will automatically interrupt the currently exe-
IRET Instruction at the end of the interrupt-service cuting program.
procedure returns execution to the interrupted program. At the end of each instruction cycle, the 8086 checks
This chapter introduces you to the 8086 interrupt types. to see if any interrupts have been requested. If an
shows you how the microprocessors in the 8086 family interrupt has been requested, the 8086 responds to the
respond to interrupts, teaches you how 10 write inter- interrupt by stepping through the following serie€ of
rupt-service procedures, and describes how interrupts major actions.
are used in a variety of applications.
I. It decrements the stack pointer by 2 and pushes the
flag register on the stack.
5. Describe the operation of an 8259A priority interrupt Figure 8-1, p. 208. summarizes these steps in diagram
controller and write the instructions needed to ini- form. As you can see, the 8086 pushes t he flag register
tialize an 8259A for a specified application. on the stack. dfsable& the INTR input and the single-
step function, and does essentially an indirect far call
6. Call a BIOS procedure using a software interrupt. to the interrupt service procedure. An IRET Instruction
at the end of the interrupt service procedure returns
execution to the main program. Now let's see how the
8086 INTERRUPTS AND 8086 actually gets to the interrupt procedure.
INTERRUPT RESPONSES Remember from Chapter 5 that when the 8086 does
a far call to a procedure. it puts a new value in the code
Overview segmeni register and a new value in the instruction
An 8086 interrupt can come from any one of three pointer. For an indirect far call, the 8086 gets the new
sources. One source is an external signal applied to values for CS and IP from four memory addresses.
the norrrnaskable interrupt INMII input pin or to the Likewise, when the 8086 responds to an Interrupt. It
Interrupt (INTR) input pin. An Interrupt caused by a goes to four memon' locations to get the CS and IP
signal applied to one of these Inputs Is referred to as a values for the Start of the interrupt-service procedure.
hardware Interrupt. In an 8086 s y stem. the first 1 Kbyte of memor . from
A second source of an interrupt is execution of the 00000H to 003FFH. is set aside as a table for storing
Interrupt Instruction. tNT. This is referred to as a the starting addresses of interrupt service procedures.
software interrupt. Since 4 b y tes are required to store the CS and IP values
207
MAINLINE
PROGRAM INTERRUPT for use In more complex microprocessors, such as the
SERVICE 80288, 80386, and 80486. In a later chapter we dlscus.c
PUSH FLAGS EDURE
CLEAR IF PUSH REGISTERS some of these Interrupt types. The upper 224 Interrupt
CLEAR IF types, from 32 to 255. are available for you to use for
PUSH CS hardware or software interrupts.
PUSH IF
FETCH ISR ADDRESS As you can see in Figure 8-2. the vector for each
interrupt type requires four memory locations. There.
POPIP fore, when the 8086 responds to a particular type
I
II Interrupt, it automatically multiplies the type by 4 to
PoPCS
POP REGISTERS produce the desired address In the vector table. It then
RET
goes to that address In the (able to get the starting
FIGURE -1 8086 interrupt response. address of the interrupt service procedure. We will show
you later how you use instructions at the start of your
program to load the starting address of a procedure into
for each Interrupt service procedure, the table can the vector table.
hold the starting addresses for u.i to 256 Interrupt Now that you have an overview of how the 8086
procedures. The starting address of an Interrupt service responds to interrupts, we will discuss one type of
procedure Is often called the interrupt vector or the interrupt in detail and showyou how to write a procedure
interrupt pointer, so the table is referred to as the to service that interrupt.
interrupt-vector table or the interrupt -pointer table.
Figure 8-2 shows how the 256 Interrupt vectors are
arranged In the table In memory. Note that the instruc- An 8086 Interrupt Response Example—Type 0
tlon pointer value is put In as the low word of the vector. Probably the easiest 8086 interrupt to understand It
and the code segment regIster Is put In as the high the divide-by-zero Interrupt, identified as type 0 Ic
word of the vector. Each doubleword Interrupt vector is FIgure 8-2. Before we get into the details of the type C
identified by a number from 0 to 255. Intel calls this interrupt response, let's refresh your memory about how
number the type of the interrupt. the 8086 DIV arid IDlY Instructions work.
The lowest five types are dedicated to specIfic Inter- The 8086 DIV instruction allows you to divide a 16-
rupts, such as the divide-by-zero interrupt, the single- bit unsigned binary number in AX by an 8-bit unsigned
step interrupt, and the nonmaskable interrupt. Later In number from a specified register or memory location.
this chapter we explain the operation of these Interrupts The 8-bit result (quotient) from this division will be left
in detail. Interrupt types 5 to 31 are reserved by Intel in the AL register. The 8-bit remainder will be left in the
Al-I register. The DIV Instruction also allowsyou to divide
a 32-bit unsigned binary number In DX and AX by a 16-
3FFI-4 bit number in a specified register or memory location.
TYPE 255 POINTER: The 16-bit quotient from this division Is left in the AX
3FCH (AVAILABLE)
register, and the 16-bit remainder is left In the DX
4
AVAILABLE INTERRUPT
POINTERS (224) allows you to divIde a 16-bit signed number in AX by an
TYPE 33 POINTER 8-bit signed number In a specified register or a 32-bit
(AVAILABLE) signed number in DX and AX by a 16-bit signed number
TYPE 32 POINTER. from a specified register or memory location.
080H IAVAJ LABLE)
O7FH
lithe quotient from dividing a 16-btt number is too
TYPE31POINTER large to fit in AL or the quotient from dividing a 32-bit
(RESERVED)
number is too large to fit In AX. the result of the division
RESERVED INTERRUPT
POINTERS )27( will be meaningless. A special case of this Is where an
attempt Is made to divide a 32-bit number or a 16-bit
I TYPESPOINTER number by zero. The result of dividing by zero is Infinity
01414 (RESERVED)
(actually undefined), which Is somewhat too large to fit
TYPE 4 POINTER: In AX or AL. Whenever the quotient from a DIV or IDly
010H OVERFLOW operation IS too large to fit in the result register. the
TYPE 3 POINTER. 8086 will automatically do a type 0 interrupl. In response
1-BYTE INT INSTRUCTION
DEDICATED INTERRUPT
to this Interrupt the 8086 proceeds as follows.
TYPE 2 POINTER The 8086 first decrements the stack pointer by 2 and
POINTERS (5) . NON-MASKABLE
008H
copies the flag register to the stack. It then clears IF and
I TYPE 1 POINTER TF. Next, It saves the return address on the stack. To
____________________ 004H SINGLE-STEP
CS BASE ADDRESS1I do this, the 8086 decrements the stack pointer by 2.
TYPE OPOINTER
IPOFFSET '1 j, DIVIDE ERROR pushes the CS value of the return address on the stack.
0001-4 I decrements the stark pointer by 2 again, and pushes
I- 16 BITS the lP value of tile return address on the stack, The
8086 then gets the starting address of the interrupt-
FIGURE 8-2 8086 Interrupt-pointer table.
service procedure from the type 0 locatIons In the
208 CHAPTER FIGHT
interrupt-vector table. As you can see in Figure 8-2. It INITIALIZATION LIST
gets the new value for Cs from addresses 00002H and
00003H and the new value for IP from addresses 00000H REPEAT
Get INPUT VALUE
and 00001 H. After the starting address of the procedure Divide by scale factor
is loaded Into CS and IP. the 8086 then fetches and If result valid THEN
executes the first Instruction of the procedure. store result as scaled value
At the end of the interrupt-service procedure, an IRET ELSE store zero
UNTIL all values scaled
instruction Is used to return execution to the interrupted
program. (a)
• The IRET instruction pops the stored value of IP off Save registers
the stack and increments the stack pointer by 2. It then Set error flag
pops the stored value of CS off the stack and increments Restore registers
Return to mainline
the stack pointer again by 2. Finally, it restores the flags
by popping off the stack the values stored during the Is
Interrupt response and increments the stack pointer by
FIGURE 8-3 Algorithm for divide'by-zero program
2. Remember from the previous paragraph that during example. (a) Mainline program. (b) Interrupt.ser-vice
Its Interrupt response, the 8086 disables the INTR and procedure.
single-step interrupts by clearing IF and TF. If the INTR
Input and/or the trap Interrupt were enabled before the
interrupt, they will be enabled upon return to the program. The flag in this case Is not one of the flags in
interrupted program. The reason for this is that flags the 8086 flag register. The flag here is a bit In a mem-
from the interrupted program were pushed on the stack ory loiation we set aside for this purpose. In the
before IF and TF were cleared by the 8086 in tts interrupt actual program, we give this memory locatIon the name
response, To summarize, then. IRET returns execution BAD..DIV_FLAG. At the end of the interrupt-service
to the Interrupted program and restores IF and TF to procedure, execution is returned to the interrupted
the state they were in before the Interrupt. Now that we mainline program.
have described the type 0 response, we can show you After the divide operatIon in the mainline program.
how to write a program to handle this interrupt. we check the value of the BAD_DIV_FLAG to determine
If the result of the division is valid, If the result of the
An 8086 Interrupt Program Example division was too large. then the 8086 wIll have done a
type 0 Interrupt, and the interrupt . servtce procedure
DEFINING THE PROBLEM AND will have set the BAD_DIV_FLAG to a 1. If the result of
WRITING THE ALGORITHM the division is valid, then the 8086 will not have done
In the last chapter we were working mostly with hard- the type 0 interrupt, and the HAD_DIV_FLAG will be 0.
ware, so Instead of jumping directly into the program, This sequence of operations is repeated until all the
let's use this example to review how you go about writing values have been scaled.
any program.
For the example program here, assume we have four
WRITING THE INITIALIZATION LIST
word-sized hexadecimal values stored in memory. We After you have worked out the data structure and the
want to divide each of these values by a byte-type scale algorithm for a program. the next step is to make an
factor to give a byte-type scaled value. If the result of the initialization list such as the one shown in Chapter 3.
division is valid, we want to put the scaled value in an Here is a list for this program.
array in memory. If the result of the division is invalid
(too large to fit in the 8-bit result register), we want to I. Initialize the Interrupt-vector table. In other words,
put 0 in the array for that scaled value. Figure 8-3 shows the starting address of our type 0 interrupt service
the algorithm for this program in pseudocode. routine must be put in locations 00000FI and
00002H.
As shown in Figure 8-3a. the mainline part of this
program gets each (6-bit value from memory in turn 2. Set up the data segment where the values to be
and divides that value by the 8hii scale factor. If scaled, the scale factor, the scaled values, and the
the result of the division is valid, it is stored in the BAD_DIV_FLAG will be put.
appropriate memory location: else a 0 is stored in the
memory location. Not indicated in the algorithm is how 3. Initialize the' data segment register to point to tile
we determine whether the quotient is valid or not. base addrcss of the data segment containing the
With 8086 family microprocessors, a t ype 0 interrupt values to he scaled.
procedure is a handy way to do this. 4. Set up a stack to store the 11a and return address.
Remember from the preceding discussion that If the
result of the division is too large to fit in the quotient 5. Initialize the stack segment and stack pointer reg-
register. AL. then the 8086 will do a t ype 0 Interrupt isters.
Im mediately aftcr the divide instruction finishes. Figure
6. Initialize a pointer to the start of the data to he
8-3b shows the algorithm for a procedure to service this scaled, a counter to keep track of how many values
type 0 interrupt. The main function of this procedure
is to set ajiag which will be checked by the mainline have been scaled, and a pointer to the Start of the
array where the scaled values are to be written.
FIGURE 8-4 8086 assembly language program for divide-by-zero example. (a) Mainline.
(See also next page.)
Once you have the algorithm and the initialization list when y ou write your own interrupt programs. Also, to
for a program. the ni-st step is to start writing the help refresh your memory of the PUBLIC and EXTRN
instructions for the program. so flow lets look at the directives, we have written the mainline program and
assembl y language program for this problem. I lie interrupt service procedure as two separate assembly
modules.
At the start of the mainline program in Figure 8-4a,
ASSIMBI.Y 1.ANC;UAGE PROGRAM we declare a segme lit named I )ATA for the data that
AND INTERRUP1 PROCEDURE the program will he working with. The WOR[) in this
Figure 84 shows our 8086 assembl y lartguage program statement tells the Iltiker. locator to locate this segment
for I lie mainline arid for the t ype 0 interrupt service on the lirst available even address. The PUBLIC in this
procedure. You can use man y parts from these examples statement tells the linker that this segment can bejoined
together (concatenated) with segments of the same name as public. This means that when the object module for
from other assembly modules. The Input values are this program is linked with some other object module
words, so we use a DW directive to declare these four that declares BAD_DIV_FLAG as EXTRN, the linker will
values. The scaled values will be bytes. so we use a DB be allowed to make the connection. Some programmers
directive to set aside four locations for them. Remember say that the PUBLIC directive "exports" a name or label.
that the DUP(0) In the statement initializes the 4-byte The other end of this export operation is to "import"
locations to all Os. As the program executes, the results labels or names that are defined in other assembly
will be written Into these locations. SCALE_FACTOR modules. For example, the statement EXTRN BAD_
DB 09H Sets aside a byte location for tl'ie number by DIV:FAR in our example program tells the assembler
which we are going to be dividing the input values. The that BAD_DIV is a label of type far and that BAD_DIV ts
advantage of using aDB rather than an EQU directive defined in some other assembly module. The INT_PROC
to declare the scale factor is that with a DB the value of SEGMENT WORD PUBLIC and INT_PROC ENDS state-
the scale factor can be held in RAM, where ii can be ments tell the assembler that BAD_DIV is defined in a
changed dynamically in the program as needed. If you segment named INT_PROC. When the assembler reads
use a statement such as SCALE_FACTOR EQU 09H to these statements. It will make an entry in its symbol
Set a value, you have to reassemble the program to table for BAD_DIV and identify It as external. When the
change the value. object module for this program is linked with the object
Part of the 8086 interrupt response Is essentially a far module for, the program where BAD_DIV Is defincI. the
call to the Interrupt service procedure. In any psogram linker will fill in the proper values for the CS and IP of
that calls a procedure, we have to Set up a stack to Store BAD_DIV.
the return address and parameters passed to and from For the actual instructions of our mainline program.
the procedure. The next section of the program declares we declare a 'ode segment with the statement CODE
a stack segment called STACK_SEG. It also establishes SEGMENT WORD PUBLIC.
a pointer to the next location above the stack with the As usual, at the start of the code segment we use an
statement TOP_STACK LABEL WORD. Remember from ASSUME statement to tell the assembler what logical
the examples in Chapter 5 that this label is used to segments to use for code, data, and stack. After this
initialize the stack pointer to the next location after the come the familiar Instructions for initializing the stack
(op of the stack. segment register. the stack pointer register. and the
The next two parts of the program are necessary data segmcrit register.
because we wrote the mainline program and (he inter- The next tour instructions load the address of the
rupt service procedure as two separate assembly mod- BAD_DIV interrupt-service procedure in the type 0
ules. When the assembler reads through a source pro- locations the interrupt-vector table. We load ES with
gram, It makes a symbol table which contains the 0000 so tiat we can use it as an Imaginary segmc"t
segment and offset of each of the names and labels used absolute ,ddres 00000H. Then we use the staternn
In the program. Thestatement PUBLIC_BAD_DIV_FLAG MOV WC'i ii) PTR ES:0000 OFFSET BAD_DIV to Ioa
tells the assembler to Identify the name BAD_DIV_FLAG the offst )f the interrupt-service procedure in memory
29 8086 INTl KRUPTS ANI) INT'KK'Jrl ArtLILAIIUF4) LI
at 00000FI and 00(X) I H. The statement MOV WORD PTR that It does not load the DS register with a value for the
ES:0000 SEG BAD_DIV is used to load the segment start of that segment. The instructions MOV AX,DATA
base address of BAD_DIV into memory at 00002H and and MOV DS.AX do this In our procedure.
00003H. It is necessary to load the interrupt procedure Finally, we get to the whole point of this procedure
addresses in this way if you are using an SDK-86 bosrd with the MOV BAD_DIV_FLAG,01 instruction. This
or using the MASM and Link programs on an IBM PC- instruction simply sets the least significant bit of the
type machine. memory location we set aside with a DB directive at the
Next, we initialize SI as a pointer to the first input start of the mainline program. Note that in order to
value and initialize BX as a pointer to the first of the access this variable by name, you have to let the assem-
locations we set aside for the 8-bit scaled results. CX is bler know that it Is external, and you have to make sure
initialized as a counter to keep track of how many values that the DS register contains the segment base for the
have been scaled. segment in which BAD_DIV_FLAG Es located.
Finally, after everything is initialized, we get to the To complete the procedure, we pop the saved registers
operations weset out to do. The statement MOVAX.ISII off the stack and return to the interrupted program.
copies an input value from memory to the AX register, The IRET instruction, remember, is different from the
where It has to be for the divide operation. The DIV regular RET instruction in that it pops the flag register
SCALE_FAC'FOR instruction divides the number in AX and the return address off the stack. Note in the program
by 09H, the value we assigned to SCALE...YACTOR in Figure 8-4b that the procedure must be "closed" with
previously with a DB directive. The 8-bit quotient from an ENDP directive, and the segment must as usual be
this division will be put in AL, and the S-bit remainder closed with an ENDS directive.
will be put in AH. If the quotient Is too large to fit in AL. Now let's look back in the mainline to see what it does
then the 8086 wIll automatIcally do a type 0 Interrupt. with this BAD_DIV_FLAG. Immediately after the DIV
For our program here, the 8086 will push the flags on instruction, the mainline checks to see if the BAD_
the stack, reset IF and TF, and push the return address DIV_FLAG is set by comparing it with 01. If the BAD_
on the stack. It will then go to addresses 0000H and DIV_FLAG was not set by the type 0 interrupt-servIce
0002H to get the IP and CS values for the start of procedure, then a Jump is made to the MOV IBXLAL
BAD_DIV. the procedure we wrote to service a type 0 instruction. This instruction copies the result of the
interrupt. It will then execute the HAD_DIV procedure. division in AL to the memory location in SCALED_VAL-
Now let's look at the procedure in Figure 8-4b and see UES pointed to by BX. If BAD_DIV_FLAG was set by a
how it works, type 0 interrupt, then 0 is put in the memory location
The BAD_DIV procc. iure starts h ietflng the assem- In SCALED_VALUES and a Jump will be made to the
bler know that the nanc' BAD_DI\,., t"LAG represents a MOV BAD_DIV_FLAG,00 instruction, which resets the
variable of type byte nd that this variable Is thtined in BAD_DIV_FLAG. Since this Jump passes over the MOV
a segment called DA"A In -a . me other tEXTRr4 assembly ( T3XJ.AL Instruction, the invalid result of the division
module. We also tell the se. 'r that the label BAD_ will not be copied into one of the locations in
DIV should be made available l& ther assembly modules SCALED_VALUES.
(PUBLIC). After putting the scaled value or 0 in the array and
Next, we declare a logical seg'et.0 called INT_PR')(. resetting the flag. we get ready to operate on the next
We could have put this procedure in the segment CODE input value. The ADD Sl.02 instruction increments SI
with the maInline program. Howeer, in system pro- by 2 so that it points to the next 16-bit value in
grams where there are many interrupt-service procc. INPUT_VALUES. The INC BX Instruction points BX at
dures, a separate segment is usually set aside for them. the next 8-bit location in SCALED_VALUES. The LOOP
The statement BAD_DIV PROC FAR identifies the actual instruction after these automatically decrements the CX
start of the procedure and tells the assembler that both register by 1 and, if CX is not then 0, causes the 8086
the CS and IP values for this procedure must be saved. to Jump to the specified label. NEXT.
Now, an Important operation to do at the start of any The preceding section has shown you how to set U
Interrupt-service procedure Is to push on the stack any an interrupt-pointer table, how to write an Interrupt-
registers that are used in the procedure. You can then service procedure, and how the 8086 responds to a type
restore these registers by popping them off the stack 0 interrupt. Now we can discuss some of the other types
just before returning to the Interrupted program. The of 8086 Interrupts.
interrupted program will then resume with its registers
as they were before the interrupt. In the procedure in 8086 Interrupt Types
Figure 8-4b, we saved AX and DS. Since we use the
same data segment, DATA, In the mainline and in the The preceding sections used the type 0 Interrupt as an
procedure, you may wonder why we saved DS. The point example of how the 8086 interrupts function. In this
is that an interrupt-service procedure should be written section we discuss In detail the different ways an 8086
so that it can be used at any point In a program. By can be Interrupted and how the 8086 responds to each
saving the DS value for the Interrupted program. this ol these Interrupts. We discuss these in order. starting
interrupt-service procedure can be used in a program with type 0, so that you can easily find a particular
section (hat does not use DATA as Its data segment. discussion when you need to refer back to it. However,
The ASSUME statement tells the assembler the name as you read through this section. you should not attempt
of the segment to use as a data segment. but remember to learn all the details of all the interrupt types at once.
INTERRUPT
IN PUTS
AOl 07 IR7
INTA tNT
INTA
IN TA
T T T T ITI T, j T3 1'
ALE _,[\
FROM 8086
INTA
F ROM 9086 TO\-_________.__' t_______f -
AO0-AD15 D.OAT
ASCII PORT
KEYBOARD FFF8H 8086 IF 100 characters not read THEN
Read character from port
I ool Ioo I IADO
Mask parity bit
8255 Put character irs array
P2A ________ Increment array pointer
Decrement character count
07 A07
Return
KP ELSE Return
.
characters ha,Ye been read, If CHARCNT Is 0, then Using Interrupts for Counting and Timing
we Just pop the regIsters and return to the mainline
program. If CHARCNT Is not 0, we copy the array pointer
COUNTING APPLICATIONS
from Its named memory location, ASCII_POINTER, to
BX. We then read In the ASCII character from the port As a simple example of the use of an interrupt Input for
that the keyboard Is connected to and mask the parity counting, suppose that we are using an 8086 to control
bit of the ASCII character. The MOV {BXI,AL Instruction a printed . clrcult-board . maklng machine In our comput.
next copies the ASCII character to the memory location erized electronics factory. Further suppose that we want
pointed to by BX. To get the poInter ready for the read to detect each finished board as It comes Out ol the
and store operation, we Increment the stored pointer machine and to keep a count of finished boards so that
with the INC ASCII_POINTER Instruction. Finally, we we can compare this count with the number of boards
restore DX, CX. BX, and AX, and return to the mainline fed in. This way we can determine if any boards were
program. lost In the machine.
Sitting In a HEREJMP HERE loop waiting for an To do this count on an interrupt basis. all we have to
Interrupt signal may not seem like much of an Improve. do is detect when a board passes out of the machine
mcnt over polling the key . pressed strobe. However, in a and send an interrupt signal to an interrupt Input on
more realistic program, the 8086 would be doing many the 8086. The interrupt-service procedure [or that Input
other tasks between keyboard interrupts. With polling. can simply increment the board count stored in a named
the 8086 would not easily be able to do this. memory location.
(b)
FIGURE 8-9 (continued) (b) Interrupt-service procedure.
To detect a board coming out of the machine, we use an response. As we mentioned before, all the type 2 inter-
infrared LED, a phototransistor. and two conditioning rupt-servIce procedure has to do in this case Is incremel:t
gates. as shown In Figure 8-10, p. 220. The LED is the board count in a named memory location and return
positioned over the track where the boards come Out, to running the machine. This same technique can be
and the phototransistor Is positioned below the track. used to count people going into a stadium, cows coming
When no board Is between the LED and the phototransis. in from the pasture, or Just. about anything else you
tor, the light from the LED will strike the phototransistor might want to• count.
and turn it on. The collector of the phototransistor will
then be low, as will the NMI input on the 8086. When a
TIMING APPLICATIONS
board passes between the.LED and the phototransistor.
the light will not reach the phototranststor. and it will In Chapter 4 we showed how a delay loop could be used
turn off. Its collector will go high, and so will the signal to set the time between microcomputer operations. In
to the NMI input of the 8086. The 74LS14 Schmitt the example there, we used a delay loop to take in data
trigger inverters are necessary to turn the slow-risetime samples at 1-ms Intervals. The obvious disadvantage of
signal from the phototransistor collector into a signal a delay loop Is that while the microcomputer is stuck in
which meets the risetine requirements of the NMI input the delay loop, it cannot easily be doing other useful
on the 8086. work, in many cases a delay loop would be a waste
When the 8086 receives the low-to-high signal on Its of the microcomputers valuable time. so We USC an
NMI input. it will automatically do a type 2 interrupt interrupt approach.
+5 V I r- t a I. i
Interrupt Punter Table
•5V
Stack and Stack seyserrt Pointer
1kU Data Segment
Seconds count to 240 decimal
620 Wait br Irsterrutst
?4LS14 74LS14
Ii)
GENERATING AN ACCuRATE TIME BASE 1. The maximum Input clock frequency for the 6253
FUR TIMING INTERRUPTS is 2.6 MHz: the maximum clock frequency for the
The 555 timer that we used for the 4-miri timer tust 8254 ts 8MHz (10 MHz for the 8254-2).
described was accurate enougn for that application, but
2. The 8254 has a read-bock feature which allows you
for many applications--such as a real-time clock-'it is
to latch the count in all the counters and the status
not. For more precise timing, we uauallv use a signal
of the counter at arty point. The 8253 does not have
derived from a crystal-controlled oscillator. The proces.
this read-back feature.
sor clock signal is generated by a crystal-controlled
oscillator. so ft is stable. bu'. thIs signal is onsiously too
To simplify reading of this section. we will refer only to
high in lr . 'qucncy to drive a processor interrupt input the 8254. However, you can assume that the discussion
directly. The solution is to divide the clock signal down
also applies to the 8253 except where we specifically
with an eternal counter device lottie desed frequency
state otherwise.
for the interrupt input. Most microcomputer manu 1ac- As shown by the block diagram of the 8254 In Figure
turers have a compatible device hich can be pro- 8-13. the device contains three 16-bit counters. in some
gramrncd with instructionS o dIvide air Input frequency
ways these counters are similar to the TTL presettable
by any desired number. Besides acting as programmable
counters we reviewed in Chapter 1. The big advantage
frequency dividers, these devices have many lrrportant
of these counters, however, is that you can load a count
uses in microcomputer systems. Thereflire. the next
in them, start them, and stop them with instructions
section describes how art Intel 8251 Programmable
In your program. Such a device is said to be software-
Counter operates. how an 8254 can easil y be added to programmable. To program the device, you send count
an SDK-86 board, arid how an 8254 is used in a variety
bytes and control bytes to the device just as you would
of Interrupt applications. Also in the next section. we
send data to a port device.
use the 8254 discussion to show y ou the general proce- If you look along the left side of the block diagram in
dure [or initializing any of the progi ammable peripheral
Figure 8-13. you will see the signal lines used to Interface
devices we discuss in later chapters.
the, device to the system buses. A little later we show
how these are actually connected in a real system. The
main points for you to note about the 8254 at the
moment are that it has an 8-bit in'terface to the data
8254 SOFTWARE-PROGRAMMABLE bus, it has a CS input which will be asserted by an
TIMER/COUNTER
hecause of the many tasks that they can be used tar in
microcomputer systems. prograinniabte timer/counters
I.—CLKO
are very important for you to learn about. As you read DATA
through the following sections, pay particular attention
to the applications of this device in systems and the
general procedure for initializing a programmable device
BUS
BUFFER
I I
0
I-
COUNTERI.._.GATEO
OUT 0
r NUMBER
JACKNUMBEII +5 +5V 4-5V
A15 1
3433 vc SP/EN V GND
3233 P14 2 15 1 1
A13 3 80
230.J3 7 AO Vt
j1 74LS30 I 181 19
26J3 TT_2P
j.___ 8 I
HY21
12
11
10
DO
01
182
183
20
21
11
02 184 22
22)3 A9 10 8 23
03 185
20J3 V6 04
2433;74Ls2: 186 24
M,10 6 25
V7 D5 IA)
44J3
06 12
74 IS 138 CASO 13
18J3 Al D7
3 CASt 15
1 6J3 IIII - Th__!J G 1 I
6 2 - CAS2
I 4J3 --___-cf'
6 - 17 +5V
Afl 321 INTA INT
433
1233 -
A4 8259A#1 1 28
-- Ill
CS lAB
6J3 27 19
PB IR1
231
00 182 20
431 10
183 21
8J1-- 9 22
02 1R4
831
8 03 IRS 23
10J1
04 186 24
1W 6 25
05 1R7
1431 5 II
06
tail CASO 12
D7
CASt13
4&13- 2 -- CAS2 15
WA
26
I NT INT 17
3831 - 8259A$t2
A2
8J3 - SP/ENGN0
+5 V
24 J6J,4
2:11- Vcc I
— 1WA I
22,— 19
—lAD CIKOI-
3231 - _!J GATEOLJJ +5 V
3Wt2i 21 lic
-f 06 OUTOI-
2BJ12! I
-D5
26J1 41 ItS
—104 CLK1I-
2431 -JD3 GATE1L!i +5 V
2231 !1 02 0UTlL!
2031 7, I
—101 I
tail 81 118
H D0 CLK2I-
19. 116
—fAD GATE2I- +5 V
1 Ii?
•— CS OUT2
201 8254
741S30 AND 74LS27 1A1
V PIN 14 GND
GND = PIN I 112
FIGURE 8-14 Circuit showing how to add an 8254 and 8259A(s) to an SDK-86 board.
address decoder when the device is addressed, and it The right side of the 8254 block diagram in Figure
has two address Inputs, AO and Al. to allow you to 8-13 shows the counter inputs and outputs. You can
address one of the three counters or the Control word apply a signal of any frequency from dc to 8MHz to each
register in the device, of the counter clock inputs, labeled CLK in the diagram.
222 CHAPTER EIGHT
The GATE input on each counter allows you to start or 8-15 shows the system base addresses that will enable
stop that Counter with an external hardMre signal. If each of the 74LS 138 Y outputs. As you will see a little
the GATE input of a counter is high (1), then that later, system address lines Al and A2 are used to select
Counter is enabled for counting. If the GATE input is internal parts of the 8254 and 8259A.
low, the Counter is disabled. The output signal from We connected AO to the C input so that half of the Y
each Counter appears on its OUT pin. Now let's see how outpits will be selected by even addresses and half of
a programmable peripheral device such as the 8254 is the Y outputs will be selected by odd addresses. We did
connected in a system. this so that loading on the two halves of the data bus
will be equal as we add peripheral devices such as the
8254 and 8259A. To see how this works, note that the
System Copnedions for an 8254 Timer/Counter peripheral devices have only eight data lines. For an
8254 is a very useful device to have in a microcom- odd-addressed device we connect these data Lines to the
puter system, but, in order to keep the cost down, the upper eight system data lines, and for an even-addressed
SDK-86 was not designed with one on the board. Figure device, we connect these to the lower eight system data
8-14 shows the circuit connections for adding an 8254 lines. By alternating between odd- and even-selected
Counter and an 8259A Priority interrupt Controlier to outputs as we add peripheral devices, we equalize load-
an SDK-86 board. We discuss the 8259A in a later ing on the bus,
section of this chapter. As shown by the truth table in Figure 8-15. the
If you use wire-wrap headers for connectors Ji and system base address of the added 8254 is FFOIH. Other
J3 on an SDK-86 board, the circuitry shown can easily connections to the 8254 are the system RD and WR lines
be wire-wrapped on the prototyping area of the board. used to enable the 8254 for reading or writing: eight
Install th" WALT-state jumper to insert one WAIT state. data lines, used to send control bytes. status bytes, and
As explained in Chapter 7, a WALT state is needed count values between the CPU and the 8254: and system
because of the added delay of the decoders and buffers. address lines Al and A2, used to select the control
The 74LS 138 in Figure 8-14 is used to produce chip register or one of the three counters In the 8254 , Now
select (CS) signals for the 8254, the 8259A. and any that you see how an 8254 is connected in a system, we
ether L'O devices you might want to add. Let's look first will show you how to initialize an 8254 to do some useful
at the cIrcuitry around this device to determine the work for you.
system base address which selects each device.
In order for any of the oups of the 74LS 138 to be
asserted, the Gi, G2A, and G2B enable inputs must all Initializing an 8254 Programmable
be asserted The G1 Input will be asserted (high) if Peripheral Device
em address lines A5, A6, and A7 are all low. The
G2A input will he asserted (low) if System address lines When the power is first turned on. programmable periph-
A8 through Al5 are all high. As shown by the truth eral devices such as the 8254 are usually in undefined
table in Figure 8-15, these two inputs therefore will be states. Before you can use them for anything, you have
to initialize them in the mode you need for your specific
asserted for a system base address of FFOOH. The G2B
input of the 74LS138 will be asserted (low) if the M/lO application. initializing these devices is not usually
line is low, as It will be for a port read or write operation. difficult, but it is very easy to make errors if you do
Now, remember from Chapter 7 that only one of the not do it in a very systematic way. To initialize any
Y outputs of the 74LS 138 will ever be asserted at a time. programmable peripheral device, you should always
The output asserted is determined by the 3-bit binary work your way through the following series of steps.
code applied to the A. B, and C select inputs. Ii the
circuit in Figure 8-14. we connected system address line I. Determine the system base address for the device.
AO to the C input, address line A4 to the B Input, and You do this from the address decoder circuitry or
address line A3 to the A input. The truth table in Figure the address decoder truth table. From the truth table
FIGURE 8-15 Truth table for 741S138 address decoder in Figure 8-14.
07 06 D5 04 03 02 Dl 00
Al AD SELECTS MO BCD
Sd SCO Awl AWO M2 Ml J
o 0 COUNTER 0
o I COUNTER 1
SC _SELECTCOUNfERI
I 0 COUNTER 2 Sd 5CC
I 1 CONI AOL WORD REGISTER
O 0 SELECTCOUNTERO
(t O 1 SELECT COUNTER I -
1 0 SELECTCOUNTER2 -
SYStEM ADDRESS 2254 PART 1 READ-BACK COMMAND SEE READ OPERATIONSi
1
F F 0 1 COUNTERO
F F 0 3 COUNTER1 RW .- READ/WRITE.
F F 0 5 COUNTER2 AWl RWO
F F 0 7 CONTROL RED COUNTER LATCH COMMAND (SEE READ
0 0 OPERATIONS)
(b)
O I READ/WAITE LEAST SIGNIFICANT BYTE ONLY.
FIGURE 8-16 8254 addresses. (a> Internal. (b) System 1 0 READ/WRITE MOST SIGNIFICANT BYTE ONLY.
- READ/WAITE LEAST SIGNIFICANT BYTE FIRST
I 1 THEN MOST SIGNIFICANT BYTE.
in Figure 8-15. the system base address of the 8254
In our example here Is FF011-I. M - MODE
M2 Ml MO
2. Use the device data sheet to determine the internal 0 MODE 0-' INTERRUPT ON TERMINAL. COUNT
O 0
addresses for each of the control registers, ports.
0 0 I MODE 1 - HARDWARE ONE-SHOT
timers, status registers. etc., in the device. Figure
8-l6a shows the internal addresses for the three X 1 0 MODE2-PuLSEGENERATOR
counters and the control word register for the 8254. X 1 1 MODE 3- SQUARE WAVE GENERATOR
AO in this table represents the AD input of the device. 1 0 0 MODE 4-SOFTWARE TRIGGERED STROBE
and Al represents the Al input of the device. Note 1 0 1 MODES-HAFIDWARETRIGGEREDSTROBE
in (he schematic in Figure 8-14 that system address
line Al Is connected to the AD input of the 8254, OCO:
and system address line A2 is connected to the Al
0[iINARY COUNTER 16-BITS
input. We could not use system address line AO as
BINARY CODED DECIMAL (BCD) COUNTER (4 DECADESt
oae of these because, as described before, we used
system address line AO as one of the inputs to the
NOTE: DON'T CARE BITS (Xl SHOULD BE 0 TO INSURE
address decoder. COMPATIBILITY WITH FUTURE INTEL PRODUCTS.
3, Add each, of the internal addresses to the system
base address to determine the system address of FIGURE 8-7 8254 control word formal. (Intel
Corporation)
each of the parts of the device. You need to do this
so that you know the actual addresses where you
have to send control words, timer values, etc. Figure each resulting half in two, and finally divide each
8-16b shows the system addresses for the three resulting quarter in two.) To help keep track of the
timers and the control register of the 8254 we added
meaning of each bit of a control word, write under
to the SDK-86 board. Note that the addresses all each bit the meaning of that bit. A little later we
have to be odd because the device is connected on show you how to do this br an 8254 control word.
the upper half of the data bus. Documentation of ih(s sort is very valuable when
4. Look in the data sheet for the device for the format you are trying to debug a program or modify an old
of the control word(s) that you have to send to program for sonic new applicalion.
the device to initialize it. For different devices.
6. Finally. send the control words) you have made up
incidentally, the control word(s) may he referred to to the control register address for the device. III (lie
as command words or mode words. To initialize the case of the 8254, you also have to sertd the starting
8254, you send a control word to the control register
count to each of the counter regislers.
for each counter that you want to use. Figure 8-17
shows the format for the 8254 control word.
Now that you have ati overview oi' the initialization
5. Construct the control word required tc initialize the process, let's take a closer look at how y ou do the lasI
device for your specific application. You copstruct two steps for an 8254.
this control word on a bit-by-hit basis. We have A separate control word must be sent for each counter
found it helpful to actually draw the eight little boxes that you want to use in the device. however, according
shown at the top of Figure 8-17 so that we don't to Figure 8-16a. the 8254 has onl y one control register
miss any bits, iAn easc way to draw Ihe eight boxes add less. The trick here is tha I tile COIl I ml words for all
is to draw a long rectangle. divide It ifl hail. divide Ihrcc counters arc .senl to the same address In the
Note that since we set the RW bits of the control word cw 10 LSB 4
for reaWwrtte 'SB only, we do not have to include
instructions to load the MSB of the counter. Pro-
grammed in this way, the 8254 will automatically load
Os in the upper byte of the counter.
If you need to load a count that is larger than I byte.
JSJJ1JL1LJW
GATE
make the RW bits In the control word both l's. Send the
lower byte of the count as shown above. Then send the OUT
high byte of the count to the Count register by adding N 0 0 0 0 0 EF FF
the instructions N N N 4 2 1 0 FF FE
3
MOV AL,HIGI-L..BYTE_OFCOUNT Load MSB of count
CW 10 LSB = 3
OUT DX,AL Send MSB to
count register
Note that the high byte of the count is sent to the same
CL K
address that the low byte of the count was sent.
For each counter that you want to use in an 8254,
you repeat the preceding series of six or eight instruc-
tions with the control word and count for the mode that
GATE I I
you want. Before going on with this chapter. review the our
six Initialization steps shown at the Start of this section
to make sure these are firmly fixed In your mind. In the N I NNN
0 I 0 I 0 I 0 I 0 I 0 1FF I
31 21 2 I 2 I 1 101 FF1
next Section we discuss and show some applications of
the different modes In which an 8254 counter can he CW=10 LSB = 3 LSB2
operated, but we do not have space there to show all the
steps for each of the modes.
C LK
8254 Counter Modes and Applications GATE
As we mentioned previously, an 8254 counter can be OUT I
programmed to operte in anyone of six different modes.
01 0 01 01 01 01FF
The Intel data book uses timing diagrams such as those NNNINIaI2Il2IljoIFF
in Figure 8-18 to show how a counter functions in each
of these modes. Since these waveforms may not be totally NOTE THE FOLLOWING CONVENTIONS APPLY TO ALL MODE
obvious to you at first glance. we will work our way TIMNG DIAGRAMS.
through some of them to show you how to interpret 1. COIJNTERS ARE PROGRAMMED FOR BINARY NOT BCDI
COUNTING AND FOR READING/WRITING LEAST
them. We will also show some uses of the different SIGNIFICANT BYTE (LSBI ONLY. -.
counter mods. As you read through this scetion, don't 2. THE COUNTER IS ALWAYS SELECTED (CS ALWAYS LOW).
try to absorb all the details of the different modes. 3. CWSTANDSFOR"C0NTR0LWORD";CW10MEANS
CONTROL WORD OF 10 HC ISVIRITTEN TO THE COUNTER.
Concentrate on learning to interpret the timing wave- 4. LSB STANDS FOR 'LA ""ICANT BYTE' OF COLNT.
forms and on the different types of output signals you 5. NUMBERS BELOW DIAGRAMS AFIE COUNT VALUES.
THE LOWER NUMBtR IS THE LEAST SIGNIFICANT BYTE.
can produce with an 8254. THE-UPPER NUMBER IS THE MOST SIGNIFICANT SYTE.
SINCE THE COUNTER IS PROGRAMMED TO READ/WRITE
MODE 0—INTERRUPT ON TERMINAL COUNT LSB ONLY. THE MOST SIGNIFICANT BYTE CANNOT BE READ.
N STANDS FOR AN UNDEFINED COUNT.
First read the Intel notes at the bottom of Figure 8-18; VERTICAL LINES SHOW TRANSITIONS BETWEEN
then take a look at the top set of waveforms in the figure. COUNT VALUES.
For this first example, the GA'rE input is held high so
that the counter is always enabled for counting. The MOOED
first dip in the waveform labeled WR represents the FIGURE 8-18 8254 MODE 0 example timuig diagrams.
control word for the counter being written to the 8254. (Intel CorporatIon)
CW - 10 over this dip Indicates that the control word
written is lOt-I. According to the control word format in
Figure 8-17, this means that counter 0 is being initial- the timing diagram. you should see that the count of 4
ized for binary counting, mode 0. and a readlwrite of is transferred into the counter by the next clock pulse
only the LSB. After the control word Is written to the after WR goes high. Each clock pulse after this will
control register. the output pin of counter 0 will go low. decrement the count by I. When the count is clecre-
The next dip in the WR waveform represents a count of mented to 0, the OUT pin will go high. If you write a
4 being written to the count register of counter 0. Before count N to a counter in mode 0. the OUT pin will go
this count can be counted down, it must be transferred high after N + I clock pulses have occurred. Note that
from the count register to the actual counter, llyou look the counter decrements from 0000 to FFFFH on the next
at the count values shown under the ou'r waveform in clock pulse unless you load sonic new count into the
lot, you can use an optical sensor such as the one shown
CW12 LSB3
in Figure 8-10. Each time a car passes through, this
Lfl_fl circuit will produce a pulse. You could connect the signal
from this sensor directly to an interrupt input and we
CLK ..f1J1J1J1JJ1J1J'lJlJlJlJ the processor count interrupts, as we dtd for the printed-.
circuit-board-making machine in a previous example.
GATE - - 111_______ However, the less you burden the processor with trivial
tasks such as this, the more time tt has available to do
complex work for you. Therefore, you let a counter In
OUT
an 8254 count cars and Interrupt the 8086 only when
0000FFOO it has counted 1000 Cars.
INININININI3I2I1I0IFFI3I2I
You connect the output from the optical sensor circuit
to the CLK input of. say, counter 1 of an 8254. You tte
CW12 LSB3 the GATE input of Counter Ito + S V so it will be enabled
for counting and connect the OUT pin of counter 1 to
an interrupt input on an 8259A or the NMI input on the
C LK .....flJlJlJ •'lJlJ• lJ'lJ'lJ.l.flJ.lJ---
8086.
In the mainline program, you initialize counter I for
GATE mode 0, BCD counting, and readiwrlte LSB then MSB
with a control word of 01110001 binary. You want the
OUT
_J I0IOIOl0IOlOI0I
counter to produce an interrupt after 1000 pulses from
the sensor, so you will send a count of 999 decimal to
the counter. The reason that you want to send 999
I N I N I N I NN 1312 1131211101 Instead of 1000 is that, as shown in FIgure 8-18. the
OUT pin will go high N + I clock pulses after the count
CW12 LS82 LS84 value is written to the counter. Since you Initialized the
counter for read/write LSB then MSB. you send 99H and
then 09H to the address of counter I. By Initializing the
CL K ....j1J1J1J1J1JlJ1.flJl.jl.J.1,.f' counter for BCD counting. you can just send the Count
value as a BCD number instead of havtng.to convert it
to hex.
GATE ---_____
The service procedure for this interrupt will contain
instructions which turn on the parking-lot-full sign.
OUT close off the main entrance, and return to the mainline
0 0 0 FF FF 0 0 program. For this example you dort't have to worry that
N N N N N 2 I 0 F FE 4 3 the Counter decrements from 0000 to FFFFH. because
after you shut the gate, the counter will not receive any
MODE 1 more interrupts.
FIGURE 8-19 8254 MODE 1 example timing diagrams.
(Intel Corporation) MODE 1—HARDWARE-RETRIGGERABIE
ONE-SHOT
counter. If the OUT pin is connected to an 8259A IR The basic principle of a one-shot is that when a siga
Input or the NMI interrupt Input of aj 8086. then the is applied to the trigger input of the device, its outpt'
processor will be interrupted when the counter reaches will be asserted. After a fixed amount of time the outpe.
0 Iterminat count). will automatically return to its unasserted state. For
The second set of waveforms in Figure 8-18 shows TFL one-shot such as the 74LS122. the time that t
that lithe GATE input is made low, the counter value output is asserted is determined by the time constao
will be held. When the GATE input is made high again. of a resistor and a capacitor connected to the devhe
the Counter continues to decrement by I for each Clock For an 8254 counter in one-shot mode, the time that
pulse. the output is asserted low Is determined by the frequency
The third set of waveforms in Figure 8-18 shows that of an applied clock and by a count loaded into the
If a new count is written to a counter, the new Count counter. The advantage of the 8254 approach Is that
will be loaded into the Counter on the next clock pulse. the output pulse width can be changed under program
Following clock pulses will decrement the new count control and, if a crystal-controlled clock is used, the
until it reaches 0. output pulse width can be very accurately specified.
As an example 01 what you can use this mode for. Figure 8-19 shows some example timing waveforms
suppose that as one of its jobs you want to use an 8086 for an 8254 counter In mode I. Lets take a look at th,
to control some parking lot signs around an electronics top set of waveforms. Again, the Ilrst dip in the WR
factory. The main parking lot can hold 1000 cars. When waveform represents the control word of 12i1 being s'
it gets full, you want to turn on a sign which directs to the 8254. Use Figure 8-17 to help you determine h'
people to another lot. To detect when a car enters the this control word initializes the device. 'i'au shoulu fl
FILTERED
1N914 2.45 MHZ
J AAA •5V PCuc
OPTICAL 1K
270 t COUPLER 8254
4533
OUT 0
c___ I
POWER L
TRANSFORMER TO 8086 SMI
first. The two dips in the WR waveform represent a the processor clock signal to the CLK Input on one of
control word and the LSB of a count being written to the 8254 counters and tie the GATE input of that Counter
the count register. The next clock pulse after the count high. You initialize that counter for BCD counting, mode
Is written will transfer the count from the count register 2, and read/write LSI3 then MSB. Since you want to
to the actual counter. Since the GATE input is high. divide the 8 MHz by 8000 decimal to get I kHz. you then
succeeding clock pulses will count down this value until write OOH to the counter as the LSB and 80H to the
it reaches 1. When the count reaches 1. the OUT pin, counter as the MSB.
which was previously high. will go low for one clock A question that may occur to you at this point is. How
pulse time. The falling edge of the next clock pulse will do I count seconds if (lie interrupts are coming in every
cause the OUT pin to go high again and the original millisecond? The answer to the question is that you set
count to be loaded into the counter again. Successive aside a memory location as a milliseconds counter and
clock pulses will cause the countdown and load cycle to initialize that location with 1000 decimal (3E8H). The
repeat over and over. If the counter is loaded with a interrupt . servlce procedure decrements this count each
number N. the OUT pin will go low (or one clock cycle time an interrupt occurs and checks to see if the count
every N input clock pulses. The frequency of the output is down toO yet. If the count is not 0. then execution is
waveform then will be equal to the input clock frequency simply returned to the mainline. If the Count is down to
divided by N. 0. 1000 interrupts or I s has passed. The milliseconds
Now, for a specific example, suppose that you want to counter location is then reloaded with 3E8H, and the
produce a l'kHz signal for a real-time clock from an seconds . minutes-hours procedure is called to update
8-MHz processor clock signal. To do this, you Connect the count of seconds, minutes, and hours. An exercise
in the accompanying lab manual gives you a chance to
develop a real-time clock in this way. Incidentally, the
I -kHz Interrupt-service procedure can be used to meas-
CW-14 LS53 ure off several different time intervals that are multiples
of lms.
The middle set of mode 2 waveforms in Figure 8-21
CLK ,J1J1J'lJlJlJ'lJlJlJlJlJ' demonstrates that if the GATE input is made low while
the counter is counting, counting will stop. If the GATE
GATE input is made high again, the original count will be
reloaded into the counter by the next clock pulse.
Succeeding clock pulses will decrement the loaded count.
The bottom set of mode 2 waveforms in Figure 8-21
shows that ifa new count is written to the count register.
this new count will not be transferred to the counter
CW14 LSB3 until the previously loaded count has been decremented
to I.
CLX _f1,fl_f1f1.f1L11..J1.f1_fl.flr
CLK ,...J1J•1J'lflJ•'lJ'l.JlJlJ-l..fl,..J.l..fl.J-
GATE GATE PL__Jl--
OUT
OUT _fJ-
000000FF
IN N IN IN 31 31 31 2 I 1 Ia FF1
CW1A LS83 LSB5
=
CW 1B LSB3 LSB2 W4
CLX ...jiJljlJ1,fl,J'1J'1J'1flfl.J1.J'1..f
C LK .....ffLJ1...J1.fl.J1..f1..Jl.fl..f1J
GATE
GATE jfl
OUT _fLf ouij
0 0 0 0 FF FF 0 0
Li
NIN(NINIIIfjI?IoIFFl N N N N F. 3 2 1 0 FF FE 5 4
MODE 4 MODE 5
FIGURE 8-24 8254 MODE 4 example timing waveforms. FIGURE 8-25 8254 MODE 5 example timing waveforms.
(Intel Corporation) )InteI Corporation)
by the count sequence under the OUT waveform, how- count will not be loaded Into the counter until a new
ever, the count does not get transferred to the counter trigger pulse occurs.
until the GATE (trigger) Is made high. When the trigger
input is made high. the count will be transferred to the USING A NONSYSTEM CLOCK
Counter on the next clock pulse. Succeeding clock pulses WITH AN 8254 IN MODES 2 AND 3
will decrement the counter. When the counter reaches
0. the OUT pin will go low for one clock pulse time. The If you are applying a signal which is not derlvcd from
OUT pin will go low N + 1 clock pulses after the trigger the system clock to the CLK input of an 8254 in mode
input goes high. 2 or mode 3, then a small note in the Intel data sheet
The second set of waveforms In Figure 8-25 shows Indicates that the GATE input of the counter must be
that If another trigger pulse occurs during the count- pulsed low just alter the count Is written to the counter.
down time, the original count will be reloaded on the An easy way to do this is to connect the GATE Input of
next clock pulse and the countdown will start over. The the counter to an otherwise unused output port pin.
OUT pin will remain high until the count is finally You can then pulse the GATE by outputting a low and
counted down, If trigger pulses continue to come before then outputting a high to that port pin.
the countdown Is completed, the OUT pin will continue
to stay high. Therefore, you can use a counter In mode READING THE COUNT FROM AN 8254 COUNTER
5 to produce a power fail signal. as we showed in the
previous discussion of mode I. Note that for mode 5. For many counter applications, we want to be able to
however, the OUT pin will bc high if the power is on and read the current count in the counter. Suppose. for
go low when the power fails. example. that we are using an 8254 counter to count
The bottom Set of waveforms En Figure 8-25 shows the cars coming into a parking lot, as we did In our
that if a new count is written to a counter, the ne' example for mode 0 In an earlier section. In that case
procedure can, at 730 AM., call a procedure which reads 05 0 = LATCh COUNT or SELECTED COUNTERS(S)
in the current count from the counter. Since the Counter 04: 0 LATCh STATUS OF SELECTED COUNTER(S)
was initially loaded with 1000 decimal and is being 03 1 =SELECTCOUNTER2
Counted down as cars come in. we can simply subtract 02: I SELECT COUNTER I
the current count from 1000 to determine how many Dl: 1 SELECTCOUNTEAO
cars have come in. 00: RESERVED FOR FUTURE EXPANSION, MUST BE 0
The Counters in an 8254 have latches on their outputs.
FIGURE 8-26 8254 read-back control word format.
When you read the count from a counter, what you are
actually reading Is the data on the outputs of these
latches. These latches are normally enabled during This method is available in the 8254 but not in the
counting so that the latch outputsjust follow the counter 8253. It is essentially an enhanced version of the counter
outputs. If you try to read the count while the counter latch command approach described in the preceding
is counting, the count may change between reading the paragraphs.
LSB and the MSB. This may give you a strange count. Figure 826 shows the format for the 8254 counter
To read a correct count, then, you must in some way read-back command word. It is sent to the same address
stop the counting or latch the current count on the that other control words are for a particular 8254. The
output of the latches. There are three major ways of l's in bits D7 and D6 identify this as a read-back
doing this. command word. To latch the count on a counter, you
The first is to stop counting by turning off the clock put a 0 in bit D5 of the control word and put a 1 in the
signal or making the GATE Input low with external bit position that corresponds to that counter In the
hardware. This method has the disadvantages that it control word. The advantage of this control word is that
requires external hardware and that a clock pulse which you can latch one, two, or all three counters by putting
occurs while the clock is disabled will obviously not be l's in the appropriate bits. Once a counter is latched.
counted. the count is read as shown in the previous example
The second way of reading a stable value from a program. After being read, the latch outputs return to
counter is to latch the current count with a counter following the counter outputs.
latch command and then read the latched count. A If a read-back command word with bit D4 = 0 is sent
counter is latched by sending a control word to the to an 8254, the status of one or more counters will be
control register address in the 8254. If you look at the latched on the output latches. Consult the Intel data
format for the 8254 control word in F'tgure 8-17. you sheet for further information on this latched status.
should see that a counter latch command is specified by The preceding sections have shown how 8254
making the RWI and RWO bits both 0. The SC! and counters can be used to do a wide variety of tasks around
SCO bits specify which counter we want to latch. The microcomputers. Many of these applications produce an
lower 4 bits of the control word are dont cares" for a interrupt signal which must be connected to an inter-
counter latch command word, so we usually make them rupt input on the microprocessor. In the next section
Os for simplicity. As an exam ple, here is the sequence we show how a prionty interrupt controller device, the
of instructions you would use to latch and read the LSB Intel 8259A, is used to service multiple interrupts.
and MSB from counter I of the 8254 in Figure 8-14. We
assume that the counter was already programmed for
read/write LSB then MSB when the device was initial-
ized. If the counter was programmed for only LSB or 8259A PRIORITY INTERRUPT CONTROLLER
only MSB. then Only that byte can be read.
Previous sections of this chapter show how interrupts
MOVAL.ol000000B Counter I latch command can be used for a variety of applications. In a small
MOV DX,OFFO71-{ Point at 8254 control register system, for example, we might read ASCII characters in
OUT DX.AL Send latch command from a keyboard on an interrupt basis: count interrupts
MOV DX.OFFO3H ; Point at counter 1 address from a timer to produce a real-time clock of seconds,
IN AL.DX Read LSB of latched count minutes, and hours: and detect several emergency or
MOV A1-I,AL Save [SB of latched count job-done conditions on an interrupt basis. Each of these
IN AL,DX Read MSB of latched count interrupt applications requires a separate interrupt
XCI-{G AH,AL Count now in AX input. If we are working with an 8086, we have a problem
here because the 8086 has only two interrupt inputs,
When a counter latch command is sent, the latched NM1 and INTR. If we save NMI for a power failure
count is held until it Is read. When the count is read interrupt, this leaves only one interrupt input for all the
from the latches, the latch outputs return to following other applications. For applications where we have
the Counter outputs. interrupts from multiple sources, we use an external
The third method of reading a stable count from a device called a priority interrupt controller (PlC) to "fun-
counter is to latch the count with a read-back Command. nel" the interrupt signals into a single interrupt input
on the processor. in this section. we show how a common bus allows the 8086 to send control words to the 8259A
PlC. the Intel 8259A, is connected in an 8086 system, and read a status word from the 8259A. The RD and
how it is initialized, and how It is used to handle WR inputs control these transfers when the device is
Interrupts from multiple sources. selected by asserting its Chip select (CS) input low. The
8-bit data bus also allows the 8259A to send interrupt
types to the 8086.
8259A Overview and System Connections Next, in Figure 8-27. observe the eight interrupt
To show you how an 8259A functions In an 8086 system, inputs labeled IRO through 1R7 on the right side of the
we first need to review how the 8086 INTR input works. diagram. If the 8259A is properly enabled, an interrupt
Remember from Figure 8-5 and a discussion earlier in signal applied to any one of these inputs will cause the
this chapter that if the 8086 interrupt flag is set and 8259A to assert its INT output pin high. If this pin is
the INTR input receives a high signal. the 8086 wIll connected to the INTR pin of an 8086 and if the 8086
Interrupt flag Is Set, then this high signal will cause the
1. Send Out two interrupt acknowledge pulses on its previously1escribed INTR response.
INTA pin to the INTA pin of an 8259A PlC. The INTA The INTA input of the 8259A is connected to the INTA
pulses tell the 8259A to send the desired interrupt output of the 8086. T-he 8259A uses the first INTA pulse
type to the 8086 on the data bus. from the 8086 to do some activities that depend on the
mode in whichit is programmed. Wben it receives the
2. Multiply the interrupt type it receives from the 8259A second INTA pulse from the 8086. the 8259A outputs
by 4 to produce an address in the interrupt vector an interrupt type on the 8-bit data bus, as shown in
table. Figure 8-6. The Interrupt type that It sends to the 8086
3. Push the flags on the stack. is determined by the IR input that received an interrupt
signal and by a number you send the 8259A when you
4. Clear IF and IF. initialize it. The point here Is that the 8259A "funnels"
5. Push the return address on the stack. interrupt signals from up to eight different sources into
the 8086 INTR Input, and it sends the 8086 a specitied
6. Get the starting address for the interrupt procedure interrupt type for each of the eight Interrupt inputs.
from the interrupt-vector table and load that address At this point the question that may occur to you is,
in CS and lP. What happens if interrupt signals appear at, for example.
7. Execute the interrupt-service procedure. 1R2 and 1R4 at the same time? In theJixed -priority mode
that the 8259A is usually operated in, the answer to this
Now let's take a little closer look at how the 8259A question is quite simple. In this mode, the IRO input
functions during this process. To statt, study the inter- has the highest priority, the lRl input the next highest.
nal block diagram of an 8259A in Figure 8-27. In the and so on down to lR7, which has the lowest priority.
figure, first notice the 8-bit data bus and control signal What this means is that if two interrupt signals occur
pins in the upper left corner of the diagram. The data at the same time, the 8259A will service the one with
NT
DATA
D7-D0 BUS
BUFFER
CONTROL LOGIC
RD
REAO/ lAO
WRITE Ri
LOGIC
AD - IN INTERRUPT 1A2
SERVICE PRIORITY REQUEST 1R3
REQ RESOLVER REG
(IRA) 1R4
CS ISR
1R5
IRS
Ri
CAS 0
CASCADE
CAS1 BUFFER/
COM PA A A TO R INTERRUPT MASK REQ
CAS2 - IIMRI
SP/EN
the highest priority first, assuming that both inputs are than the highest-priority interrupt currently being ser-
unmasked (enabled) in the 8259A. viced, it will set the appropriate bit In the ISR and
Now let's look again at the block diagiam of the 8259A activate the circuitry which sends a new [NT signal to
In Figure 8-27 so we can explain in more detail how the the 8086. For our example here. lR2 has a higher priority
device will respond to multiple interrupt signals. In the than lR4, so the priority resolver will set bit 2 of the ISR
block diagram note the four boxes labeled Interrupt and activate the circuitry which sends a new INT signal
request register (IRR). interrupt mask register (IMR). to- to the 8086. II the 8086 INTR input was reenabled
service register ([SRI. and priority resolver. with an STI instruction at the start of the iR4 service
The interrupt mask register is used to disable (mask) procedure, as shown in Figure 8-28a. then this new INT
or enable (unmask) individual interrupt inputs. Each signal will interrupt the 8086 again. When the 8086
bit in this register corresponds to the interrupt input sends out a second INTA pulse in response to this
with the same number. You unmask an interrupt input interrupt, the 8259A will send it the type number for
by sending a command word with a 0 in the hit position the lR2 service procedure. The 8086 will use the received
that corresponds to that input. type number to find and execute the lR2 service pro-
The interrupt request register keeps track of which cedure.
interrupt inputs are asking for service. If an interrupt At the end of the lR2 procedure, we send the 8259A
input has an interrupt signal on it. then the correspond. a command word that resets bit 2 of the In-service
ing bit in the interrupt request register will be Set. register so that lower-priority interrupts can be serviced.
After that, an IRET instruction at the end of the lR2
NOTE An interrupt signal must remain high on procedure sends execution back to the interrupted lR4
aniR input until after the falling edge of the first procedure. At the end of the lR4 procedure, we send the
INTA pulse. 8259A a command word which resets bit 4 of the In-
service register so that lower . priority interrupts can be
The inservice register keeps track of which interrupt
inputs are currently being serviced. For each input that
is currently being serviced, the corresponding bit will MAINLINE
INITIALIZE 8259A
be set in the in-service register. UNMASK R2, R4
The priority resolver acts as a "judge" that determines STI 1R4 IR2
if and when an interrupt request on one of theiR inputs PROCEDURE PROCEDURE
gets serviced. ST I
As an example of how this works, suppose Ihat lR2
and lR4 are unmasked and that an interrupt signal
comes in on the 1R4 input. The interrupt request on the
1R4 input will set bit 4 in the interrupt request register. EOI EOI
COMMAND
The priority resolver will detect that this bit IS set and RET
check the bits in the in-service register (ISR) to see if a
higher-priority input is being serviced, If a higher- (a)
priority Input Is being serviced, as indicated by a bit
being set for that input in the ISR, then the priority MAIN
resolver will take no action, If no higher-priority inter- INITIALIZE 8259A
rupt is being serviced, then the priority resolver will UNMASK 1R2. tR4
STI R4
activate the circuitry which sends an inte, t signal PROCEDURE
to the 8086. When the 8086 responds with INTA pulses,
the' 8259A will send the interrupt type that was specified
for the lR4 input when the 8259A was initialized. As we
said before, the 8086 will use the type number it receives
from the' 8259A to find and execute the inierrupt.scrviee EOI COMMAND
IRET
procedure written for the 1R4 interrupt.
Now, suppose that while the 8086 is executing the 52
lR4 Service procedure, an interrupt signal arrives at the PROCEDURE
lR2 input of the 8259A. This will set bit 2 of the interrupt
request register. Since we assumed for this example
that 1R2 was unmasked, the priority resolver will detect
that this bit in the IRR is set and make a decision
EOI COMMAND
whether to send another interrupt signal to the' 8086.
To make the decision, the priority re-solver looks at the
in-scr'ice register. If a higherpriorlty hit in the [SR is
(11)
set, then a higher . priority interrupt is being serviced.
The priority resolver will wait until the higher-priority FIGURE 8-28 8259A and 8086 program flow for lR4
bit in the' ISR is re-set before sending an itlle'rrupt signal Interrupt followed by tR2 Interrupt. (a) Response with
to the 8086 for Ito' new interrupt input. Ii the priority INTR enabled in 1R4 procedure. (b) Response with INIR
resolver finds that the' neW Interrupt has a higher priority not enabled in 1R4 procedure.
ICW 1
AD 07 D6 05 04 03 02 01 DO
0 A7 A6 A5 1 jLTIMf ADI ! SNGL 1C4 I 1 __________________________
___________I i__I 1 CW4 NEEDED
[ ONOICW4NEEDED
1 SINGLE
0 CASCADE MODE
(MCS8O/B5MODEONLyI
AO 01 I CW2
1 A15 A14 A13 Al2 All
,.ç' ,4 / / AlO A9 AB 1 A15-A8OF INTERRUPT
VECTOR ADDRESS
I I I I I I )MCS80185 MODE)
T,-TOF INTERRUPT
ICW3 (MASTER DEVICE) VECTOR ADDRESS
(8086/8088 MODEl
AD 07 06 05 04 03 02 ADE
his, I I S6 S l S .
Dl DO
_____________
Ii = IR INPUT HAS A SLAVE
I
I
I
I
I
I
I
I I I I ES
-JOIRINPUTOOE5NOTHAVEI (SNGL-O)
[ A SLAVE
ICW3 (SLAVE DEVICE)
ICW3
1413 II[ib0uib001sLAvEi
I CW4
AU D7 0:
SFNM BUF !AEOi ___________________
H
ICW4
H READY TO
ACCEPT
INTERRUPT
o x7 NON BUFFERED MODE REQUESTS
[ 1 0 BUFFERED MODE/SLAVE
1 I BUFFERED MODE/MASTER
Ia) (b)
FIGURE 8-29 8259A InIllalizaton command word formats and sending order.
(a) lormals. (b) SendIn rder and requirements. (Intel Corporation)
236 CI-IAPTER Er
initialize it. The sight of all these command words may don't need to send an ICW3. If you are using a slave
seem overwhelming at first, but taken one at a time, 8259A in a system, you have to send an ICW3 to the
they are quite straightforward. To help you see which master to tell it which IR inputs have slaves. The master
Initialization command words (ICWs) are needed for has to be told this so that it knows for which IR input
various 8259A applications. Figure 8-29b shows this in signals it has to send out a slave ID number on the
flowchart form. According to this flowchart, an ICW1 CASO, CAS 1, and CAS2 lines. You have to send an ICW3
and an ICW2 must be sent to any 8259A in the system. to a slave 8259A to give it an ID number. The ID number
If the system has any slave 8259As (cascade mode), then you give a slave is equal to the IR input of the master
an ICW3 must be Sent to the master, and a different that its INT output is connected to. When the master
ICW3 must be sent to the slave. If the system is an 8086 sends out an ID number on the CAS lines, the slave will
or if you want to specify certain special conditions, then recognize Its ID number and opit the desired type to
you have to send an ICW4 to the master and to each the 8086 when it receives an INTA pulse.
slave. Now let's look at the formats for the different For our example here, the only reason we need to send
ICWs, an ICW4 is to let the 8259A know that it is operating in
The first thing to notice about the ICW formats in an 8086 system. We do this by making bit DO of the
Figure 8-29a is that the bit labeled AO on the left end of wor&a 1. Another interesting bit in this command word
each of these Is not part of the actual command word. is Dl, the automatic end-of-interrupt bit. If this bit is
This bit tells you the internal address that the control set in ICW4, the 8259A will automatically reset the in-
word must be sent to. The A0 = 0 next to ICWI. for service register bit for the interrupt input that is being
example, tells you that ICW 1 must be sent to internal responded to when the second interrupt-acknowledge
address 0. which for our 8259A corresponds to system pulse is received. The effect of this is that the 8259A
address FFOOH. will then be able to respond to an interrupt signal on a
The next step in the initialization procedure is to lower-priority IR input. In other words, a lower-priority
make up the control words. The least significant bit of interrupt input could then interrupt a higher-priority
ICW1 tells the 8259A whether it needs to look for an procedure. Since we don't want automatic end of inter-
ICW4 or not. Since we are using the device in an 8086 rupt, the ICW4 for our example here is 00000001.
system, we need to send ICW4. Therefore we make bit In addition to the initialization command words
DO a 1. We only want to use one 8259A for now, so we shown in Figure 8-29a. the 8259A has a second set of
make bit Dl a 1. When used with an $086, bit D2 isa command words called operation command words, or
don't care, so we make it a 0. Bit D3 is used to specify OCWs. These are shown in Figure 8-30.p. 238. An
level-trIggered mode or edge-triggered mode. In level- OCW1 must be sent to an 8259A to unmask any IR
triggered mode, service will be requested whenever a inputs that you want it to respond to. For our example
high level Is present on an IR input. In edge-triggered here, let's assume that we want to use only 1R2 and IR3,
mode, a signl on an IR input must go from low to high Since a 0 in a bit position of OCWI unmasks the
and stay high until serviced. We usually use the edge- corresponding IR input, we put 0's in these two bits and
triggered mode so that a signal such as a square wave is in the rest of the bits. Our OCWI is 111110011.
will not cause multiple interrupts. Making bit D3 a 0 OCW2 is mainly used to reset a bit in the in-service
does this. Bit D4 has to be a 1. For operation in an 8086 register. This is usually done at the end of the interrupt-
system, bits D5, D6. and D7 are don't cares, so we make service proredure. but it can be done at any time in the
them Os for simplicity. Therefore, the ICWl for our procedure. 'l'he effect of resetting the ISR bit for an
example here is 00010011. interrupt level is that once the bit is reset, the 8259A
In an 8086 system, ICW2 is used to tell the 8259A the can respond to interrupt signals of lower priority. In
type number to send In response to an interrupt signal small systems we usually use the nonspecific End-
on the IRO input. In response to an interrupt signal on of-Interrupt command word. The OCW2 for this is
some other IR input, the 8259A will automattcally add 00100000. When the 8259A receives this OCW, it will
the number of the IR input to this base number and automatically reset the in-service register bit for the IR
send the result to the 8086 as the type number for that input currently being serviced. If you want to reset a
input. Because 8086 Interrupt types 0 through 31 are specific ISR bit, you can send the 8259A an OCW2 with
either dedicated or reserved, type 32 idecimal) is the 011 in bits 137. 136, and D5, and the number of the ISR
lowest type number available for us to use. If we send bit you want to reset in the lowest 3 bits of the word.
the 8259A an ICW2 of 00100000 binary or 32 decimal, You can also use OCW2 to tell the 8259A to rotate the
the 8259A will send this number as the type to the 8086 priorities of the IR inputs so that after an IR input is
in response to an !R0 interrupt. For an interrupt request serviced, it drops to the lowest priority. If you are
on the IRI input, the 8259A will send 00100001 binary interested, consult the Intel data sheet for more informa-
or 33 decImal, arid for an interrupt request on the 1R2 tion on this and on the use of OCW3.
input, the 8259A will send an Interrupt type 001000010 Now thai we have made up the required iCWs and
binary or 34 decImal. The same pattern continues for OCWs. the next step is to write the instructions to send
interrupt requesis on the remaining IR inputs. In any these command words to the 8259A.
ICW2 you send the 8259A, the lowest three bits must Figure 8-31. p. 239-40. shows an 8086 assembly
always be Os because the 8259A automatically supplies language program which initializes an 8259A and dem-
these bits to correspond to the number of the IR Input. onstrates many of the concepts of this chapter. You can
Since we are not usmg a slave in this examplr. we use this program as a pattern for writing programs
OCw1
AU 01 DC 05 04 D3 02 Dl DO
Mi [ M6 MS M4 M3 M2 Ml
I MO] TERIPT MASK
AU 07 D6DS D4
OCW3
AU 07 06 D4 D5 03 02 Dl DO
I 0 0 JESMMJ SMM j 0 7 P RR RIS
READ REGISTER COMMAND
oji I
0 0 7
I
IREAD IR flOG ONIREAD IS flOG ON
I
I.
I NOACTION
1NEXTfi5PULSE !NEXTRDPULSE I
iIlroili
which service one or more interrupts. This program that the key-pressed strobe from the ASCII keyboard is
ln)tializes the SDK-86 system in Figure 8-14 for generat- connected to the 1R2 input of the 8259A.
ing a real-time clock of seconds. minutes. and hours In the program. we first declare a segment called
from a l-kl-lz interrupt signal and for reading ASCII AINT_TABLE to reserve space for the vectors to the
codes from a keyboard on an interrupt basis. This interrupt procedures. The statement TYPE_64 DW 2
program assumes that the 2.4576-MHz PCLK signal on DUP(0). for example, sets aside a word space for the
the board Is connected to the CLK Input of the 8254 offset of the type 64 procedure and a word for the
counter 0, the GATE input of the 8254 counter 0 is tied segment base of the procedure. The statement TYPE_65
high. and the OUT pin of counter 0 is connected to the DW 2 DUP(Oi Sets aside a word for the offset of the type
IRO input of the 8259A. The program further assumes 65 procedure and a word space for the segment base
FIGURE 8-31 Assembly language program showing initialization of 8086, 8259A, and 8254 for real-time
clock and keyboard interrupt procedures. (Continued on next page.)
address of the type 65 procedure, etc. As you will soon For the two interrupt . service procedures, we showJust
see, we use program instructions to load the actual the skeletons and the End-of-Interrupt instructions. We
starting addresses of the interrupt procedures in these leave it to you to write the actual procedures. Note that
locations. the interrupt procedures must be declared as far so that
the assembler will load both the IP and the CS values
NOTE: Because of the way the EXE2BIN program in the interrupt-pointer table. Also note the End-of-
works, the AINT_TABLE segment must be first In Interrupt operation at the end of each procedure.
your program so that It will be located at absolute Remember from a previous discussion that when the
address 0000:01001-1, where it must be for the 8259A responds to an lR signal, it sets the corresponding
program to work correctly when downloaded to an bit in the ISR. This bit must be reset at some time
SDK-86 board. during or at the end of the Interrupt-service procedure
so that the priority resolver can respond to future
The next thing we do in our program Is to declare a interrupts of the same or lower priority. At the end of
data segment and Set aside some memory locations for our procedures here we do this by sending an OCW2 to
seconds count. minutes count, hours count, and 100 the 8259A. The OCW2 of 00100000 that we send tells
characters read in from the keyboard. After the data the 8259A to reset the ISR bit for the IR level that Is
segment. we set up a stack segment. currently being serviced. This is a nonspecific End-of-
At the start of the mainline, we Initialize the stack Interrupt (EOl) Instruction.
segment register and the stack pointer register. Then
we initialize the DS register to point to the interrupt-
vector table we set up at the Start of the program. SOFTWARE INTERRUPT APPLICATIONS
The next four instructions load the addresses of the
clock and keyboard procedures in the type 64 and type In an earlier section of the chapter, we described how
66 Locations In the interrupt-pointer table. the 8086 software Interrupt instruction INT N can be
After we load the Interrupt-vector table, we ASSUME used to test any type of interrupt procedure. For example.
DS:DATA and initialize DS to point to the data segment to test a type 64 interrupt procedure without the need
which contains the data for the clock and keyboard. for external hardware such as we described in the
The next step is to Initialize the 8259A as we described preceding section. you can just execute the instruction
in the preceding section. The AU bit next to lCWl in [NT 64.
Figure 8-29 is a 0, so ICW1 Is sent to the lower of the Another important use of software Interrupts is to call
two addresses for the 8259A, FFOOH. For the example Basic Input Output System, or BIOS. procedures in an
here we chose type 64 to correspond to an IRO interrupt. IBM PC-type computer. These procedures in the system
so the needed ICW2 will be 01000000. The A0 bit next ROMs perform Specific input or output functions, such
to ICW2 in Figure 8-29 Is a 1. so ICW2 is sent to the as reading a character from the keyboard, writing some
higher of the two addresses for the 8259A. FFO2H. characters to the CRT. or reading some Information
Likewise, ICW4 and OCW1 are sent to system address from a disk.
FFO2H. To call one of these procedures, you Ibad any required
The next section of the mainline program initializes parameters In specified registers and execute an INT N
counter 0 of the 8254 for mode 3, BCD countdown, and instruction. N In this case is the interrupt type which
read/write LSB then MSB. To produce a 1-kHz signal vectors to the desired procedure. You can read the BIOS
from the 2.4576-MHz PCLK. we then write a count of section of the IBM PC technical reference manual to get
2458 to counter 0. This will not give exactly 1 kHz. but all the details of these if you need them, but here's an
it is as close as we can get with this particular Input example of how you might use one of them.
clock frequency. The PCLK frequency for this board was Suppose that, as part of an assembly language pro-
chosen to make baud rate clock frequencies come out gram that you are writing to run on an IBM PC-type
exact, not a 1-kHz real-time clock. computer. you want to send some characters to the
Finally, after the timer is initialized, we enable the printer. The [NT 17H instruction can be used to call a
8086 INTR input with the STI instruction so that the procedure which will do this.
8086 can respond to tNT signals from the 8259A. Figure 8-32 shows the header for the INt 17H proce-
and wait for an interrupt with the HERE:JMP HERE dure from the IBM PC BIOS listing. Note that the DX,
instruction. .\i1. and AL registers are used to pass the required
Saves the stack pointer value for the last entry at 16. Write the algorithms for a mainline program and
location 8000H. an interrupt-service procedure which generate a
real-time clock of seconds. minutes, and hours in
Saves the contents of memory locations OO100H
three memory locations using a 1-Hz signal applied
through 003FFH after the saved stack pointer value
to the NMI Input of an 8086. Then write the
at the start of the battery-backed memory. (A string
assembly language programs for the mainline and
instruction might be useful for this.)
the procedure. If you are working on an SDK-86
Halts. board, there is a procedure in Figure 9-32 that you
can add to your program to display the time on the
When the power comes back on, the start-up data and address field LEDs of the board. You can
routine can check the power fail flip-flop. If the flip- use this procedure without needing to understand
flop is set, the start-up procedure can copy the saved the details of how it works. To display a word on
data back into its operating locations. initialize the the data field, simply put the wor in the CX
stack segment register, and then get the saved SP register, put OOH in AL. and call the procedure. To
value from address 0800011. Using this value, it display a word on the address field, put the word
can restore the pushed registers and return execu- in CX. OIH in AL. and call the procedure. Hint:
tion to where the power fail interrupt occurred. Clear carry before incrementing a count in AL so
This is called a "warm start." If we dont want it to that DAA works correctly.
do a warm start, we can reset the flip-flop with an
external RESET key so the system does a start from 17. In Chapter 5 we discussed using breakpoints to
scratch, or "cold start." debug programs containing procedures. List the
sequence of locations where you would put
12. a. Why is the 8086 INTR input automatically breakpoints in the example program in Figure 8-9
disabled when the 8086 is RESET? to debug it if it did not work when you loaded it
b. How is the 8086 INTR input enabled to respond into memory.
to interrupts?
c. What instruction can be used to disable the Ii. Suppose that we add another 8254 to the SDK-86
INTR input? add-on circuitry shown in Figure 8-14 and that the
ci. Why is the INTR input automatically disabled CS input of the new 8254 is connected to the Y5
as part of the response to an INTR interrupt? output of the 74LS 138 decoder.
e. How is the INTR input automatically reenabled a. What will be the system base address for this
at the end of an INTR interrupt-service pro- added 8254?
cedure? b. To which half of the 8086 data bus should the
eight data lines from thIs 8254 be connected?
13. Describe the response an 8086 will make if it
c. What will be The system addresses for the three
receives an NMI interrupt signal during a division
counters and the control word register in this
operation which produces a divide . by. zero in-
terru pt. 8254?
ci. Show the control word you would use to initial-
14. The data outputs of an 8-bit analog-to-digital con- ize counter I of this device for readiwrite LSB
verter are connected to bits D0—D7 of port FFF9I-1, then MSB. mode 3. and I3CD countdown
and the end-of-conversion signal from the ND e. Show the sequence of Instructions you would
converter is connected to the NMI input of an 8086. use to write this control word and a count of
Write a simple mainline program and an interrupt- 0356 to the counter.
service procedure which, reads in a byte of data f. Assuming that the GATE input is high. when
from the converter. If tbe MSB of the data is a 0. does the counter start counting down in mode
Indicating that the value is in range. add the byte 3?
The major goal of this chapter and the next Is to show II. Describe how optical encoders are used to deter-
you the citcuitry and software needed to Interface a mine the position, direction of rotation, and speed
basic microcomputer with a wide variety of digital and of a motor shalt.
analog devices. In each topic we try to show enough
detail so that you can build and experiment with these
circuits. Perhaps you can use some of them to control PROGRAMMABLE PARALLEL PORTS AND
appliances around your house or to solve some problems HAN DSHAKE INPUT/OUTPUT
at work.
in this chapter. we concentrate on the devices and Throughout the program examples in the preceding
techniques used to get digital data into and out of the chapters. we have used port devices to input parallel
basic microcomputer. Then, in the next chapter, we data to the microprocessor and to output parallel data
concentrate on analog interfacing. from the microprocessor. Most of the available port
devices, such as the 8255A on the SDK-86 board, contain
two or three ports which can be programmed to operate
in one of several different modes. The different modes
OBJECTIVES allow you to use the devices for many common types of
At the conclusion of this chapter, you should be able to: parallel data-transfer. First we will discuss some of these
common methods of transferring parallel data, and then
I. Describe simple input and output. strobed input we will show how the 8255A is initialized and used In
and output, and handshake input and output. a variety of 110 operations.
2. Initialize a programmable parallel-port device such
as the 8255A for simple Input or output and for Methods of Parallel Data Transfer
handshake input or output. SIMPLE INPUT AND OUTPUT
3. Interpret the timing waveforms for handshake in- When you need to get digital data from a simple switch.
put and output operations. such as a thermostat, into a microprocessor, all you
4. Describe how parallel data is sent to a printer on a have to do is connect the switch to an input port line
handshake basis. and read the port. The thermostat data is always present
and ready, so you can read It at any time.
5. Show the hardware connections and the programs Likewise, when you need to output data to a simple
that can be used to interface keyboards to a micro- display dev1e such as an LED, aU you have to do is
computer. connect the input of the LED buffer on an output port
6. Show the hardware connections and the programs pin and output the logic level required to turn on the
that can be used to interface alphanumeric displays light. The LED is always there and ready, so you can send
to a microcomputer. data to It at any time. The timing waveform In Figure
9-Ia, p. 246. represents this situation. The crossed lines
7, DescrIbe how an 8279 can be used to refresh a on the waveform represent the time at which a new data
multiplexed LED display and scan a matrix key- byte becomes valid on the output lines of the port. The
board. absence of other waveforms indicates that this output
operation is not directly dependent on any other signals.
8. Initialize an 8279 for a given display and keftord
format.
SIMPLE STROBE I/O
9. Show the circuitry used to interface high-power In many applications, valid data is present on an external
devices to microcomputer ports. device only at a certain time, so it must be read in at
10. Describe the hardware and software needed to that time. An example of this is the ASCII-encoded
control a stepper motor. keyboard discussed in Chapter 4. When a key is pressed.
245
read In data only when a strobe pulse tells you that the
DATA
data is valid.
(i Figure 9-lb shows the timing waveforms which repre-
sent this type of operation. The sending device, such as
a keyboard. outpuparallel data on the data lines, and
_____Jf-- then outputs an STB signal to let you know th valid
data is present.
For low rates of data transfer, such as from a keyboard
DATA to a microprocessor. a simple strobe transfer works well.
b) However, for higher-speed data transfer this method
does not work, because there is no signal which tells
the sending device when it is safe to send the next data
STB __ byte. In other words, the sending system might send
data bytes faster than the receiving system could read
them. To prevent this problem, a handshake data
ACK transfer scheme is used.
SINGLE-HANDSHAKE I/O
DATA
Figure 9-2 shows the circuit connections and Figure 9-Ic
shows some example timing waveforms for a handshake
data transfer from a peripheral device to a microproces-
sor. The peripheral outputs some parallel data and sends
STB
f_f an STB signal to the microessor. The microproces-
sor detects the asserted STB signal on a polled or
interrupt basis and reads In the byte of data. Then the
ACK
microprocessor sends an Acknowledge signal lACK) to
the peripheral to indicate that the data has been read
DATA and that the peripheral can send the next byte of data.
From the viewpoint of the microprocessor, this operation
Id) is referred to as a handshake or strobed input.
These same waveforms might represent a handshake
FIGURE 9-1 Parallel data transfer. (a) Simple output. output from a microprocessor to a parallel printer. In
(b) Simple strobe I/O. (C) Single handshake i/O. (d) this case, the microprocessor outputs a character to the
Double handshake i/O. printer and asserts an STB signal to the printer to tell
the printer. "Here is a character for you." When the
printer is ready, it answers back with the ACK signal to
circuitry on the keyboard sends out the ASCII code for tell the microprocessor. "I got that one: send me an-
the pressed key on eight parallel data lines, and then other." We will show you much more about printer
sends out a strobe signal on another line to indicate interfacing In a later section.
that valid data is present on the eight data lines. As The point of this handshake scheme is that the
shown In Figure 4-19. you can connect this strobe line sending device or system is designed so that it does not
to an input port line and poll it to determine when send the next data byte until the receiving device or
you can input valid data from the keyboard. Another system indicates with an ACK signal that it is ready to
alternative, described in Chapter 8. is to connect the receive the next byte.
strobe line to an interrupt input on the processor and
have an interrupt service procedure read in the data DOUBLE-HANDSHAKE DATA TRANSFER
when the processor receives an interrupt. The point For data transfers where even more coordination is
here is that this transfer Is time dependent. You can required between the sending system and the receiving
GROUP
A I/O
PORT C pci-PCI
UPPER
BIDIRECTIONAL (41
DATA BUS
DATA
DI DO BUS
BUrFER 8-BIT
INTERNAL GROUP
DATA BUS 8 I/O
PORT C PC3-PCO
LOWER
(4)
AEAD/
WRITE GROUP GROUP
CONTROL I/O
Al P87-P80
AO
R ESE
in this mode, some of the pins of port C function as board. If port A is Initialized in mode 2, then pins PC3
handshake lines. Pins PCO. PC!. and PC2 function as through PC7 are used as handshake lines for port A.
handshake lines for port BJ1 it is initialized In mode 1. The other three pins. PCO through PC2, can be used for
if port A is initialized as a handshake (mode 1) input 110 if port B is in mode 0. The three pins will be used
port, then pins PC3, PC4, and PC5 function as hand- for port B handshake lines if port B is initialized in
shake signals. Pins PC6 and PC7 are available for use mode I. After you work your way through the mode I
as Input lines or output lines. If portAls Initialized as examples in the following sections, you should have little
then tC pins PC3, PC6, difficulty understanding the discussion of mode 2 in the
and PC7 fctIon as haihkè signâ]: Dort-C- pins Intel data sheet if you encounter it in a system.
1C4 and PC5 are available for use as input or output
lines. Since the 8255A is often used in mode 1. we show
several examples in the foUowlng sections. Constructing and Sending 8255A Control Words
Figure 9-5 shows the formats for the two 8255A control
MODE 2 words. Note that the MSB of the control word tells the
Only port A can be InItialized in mode 2. In mode 2. port 8255A which control word you are sending it. You use
A can be used for bidirectional handshake data transfer. the mode dejinitton control word format in Figure 9-5a
This means that data can be output or Input on the to tell the device what modes you want the ports to
same eight lines. The 8255A might be used in this mode operate In. You use the bit set/reset control word format
to extend the system bus to a slave microprocessor or In Figure 9-Sb when you want to set or reset the output
to transfer data bytes to and from a floppy disk controller on a pin of port C or when you want to enable the
CONTROL WORD
07 106 105 104 103 ID? 101100 GROUP B
PORT C (LOWER)
1 = INPUT
O = OUTPUT
PORT B
07-DO AO-AI I = INPUT
O OUTPUT
•255A
C MODE SELECTION
SI - A
0 MODE 0
1 - MODE 1
'C
HANOSI4AKE j" 'IO j'o
•GRM.0 j'I0
PORT C (UPPER)
INPUT
S P PCI PC? PC? PC' PCS PC6 PC? A f O OUTPUT
1.11111110
P87 -P80 11T8 ISP 1 rtç 1NTR 8
OR OR
fl1
OR
ISF I/O I/O PA7.P*O
OR OR OR
PORT A
INPUT
O = OUTPUT
S S
-- L
I________________________ } BIT #3
word, suppose that you want to output a Ito (set) bit 3 Port A needs to be initialized for handshake input (mode
of port C. which was initialized as an output with the 1) because instruction codes have to be read In from the
mode set control word above. To set or reset a port C tape reader on a handshake basis.
Output f)ifl, you use the bit set/reset control word shown
in Figure 9-5b. Make bit D7 a 0 to identify this as a bit
set/reset control word. and put a I in bit DO to specify
that you want to set a bit of port C. Bits D3. D2. and INTERRUPT
P E DUE ST
Dl are used to tell the 8255A which bit you want to act
on. For this example you want to set bit 3, so you put
PC3 IAOl.
011 in these 3 bits. For simplicity and compatibility IpAIL---1
with future product. make the other 3 bits of the A? 8 LEVEL
IPA2 L— PAPER
control word 0's. Th result. 0000011 lB. is shown with IPA3I___________ TAPE
proper documentation in Figure 9-6b. 1PA41
l'AsI
-lR5 READER
To send this control word to the 8255A. simply load MODE 1 _________
INPUT)
it into AL with the MOV AL.00000I jiB instruction,
point DX at the control register address with the MOV
DX.OFFFEH Instrucuon if DX is not already pointin& ACK
there, and send the control word with the OUT DX.AL STOP/GO
instruction. As part of the application examples in the
8255A
following sections. we will show you how you know
which bit in port C to set to enable the interrupt output MODE 0
signal for handshake data transfer. (INPUT) PC2
OUT OF FLUID
Port C, bit PC6 is used for output of the STOP/GO signal FIGIJRL 9-9 Port C bits to set to enable interrupt
to the tape reader. request outputs for handshake modes.
Port C. bit PCi is not used for this example.
Figure 9-8 shows the control word to initialize the After initializing everything, you would probably read
8255A for these pin functions. You send this word to port C. bits PCO, PCI. and PC2 to check if the lathe was
the control register address of the 8255A as described ready to operate. For any 8255A mode, you read port C
above. by simply doing an Input from the port C address. Then
Before we go on. there is one more point we have to you output a start command to the tape reader on
make about initializing the 8255A for this microcom- bIt PC6. ThIs Is done with a bit set/reset command.
puter-controlled lathe application. in order for the hand- Assuming that you want to reset bit PC6 to start the
shake input data transfer from the tape reader to work tape reader, the bit set/reset control word for this Is
correctly, the interrupt request signal from bit PC3 has. 00001100. When the tape reader receives the Go com-
to be enabled. This is done by sending a bit set/reset mand, it will start the handshake data transfer to
control word for the appropriate bit of port C. Figure 9-9 the 8255A. Lets work our way through the timing
shows the port C bit that must be set to enable the waveforms in Figure 9-10, p. 252. to see how the'data
Interrupt output signal for each of the 8255A handshake transfer takes place.
modes. For the example here, port A is being used for The tape reader starts the process by sending out a
handshake input, so according to Figure 9-9. port C. byte of data to port A on its eight data lines. The tape
bit PC4 must be set to enable the interrupt output for reader then asserts its STB line low to tell the 8255A
this operation. The bit set/reset control word to do this that a new byte of data has been sent. In response, the
is 000010015. You send this bit set/reset control word 8255A raises its Input Buffer Full (IBF) signal on PC5
to the control address of the 8255A. high to tell the tape reader that it is ready for the data.
Handshake data transfer from the tape reader to the When the tape reader detects the ISP signal at a high
8255A can be stopped by disabling the 8255A interrupt level, it raises its STB signal high again. The rising edge
output on port C, pin PC3. Th's Is done by resetting bit of the STB signal has two effects on the 8255A. It first
PC4 with a bit set/reset control word of 00001000. You latches the data byte in the input latches of the 8255A.
will later see another example of the use of this interrupt Once the data is latched, the tape reader can remove the
enable/disable process in Figure 9-16. data byte in preparation for sending the next data byte.
As another example of 8255A interrupt output en- This Is shown by the dashed section on the right side
abling, suppose that you are using port B as a handshake of the data waveform in Figure 9-10. Second. if the
output port. According to Figure 9-9. you need to set, bit interrupt sig output has been enabled, the rising
PC2 to enable the 8255A Interrupt output signal. The edge of the STB signal will cause the 8255A to output
bit set/reset control word to do this is 00000:101. an Interrupt Request signal to the microprocessor on
Now let's talk about how the program for this machine bit PC3.
might operate and how the handshake data transfer The processor's response to the interrupt request will
actually takes place. be to go to an interrupt service procedure which reads
in the byte of data latched In port A. When the RD signal
from the microprocessor goes low for this read of port A,
the 8255A will automatically reset its Interrupt Request
07 06 05 D4 03 02 Dl D
signal on PC3. This Is done so that a second interrupt
1 1 1 0 1 1 1 iloIsIiI cannot be caused by the same data byte transfer. When
the processor raises its RD signal high again at the end
I PORTCLOWERIN of the read operation, the 8255A automatically drops Its
PORT 8 OUTPUT IBF signal on PC5 low again. 1SF going low again is the
PORT B - MODE 0 signal to the tape reader that the data transfer is
PORT C UPPER OUT complete and that it can send the next byte of data. The
PORT A INPUT
PORT A MODE I time between when the 8255A sends the Intemapt
MODE SET WORD Request signal and when the processor reads the data
byte from port A depends on when the processor gets
FIGURE 9-5 Control word to intialize 8255A for around to servicing that interrupt. The point here Is
interface with tape reader and Iahe. that this time doesn't matter. The tape reader will no
FROM 8086 AD
TO 8255
FIGURE 9-10 Timing waveforms for 8255 handshake data input from a tape
reader.
C -nd the next byte of data until It detects that the IBF a printer must he done on a handshake basis because
signal has gone low again. The transfer cycle will then the microcomputer can send characters much faster
repeat for the next data byte. than the printer can print them. The printer must in
After the processor reads in the lathe control instruc- some way let the microcomputer know that its buffer is
tion byte from the tape reader, it will decode this full and that it cannot accept any more characters until
instruction, and output the appropriate control byte to it prints some out. A common standard for interfacing
the lathe on port B of the 8255A. The tape reader then with parallel printers is the Cent ronics Parallel Interface
sends the next instruction byte. If the instructldn tape Standard, named for the company that developed it. In
is made into a Continuous loop, the lathe will keep the following sections, we show you how a Centronics
making the specified parts until it runs out of material, parallel interface works and how to implement it with
The unused bit of port C. PCi, could b. "nnnected to a an 8255A.
mechanism which loads jr. more matr, m'I so the lathe
continue,
The microcomputer-controlled ithe w. htve thrbe'i Centronics Interface Pin Descriptions
here is a small example ( uomated manuiacurrIg. and Circuit Connections
The advantage of this approac th t relieves hum-s Centronics-type printers usually have a 36-pin interface
of the drudgery of standing mi h of a ma-hlne connector. Figure 9-li shows the pin assignments and
continually making the sime part. rh after day, We dscriptions for this connector as it is used in the IBM
hope society can find more productive for the human C printer and the Epson printers. Some manufacturers
time made available. use one or two pins differently, so consult the manual
for your specific printer before connecting it up as we
PARALLEL PRINTER INTERFACE—i-1AN)SI lAKE show here.
OUTPUT EXAMPLE 'ihirty-six pins may seem like a lot of pinsJust to send
At the end of Chapter 8. we showed you how to send a ASCII characters to a printer. The reason for the large
string of text characters to a printer by calling a BIOS number of lines is that each data and signal line has its
procedure with a software Interrupt. In this section own individual ground return line. For example, as
we show you the hardware connections and software shown In Figure 9-il. pin 2 is the LSB of the data
required to interface with a parallel printer in a system character sent to the printer, and pin 20 is the ground
which does not have a BIOS procedure you can call to return for this signal. Individual ground returns reduce
do the Job. the chance of picking up electrical noise in the lines. If
For most common printers, such as the IBM PC you are making an interface cable for a parallel printer.
printers, the Epson dot-matrix printers, and the Pana- these ground return lines should only be connected
sonic dot-matrix printers, data to be printed is sent to together and to ground at the microcomputer end of the
the printer as ASCII characters on eight parallel lines. cable, as shown in Figure 9-12. p. 254.
The printer receives the characters to be printed and While we are talking about grounds. note that pIn 16
stores them in an internal RAM buffer. When the printer is listed as logic ground and pin 17 is listed as chassis
detects a carriage return character IODH). It prints out ground. In order to prevent large noise currents from
the first row of charactès from the print buffer. When flowing in the logic ground wires, these wires should
the printer detects a second carriage return, it prints only be connected together in the microcomputer. (This
Out the second row of characters. etc. The process precaution is necessary whenever you connect any exter-
continues until all the desired characters have been nal device or system to a microcomputer.)
printed. The rest of the pins on the 36-pin connector fall into
Transfer of the ASCII codes from a microcomputer to two categories: signals sent to the printer to tell it what
STROBE puIw to read data in. PuI,. width mss ben ethnO.Siesstr.ceivingtermfrisl. The
1 -
______ 19 SIR IN level is normally 'lsjgh"; reed-in of data ii performed at the "(Ow" livel of this signal.
2 20 DATA1 IN
3 21 DATA2 IN
4 22 DATA3 IN
5 23 DATA 4 IN These tignalt represent information of the let to 8th bits of petellil data r,ectively. Each
24 DATA5 IN t1,Mnduog1 and'iow"wh.nlogical"O."
7 25 DATA6 IN
8 26 OATA7 IN
• 27 DATA8 IN
28 ACKNLG Approeimate!y5pspolw"iratesthetdatahaibeenrecaivsdandth.pnnteri.
OUT
reedy to accept other data.
A "high" signal indicates that the printer cannot receive data. The signal becomes "high"
in the following cases:
Ii BUSY OUT 1. During data envy. - 3. in "offima" dat..
2. During printing operation. 4. Owing printer error satus,
12 30 Pt OUT A"hgh"tign.Iindicete.th.tth.print.risoototpaper.
13 - SLCT OUT Th,ssignaIiatesthatthprinterisintheielictadRate.
14 - AUTO With this ,ign.I being C 'low" level, the pap. ii automatically fed one Iln.alter printing, (The
IN signal level can be fixed to "low"with DIP SW pin 2-3 provided on the control clrcuitboald.)
_________ _________ FEED XI
IS - NC Not oued.
18 - DV Logic GNO level,
17 - CHASIS- Printer chatis GNO. In the printer, the chassis GNO and the logic GNO are ilased front
ONO - eachothar.
18 - NC - Notud. -
19-30 - GND - "1w Ii, etun,' sign.I ONO level.
When the level of this signal b,comes "low" the prInter controller is reset to its initial date
31 - 1Th'f IN - and the print buffer is cleared. This signal is normally at "hig" level, and it. pulls width
must be nor, than 50 ss at the receiving terminal,
The level of this signal becomes "(ow" when the printer is in "Paper End" seats, "Off line"
0 OUT state and 'Error" state.
33 - GNO - San, atwith pin number, 19 to 30.
34 - NC - Notuted.
35 Polled op to +5 Vdc throtgh 4,7 k-ohms resistance.
Data entry to the printer is possibl, only when the level of thu signet is "low." (Intirnel
- SLCT IN IN fising can be carried out with DIP SW 1-8. The condition at the time of thipvn.m is let
___________ ___________ ___________- __________ "low" for thi, aignal,l
Notes: 1. "Direction" refers to the direction of signal flow as viewed from th, printer.
2. "Return' denotes "Twisted-Pair Return" and is to be connected at signal-ground level,
When wir,trg the nterfec., be sure to use a twisted-pair cable for each signal and never fail tø complete connection on the return side. To prevent
noise effectively, these cables should be shielded and connected to the chiuls of the system unit.
3. All interface conditiont are based on TTL level. Both the rise and fall tirnesof each signal most be lela than 02 so'
4. DaIs transfer stunt not be carried Out b y ignoring the ACKNLG or BUSY signal. Data transfer to thu printer can be carried out only after
confirming the ACXNLG signal or when the level of the BUSY signal is "low."l
operation to do, and signals from the printer that 2. The BUSY signal on pIn 11. which Is high If. før
Indicate Its status. The major control signals to the some reason such as being out of paper, the printer
printer are INIT on pin 31, which tells the printer to is not ready to receive a character.
perform its Internal initialization sequence, and
STROBE on pin 1, which tells the printer, "Here Is a 3. The PE signal on pIn 12. winch goes high If the out-
character for you." Two additional input pins, pin 14 of-paper switch In the printer Is activated.
and pIn 36, are usually taken care of inside the printer.
The major status signals output from the printer are 4, The SLCT signal on pin 13, which goes high if the
printer Is selected for receiving data.
I. The ACKNLG signal on p1 n 10. which, when low,
indicates that the data character has been accepted 5. The ERROR signal on pin 32. which goes low for a
and the printer Is ready for the next character. variety of problem conditions in the printer.
ERROR
PE
SLCT OUT
BUSY
NC
PlC
NC
NC
NC
NC
ACKNLG
STB
INIT
DATA 1
DATA 2
DATA 3
DATA 4
DATA 5
- DATA 6
DATA 7
DATA 8
30. 33-RET
LOGIC GND
IC 1 AND 2 ARE 74LS07 CHASSIS GND
Vc -PIN 14
OND PIN 7
hgure 9-13 shows the timing waveforms for transfer- Indication that the printer is ready to accept the next
ring data characters to an IBM printer using the basic character. Some systems use the ACKNLG signal for the
handshakesignals. Here how this works. handshake, and some systems use the BUSY signal.
Assuming the printer has been initialized, the BUSY Now let's see how you can do this handshake printer
signal is checked to see ic the 1 rinter is ready to receive interface With an 8255A.
data. if this signal is low, indicating the printer is ready
(not busy), an ASCII code is sent out on the eight parallel 8255A CONNECTIONS AND INITIALIZATION
data lines. After at least 0.5 p.s. the STROBE signal is
asserted low to tell th printer that a character has been For this example, we disconnected our printer cable
sent. The STROBE signal going low causes the printer from the printer output and connected It to an 8255A
to assert its BUSY signal high. After a minimum time on an SDK-86 board, as shown in Figure 9-12. The
of 0.5 p.s. the STROBE signal can be raised high again. 74L507 open-collector buffers are used on the signal
Note that the data must be held valid on the data lines and data lines from the 8255A because the 8255A
for at ieast 0.5 p.s after the STROBE signal Is made high. outputs do not have enough current drive to charge and
-When the printer is ready to receive the next character. discharge the capacitance of the connecting cable fast
it asserts its ACKNLO_signal low for about 5 s. The enough. Pull-up resistors for the open-collector outputs
rising edge of the ACKNLG signal tells the microcom- of the 74LSO7s are built Into the printer.
puter that it can send the next_character. At the same Port B of the 8255A Is used for the handshake output
time as the rising edge of the ACKNLG signal. the printer data lines. Therefore, as shown in Figure 9-4, bit PCO
also resets the BUSY signal. A low on BUSY is another functions as the Interrupt request output to the 8086.
FIGURE 9-15 Algorithm for printer mainline and interrupt-based printer driver
procedure. (a) Mainline steps. (b) Printer driver procedure steps.
mode control word shown in Figure 9-14a. A bit seti priority interrupts such as a clock can Interrupt this
reset control word is then sent to the 8255A to make procedure. The string address pointer is then read in
the STROBE signal to the printer high, because this is from the control block and used to read a character In
the unasserted level for the signal. When interfacing from the memory buffer to AL. The character in AL is
with hardware, you must always remember to put control then output to port B of the $255A.
and handshake signals such as this in known states. From here on. the program follows the timing diagram
Also, to make sure the printer is internally initialized, in Figure 9-13. After sending the character,_the program
we pulse the INIT line to the printer low for a few waits at least 0.5 p.s. asserts the STROBE input low,
microseconds. waits at least another 0.5 p.s. and raises the STROBE
When we reach a point in the mainline where we line high again. As we said before, the strobe signal
want to print a string, we first read the printer status must be generated with program instructions because
from port A and check if the printer is selected, not the hardware strobe signal generated by the 8255A does
out of paper, and not busy. In a more complete not have the correct timing for this handshake. The
program, we could send a specific error message to data hold parameter in the timing diagram Is satisfied
the display indicating the type of error found. The because the data byte will be latched on the port B
program here Just sends a general error message. If output pins until the next character is sent. Sending of
no printer error condition is found, the starting the character is now complete, so the next step is to get
address of the string of ASCII characters is loaded ready to send another character.
into the control block location set aside for this, and To do this, the buffer pointer in the control block is
the number of characters in the string is sent to incremented by 1. and the characier counter In the
a reserved location in the control block. Finally, the control block is decremented by 1. If the character
interrupt request pin on the 8255A is enabled so that counter is not down to 0, there are more characters to
printer interrupts can be output to the 8259A IR input. send, so the EOI command is sent to the 8259A, the
Note that this interrupt is not enabled until everything registers are popped off the stack, and execution is
else is ready. To see how this algorithm is implemented returned to the mainline to wait for the next Interrupt.
in assembly language, work your way through Figure If the character counter in the control block Is down to
9-1 6a. The JMP wr at the end of this program represents 0. all the characters have been sent, so the Interrupt
continued execution of the mainline program while Request output of the 8255A is disabled with a bit
waiting for an interrupt from the printer. set/reset control word. This prevents further interrupt
A high on the ACKNLG line from the printer will cause requests from the 8255A until we enable it again to send
the 8255A to output an Interrupt Request signal. This another buffer of characters to the printer. Work your
Interrupt Request signal goes through the 8259A to the way through Figure 9-16b to see how this algorithm is
processor and causes it to go to the interrupt service easily implemented. One part of the program that we do
procedure. want to expand and clarify is the generation of the
Figure 9-15b shows the algorithm for the procedure STROBE signal with bit PC3.
which services this interrupt and actually sends the We could use external hardware to "massage" the OBF
characters to the printer. After some registers are signal from the 8255A so it matches the timing and
pushed, the 8086 INTR input is enabled so that higher. polarity requirements of the receiving device. However.
FIGURE 9-16 8086 assembly language program for driver. (a) Mainline.
(Continued)
here we generate thç strobe directly under software is more than enough time for STROBE low. In this case.
control. ________ creating the STRODE signal with software does not use
in the mainline we make the STROBE signal on PC4 much of the processors time, so this is an efikient way
high by sending a bit seUrset control word of 00001001 to do it.
to the control register of the 8255A. In the printer driver
procedure a character is sent to the printer with the OUT
A FEW MORE POINTS ABOUT THE 8255A
DX,AL instruction. According to the timing diagram in
Figure 9-13. we then want to wait at least 0.5 t.s before Before leaving our discussion of the 8255A. we want to
asserting the STROBE signal low. This Is automatically show you a little more about how port C can be used.
done in the program because the instructions required Any bits of port C which are programmed as Inputs
to assert the strobe low take longer than 0.5 i.s. The can be read by simply doing a read from the port C
MOV AL,0000 I 000B instruction requires 4 clock cycles. add,ress. You can then mask out any unwanted bits of
and the OUT DX.AL instruction requires 8 clock cycles the word read In. If port A and/or port B is programmed
to execute. Assuming a 5-MHz clock l0.2-s period). 'in a handshake mode. -theti some of the bits of a byte
these two Instructions take 2.4 j,s to execute, which Is read in from port C represent status information about
more than required. the handshake signals. Figure 9-17. p. 260, shows the
Again_referring to the timing diagram In Figure 9-13. meaning of the bits read from port C for port A and/or
the STROBE time low must also be at least 0.5 u.s. The port B in mode I. Here's how you read this diagram. If
MOV AL,0000IOOW instruction takes 4 clock cycles. port B is initialized as a handshake (mode 1) input port,
and the OUT DX.AL Instruction takes 8 clock cycles. then bits DO. Dl, and D2 read from port C represent
With a 5-MHz clock, this totals to 2.5 p.s. which again the status of the port B handshake signals. Bit D2 will
be hIgh If the port H Interrupt request output has been over and over until you find this bit high. The IBF pin
enabled. Bit D2 is a copy of the level on the input buffer being high means that the input data byte has been
lull (IBF) pin. Bit D3 is a copy of the Interrupt request latched into the 8255A and can now be read. The timing
output, so It will be high it' port B is requesting an waveforms for this case are the same as those in Figure
interrupt. 9-10. except that you are not using the interrupt request
In our previous application examples. we showed how output from the 8255A.
to do handshake data transfer on an interrupt basis to Port C bits that are not used for handshake signals
make maximum use of the CPU time. However, in and programmed as outputs can be written to by
applications where the CPU has nothing else to do while sending bit set/reset control words to the control
waiting to. for example, read in the next character from register. Technically, bits PCO through PC3 can also
some device, then you can save one interrupt input by be written to directly at the port C address, but we
reading data from the 8255A on a polled basis. To do have found It safer to Just use the bit set/reset control
this for a handshake input operatin on port B, you word approach to write to all leftover port C bits
Simply lOOP through reading port C and checking bit Dl programmed as outputs.
-- S-
CGITAL INTERFACING 259
POST C 501 KEY SHEETWITH ROW
o! ________ / CONDuCTORS
107 I 0. 1 05 I I I1 _________
___________________________ / SHEETWITH HOLES
SHEET WITH COLUMN
GSOfrA OSOUPS ________________________ CONOUCTORS
STARS STARS
I4T I*UT
POST POST
KEY CAP
: ________ RETURN SPRING
OUTPUT OUTPUT
POST PLUNGER
FOAM PAD
MOVA8LE PLATE
FIXED PLATES
I c I'f "° I ImTI.I
'z'/VZ /ZZ//Z. . V/'/Z-'/ZZz ./'/ PC BOARD
FIGURE 9-17 8255A status word format for mode 1 (B)
input and output operations.
keyboard Types
REFE RE NC
When you press a key on your computer, you are CURRENT
activating a switch. There are many different ways MAGNETIC
of making these switches. Heres an overview of the FIELD
construction and operation of some of the most common
types. I Ti
FIGURE 9-18 Keyswitch types. (a) Membrane.
MECHANICAL KEYSWITCHES (b) Capacitive. (C) Hall effect.
In mechanical-switch keys, two pieces of metal are
pushed together when you press the key. The actual
switch elements are often made of a phosphor-bronze a key, you push the top ink line through the hole to
alloy with gold plating on the contact areas. The key- contact the bottom ink line. The advantage of membrane
switch usually contains a spring to return the key to the keyboards is that they can be made as very thin, sealed
nonpressed position and perhaps a small piece of foam to units. They are often t'sed on cash registers in fast.
help damp Out bouncing. Some mechanical keyswltches food restaurants: on medical instruments, and in other
now consist of a molded silicone dome with a small piece messy applications. The lifetime of membrane keyboards
of conductive rubber on the underside. When a kejr is varies over a wide range.
pressed, the rubber foam shorts two traces on the
printed-circuit board to produce the Key Pressed signal. CAPACITIVE KEYSWITCHES
Mechanical switches are relatively inexpensive but
they have several disadvantages. First, they suffer frc'm As shown in Figure 9-18b, a capacitive keyswitch has
Contact bounce. A pressed key may make and break two smafl metal plates on the printed-circuit board and
contact several times before it makes solid contact. another metal plate on the bottom of a piece of foam.
Second. the contacts may become oxidized or dirty with When you press the key, the movable plate is pushed
age so they no longer make a dependable . connection. closer to the fixed plate. This changes the capacitance
Higher-quality mechanical switches typically have a between the fixed plates. Sense amplifier circuitry de-
rated lifetime of about 1 million keystrokes. The silicone tects this change in capacitance and produces a logic-
dome type typically last 25 million keystrokes. level signal that indicates a key has been pressed. The
big advantage of a capacitive switch is that it has no
MEMBRANE XEYSWITCHES mechanical contacts to become oxidized or dirty. A small
disadvantage Is the specialized circuitry needed to detect
These switches are really just a special type of mechanical the change in capacitance. Capacitive keyswitches typi-
switch. They consist of a three-layer plastic or rubber cally have a rated lifetime of about 20 million keystrokes.
sandwich, as shown in Figure 9-ISa. The top layer has
a conductive line of silver ink running under each row
of keys. The middle layer has a hple under each key HALL EFFECT KEYSWITCHES
position. The bottom layer has a conductive line of silver This is another type of switch which has no mechanical
ink running under each column of keys. When you press contact. It takes advantage of the deflection of a moving
260 CHAPTER NINE
DETECT
}OE BOUNCE
(a)
and perform the three interfacing tasks with program microcomputer doesn't have to pay any attention to the
Instructions. For systems where the C pu is too busy to keyboard until It receives an Interrupt signal, so this
be bothered doing these tasks In software, an external method uses vel'y little of the microcomputer's time.
device is used to do them. One example of a MOS device The AYS-2376 has a feature called two-key rollover.
which can do this is the General Instrument AYS-2376. This means that ii two keys are pressed at nearly the
whicn can be connected to the rows and columns of a same time, each key will be detected. debounced. and
keyboard switch matrix, The AY5-2376 Independently converted to ASCII. The ASCII code for the first key and
detects a keypress by cycling a low down through the a strobe signal for it will be sent out; then the ASCII
rows and checking the columns just as we did in code for the second key and a strobe signal for it will be
software. When it finds a key pressed, it waits a debounce Sent out. Compare this with two-key lockout, which we
time. If the key is still pressed after the debounce time, described previously in our discussion of the software
the AY5-2376 produces the 8-bit code for the pressed method of keyboard interfacing.
key and sends it out to, for example, a microcomputer
port on eight parallel lines. To let the microcomputer ICATE M CROPROCESSOR
know that a valid ASCII code i on the data lines, he B ARD EN ODERS
AY5-2376 outputs a strobe pulse. The microcomputer Most computers and computer terminals now use de-
can detect this strobe pulse and read in the ASCII code tached keyboards with built-in encoders. Instead of
on a polled basis, as we showed in Figure 4-20. or it can using a hardware encoder device such as the AYS-2376.
detect the strobe pulse on an interrupt basis, as we these keyboards use a dedicated microprocessor. Figure
showed In Figure 8-9. With the interrupt method the 9-21 shows the encoder circuitry for the IBM PC capaci-
+5 VOC ___________________________________________
COl(A011( -
56F1 Lr cc1
P23 'GNO
.SACLOSEO 1C3
n
ciJ 26 VDD P22
SELECT 0 67 'AO -r •'-
56pF T' SELECT I 691
P21 22 CON
40 P20 21
vCC 1c2
J7
87 19 -r 50 pF
2 Xl
LI 27 M000
475H
28 MOOl SENSE B , A9
3 P12 29 M002 SENSE C Al
C5 52
20.1 pP
DIRECTLY DRIVING LED DISPLAYS 1.5 V. The output low voltage for the 7447 isa maximum
Figure 9-24. p. 268. shows a circuit that you might of 0.4 Vat 40 mA, so assume that it Is about 0.2 Vat
connect to a pataliel port on a mkroeornputcr to drive 20 mA. Subtracting these two voltage drops from the
a single 7-segment, common-anode display. For a com- supply voltage of 5 V leaves 33 V across the current-
mon-anode display, a segment Is turned on by applytng limiting resistor. Dividing 3.3 V by 20 mA gives a value
a logic low to it. The 7447 converts a 13C1.) code applid of 168 fI for the current-limiting resistor. The voltage
io its Inputs to the pattern of lows required to dtspla drops across the LED and the output of the 7447 are
the number represented by the I3CD code. This Circuit not exactly predictable, and the exact current through
connection is referred to as a static display because the LED Is not critical as long as we don't exceed Its
current is being passed through the displa y ai all times. maximum rating. Therefore, a standard value of 150 fl
Here's how you calculate the value of the current-limiting is reasonable.
resistors that have to be connected in series with eact'
segment. SOFTWARE-MULTIPLEXED LED DISPLAYS
Each segment requires a current olhetwcen 5 and 30 The Circuit in Figure 9-24 works well for driving just
mA to light. Let's assume you want a current of 20 mA, one or two LED digits with a parallel output port
The voltage drop across the LED when It Is lit is about However, this scheme has several problems if you want
02
03 NC-
04 iNC!!
05 c il_2
(6
D6 OP --
(DECIMAL
POINT) MSD LW
NC
1500 1500 1500 15Ofl 1S0 0 1500
OUTPUT
PORT
B 1(12( (ii) (10) 1(9) 1(15) i"
+5 V +5 V
DO vcc LI
1447 RBO
Di
GND RB1
02 COMMON ANODE
DISPLAYS - DL 707
03 Ri-i 1 kO
01-7 2N3906
the 22-pI capacitor between +5 V and ground at the Note that the 22-fl current-limiting resistors In series
top of the schematic. This is necessary to fIlter Out with the segment lines are much smaller in value than
tra'islents caused by switching the large currents to the those we calculated for the static circuit in Figure
LEDs off and on. 9-24. There arc two reasons for this. First, there is an
The segments of each digit are all connected on a additional few tenths of a volt drop across the transistor
common bus. Since these are common-anode displays, switch on each anode. Second. when multiplexing dis-
a low is needed to turn on a segment. Now let's look at plays. we pass a higher current through the displays so
sheet 7 in Figure 7-8 to see how these displays are that they appear as bright as they would if they were not
driven. multiplexed. Here's how the 8279 keeps these displays
The drive for the digit-switch transistors comes from refreshed.
a 7445 BCD-to-declmal decoder. This device is also The 8279 contains a 16-byte display refresh RAM.
known as a one-of-ten-l')w decoder. When a 4-bit BCD When you want to display some letters or numbers on
code is applied to the inputs of this device, the output the LEDs. you write the 7-segment codes for the letters
corresponding to that BCD number will go low. For or numbers that you want displayed to the appropriate
example, when the 8279 outputs 0100 or BCD 4, the location in this display RAM. The 8279 then automati-
7445 output labeled 04 will go low, In the mode used cally cycles through sending out one of the segment
for this circuit, the 8279 outputs a continuous count codes, turning on the digit for a short time and then
sequence from 0000 to liii over and over. This causes moving on to the next digit. The top five lines in Figure
a low to be stepped from output to output of the 7445 9-26, p. 270. show this multiplex operation in timing
in ring ccunter fashion, turning on each LED digit in dtagram form.
turn. Only one output of the 7445 will ever be low at a The 8279 first outputs the binary number for the first
time, so only one LED digit will be turned on at a time. digit to the 7445 on the SLO to SL3 lines (Figure 7-8.
The segment bus lines for the displays are connected sheet 7) to turn on the first of the digit-driver transistors.
to the A3--AO and B3—B0 outputs of the 8279 through The lines SLO and SLI in Figure 9-26 represent the SLO
some high-current inverting buffers In the ULN2003A. and SL1 lines from the 8279. During this time. the 8279
DIGITAL iNTERFACING 26
PRESCALER PROGRAMMED FOR IN-
TERNAI. FREQUENCY 100 EH SO
510
511
*0 - *3
ACTIVE HIGH
80-83
ACTIVE HIGH
RIO- Rh
FIGURE 9-26 8279 display refresh timing and keyboard scan timing. (Intel
Corporation)
outputs on the A3—A0 and 3—BO segment lines a code the microprocessor. As we will show you later, the 8279
which turns off all the segments. For the circuit in can be connected and initialized to refresh a wide variety
Figure 7-8. sheet 7, this blanking code will be all zeros of display configurations.
(OOH). The display is blanked here to prevent "ghosting" The 8279 can also automatically perform the three
of information from one digit to the next when the digit tasks for interfacing to a matrix keyboard. Remember
strobe is switched from one digit to the next. from previous discussions that the three tasks involve
After about 70 ps. the 8279 outputs the 7-segment putting a low on a row of the keyboard matrix and
code for the first digit on the A3—A0 and 83—BO lines. checking the columns of the matrix, if any keys are
This will light the first digit with the desired pattern. pressed in that row, a low will be present on the column
After 490 ps. the 8279 outputs the blanking code again. which contains the key because pressing a key shorts a
While the displays are blanked, the 8279 sends out the row to a column. If no low- is found on the columns, the
BCD code for the next digit to the 7445 to enable the low is stepped to the next row and the columns checked
driver transistor for digit 2. it then sends out the 7- again. If a low is found on a column, then, after a
segment code for digit 2 on the A3—A0 and B3—B0 lines, debounce time, the column is checked again. If the
This lights the desired pattern on digit 2. After 490 ps, keypress was valid, a compact code representing the key
the 8279 blanks the display again and goes on to digit is constructed. Take a look at the circuit on sheet 7 of
3. The 8279 steps through all the digits and then returns Figure 7-8 to see how an 8279 can be connected to do
to digit I and repeats the cycle. Since each digit requires this.
about 640 ps, the. 8279 gets back to digit I after about When connected as shown in Figure 7-8. sheet 7. the
5.1 ms for an 8-digit display and back to digit 1 after 74LS156 functions as a one-of-eight-low decoder. In
about 10.3 ms for a 16-digit display. The time it takes other words. If you apply OIl. the binary code for 3. to
to get back to a digit again is referred to as the scan its inputs, the 74LS156 will output a low on its 2Y3
time. output. Now remember from the discussion of 8279
The point here Is that once you load the 7-segment display refreshing that the 8279 is outputting a continu-
codes into the internal display RAM, the 8279 automati- ous counting sequence from 0000 to 1111 on its SL0-
cally keeps the displays refreshed withnllt any help from SL3 lines. Applying this count sequence to the Inputs
F(GURE 9-28 8279 command word (ormats and bit descriptions, (Intel CorporatIon)
Iii_ij
8 L.-...
word simply represent the binary number you want to
divide the applied clock by. For example, if you want to
divide the input clock frequency by 24. you send a
control word with 001 in the upper 3 bits and 11000 in
th' lower 5 bits.
flie final control word needed for basic initialization
RAM
LOCATION Is the clear word. You need to send this word to tell the
8279 what code to send to the segments to turn them
o (R) DISPLAY POSITION
off while the 8279 is switching from one digit to the
next. (Reter to Figure 9-26 and its discussion.) In
___ [If_I Ribicidi
addition to telling the 8279 what blanking character to
(d) use during refresh, this control word can be used to
clear the display RAM and/or the FIFO at any time. For
5 (A) REPRESENTS now we are only concerned with the first function. The
i SEGMENT
6 CODE FOO A lower 2 bits, labeled C0 in the control word in Figure
7 9-20, specify the desired blanking code. The requIred
8
c..rdc will depend on the hardware connections in a
particular system. For the SDK-86 a high from the 8279
lb turns on a segment, so the required blanking code is all
Os. Therefore you can put 0's In the 2 C 0 bits. The
FIGURE 9-29 8279 RAM and display location
resultant control word is 11000000.
relatiorlships. (a) teft entry. (b) Right entry.
Fhe three control words described so far take care of
the basic initialiaation However, before you can send
scanning a keyboard Or turning on digit drivers, you codes to the internal display RAM, you l'iave to send the
need a pattern of stepping lows. In encoded mode the 8279 a write-display-RAM control word. This word tells
8279 puts o'rt a binary Count sequence on its SLO--SL3 the 8279 that data sent to the data address laler should
scan lines, an I ar external decoder such as the 7445 IS be pn.it in the display RAM. and it tells the 8279 where
used to prodc- ie stepping lows. If you have only 4 'o put the data in the display RAM. The 8279 has an
digits to refresh, you .an program the 8279 in decoded internal 4-bit pointer to the display RAM. The lower 4
mode. in this mode, the 8279 directly outputs stepping bits of the write-display-RAM control word initialize the
lows on the four scan lines. The second choice you have pointer to the location where you want to write a data
to make for this control word is whether you want two- byte in the RAM. If you want to write a data byte to the
key lockout or N-key -otlover. in the two-key mode, one first location in the display RAM, for example, you put
key must be released before another keypress will be 0000 in these bits. If you put a I in the auto increment
detected and processed. In the N-key rollover mode. if bit, labeled Al in the figure. the internal pointer will be
two keys are pressed at nearly the some time, both autoniatIcaily Incremented to point to the next RAM
keypresses will be detected and debounced and their location after each datt byte is written. To start loading
codes put In the FIFO RAM in the order the keys were charactei-s in the first location in the RAM and select
pressed. auto increment, then, the control word is 10010000.
In addition to being used to scan a keyboard, the 8279 Figure 930. p. 274. shows the sequence of instruc-
can also be used to scan a matrix of switch sensors, tions to send the conf'rol words we have developed here'
such as the metal strips and magnetic sensors you see to the 8279 on the SDK-86 board. Also shown are
on store windows and doors. In sensor matrix mode. instructions to send a 7-segment code to the first
the 8279 scans all the sensors and stores the condition location in the display RAM. Note that the control words
of up to 64 swItches In the Flf'O RAM, lithe condition are all sent io the control address. FFEAH. .1 toe
of any of the switches changes. an IRQ signal is Sent character going to the display RAM is sent to the data
out on the IRQ pin. An interrupt service procedure can address. FFE8H. Also note from sheet 7 of Figure 7-8
then sound an alarm and let the guard dogs loose. The that the DO bit of the byte sent to the display RAM
return lines of the 8279 can also function as a strobed corresponds to segment output HO. and D7 of the byte
Input port In much the same way as port A or B on an sent to the display corresponds to segment output A3
8255A. This is important to know when you are making up a
The SDK-86 initializes the 8279 for eight-character table of 7-segeent codes to send to the 8279.
display, left entry, encoded scan. two-key lockout. Sec if You now know how to initialize an 8279 and send
.
LCD OPERATION
Liquid crystal displays are crcted by sandw:.iing a
thin (10 to 12-ii.m) layerofa lIqourystai fluid
Figure 9-34 shows how an Intersil ICM72 11 M can be
"on.neted to drive a 4-digit. rionmultipiexed. 7-segment
.
voltage is applied between a segment and the backpane.
an electric field is created in the regior under the
egment. This electric field changes the transmission of
1
gn through the region under the segment fiho
There are two commonly available types of LC.':
namtc scattering andfie(d-effeet. The dynamic scaiter- m
Ing type scrambles the molecules where the field is /c\
-sent. This produces an etched-glass-looking light
uacter on a dark background. Field-effect types use
* brization to absorb light where the electric field is
ACTIVE SEGMENT BACKPL*NE
'sent. This produces dark characters on a Silver-gray
Lkground. ()
Most LCDs require a voltage of 2 or 3 V between the
backplane and a segment to turn on the segment. You
can't, however, Just connect the backplane to ground
and drive the segments with the outputs of a TTL
OFF-SEGMENT '
--lJ--1J 1J-
ITJTLr'J-
decoder, as we did the static LED display in Figure
v
9-24. The reason for this is that LCDs rapidly and BACKPLANE
ir;eversibly deteriorate if a steady dc voltage of more
than about 50 mV is applied between a segment and the
l)ckptane. To prevent a dc buildup on the segments,
the segment-drive signals for LCDs must be square
wvs with a frequency of 30 to 150 Hz. Even if you
.1i_1JTLTILII[
fbI
pulse the TTL decoder, It still will not work because the
output low voltage of T'TL devices Is greater than 50 mV. FIGURE 9-33 LCD drive circuit arid drive waveforms.
CMOS gates are often used to drive LCDs, (a) CMOS drive circuits, fbi Segment and backplane
Figure 9-33a shows how-two CMOS gate outputs can drive waveforms.
1CM721 1M
D4 D3 D2 Dl
SEGMENT OUTPUTS SEGMENT OUTPUTS SEGMENT OUTPUTS SEGMENT OUTPUTS
I I 11111
I PROGRAMMABLE PROGRAMMABL1 [PROGRAMMABLE PROGRAMMABLE
I 4107 DECODER 4107 DECODER I 4107 DECODER 4107 DECODER
SOK-86 _______ I
0AT
ADO
____
AD1 LATCH I
AD2 1111
A03
ENABI E I
Al
rsi 2-BIT
j LATCH
I I 2 TO4
DS2 __________IDECODER
A? I
ENABLE1 II ENABLE
74LS138
__ __H
16KHZ BACKPLANE
FREE- DRIVER OUTPUT
RUNNING
+5V OSCENABLE
ENABLEDETECTOR
FIGURE 9-34 Circuit for interfacing four LCD digits to an SDK-86 bus using
Intersil ICM7211M.
LCD display such as you might buy from your local INTERFACING MICROCOMPUTER PORTS
electronics surplus store. The 721 1M Inputs can be TO HIGH-POWER DEVICES
connected to port pins or directly to microcomputer The output pins on programmable port devices can
buses as shown. For our example here, we have con- typically source only a few tenths of a milliampere from
nected the CS inputs to the Y2 output of the 74LS 138 the +5-V supply and sink only 1 or 2 mA to ground. If
port decoder that we showed you how to add to an SDK- you want to control some high-power devices such as
86 board in Figure 8-14. According to the truth table in lights, heaters, solenoids, and motors with a microcom-
Figure 8-15. the device will then be addressable as ports puter. you need to use interface devices between the
with a base address of FFIOH. SDK-86 system address port pins and the high-power device. This section shows
line A2 is connected to the digit-select input (DS2). and you a few of the commonly used devices and techniques.
'v,lrrn address line Al is connected to the DS1 input.
This gives digit 4 a system address of FFIOH. Digit 3 Integrated-Circuit Buffers
will be addressed at FFI2H. digit 2 at FF14H, and digit
I at FFI6H. The data inputs are connected to the lower One approach to buffering the outputs of port devices
four lines of the SDK-86 data bus. The oscillator input is with TFL buffers such as the 7406 hex inverting and
is left open. 7407 hex noninverting devices. In Figure 9-12. for
To display a character on one of the digits. you simpiy example, we show 74LS07 buffers on the lines from
put the 4-bit hex code for that digit In the lower 4 bits ports to a printer. In an actual circuit the 8255A outputs
of the AL register and output it to the system address to the computer-controlled lathe in Figure 9-7 should
for that digit. The ICM72I IM converts the 4-bit hex also have buffers of this type. The 74LS06 and 74LS07
code to the required 7-segment code. The rising edge of have open-collector outputs, so you have to connect a
the CS input signal causes the 7-segment code to be pull-up resistor from each output to 4-5 V. Each of the
latched in the output latches for the addressed digit: An buffers in a 74LS06 or 74LS07 can sink as much as 40
internal oscillator automatically generates the segment mA to ground. This is enough current that YOU can easily
and backplane drive waveforms shown in Figure 9-33b. drive an LED with each output by simply connecting the
For interfacing with LCD displays -which can be multi- LED and a current-limiting resistor in series between
plexed. the Intersti 1CM7233 can be used. the buffer output and +5 V.
DIGITAL INTERFACING 7
Buffers of this type have the advantage that they come Remember. current gain, or 13, as it is commonjy called.
Ix to parkage, and they are easy to apply. For cases is the ratio of collector current to the base current
where you need a buffer on only one or two port pins or needed to produce that current. To produce a collector
you need more current, you can use discrete transistors. current of 20 mA in a transistor with a (3 of 50 requires
a base current of 20 mA/50 or 0.4 mA. To drive this
buffer transistor, then, the output port pin has to supply
Transistor Buffers only the 0.4 mA.
Figure 9-35 shows some single-transistor circuits you The VOH(PER) specification of the 8255A shows that
can connect to microprocessor port lines to drive LEDs an 8255A peripheral port pin can only source 200 A
or small dc lamps. We will show you how to quickly (0.2 mAl of current and still maintain a legal TFL-
determine the parts values to put In these circuits for compatible output voltage of 2.4 V! The outputs can
your particular appiicion. First, determine whether source more than 0.2 mA. but if they source more than
you want a logic high on the output port pin to turn on 0.2 mA. the output high voltage will drop below 2.4 V.
the device or whether you want a logic low to turn on You don't care about the output high voltage dropping
the device. if you want a logic high to turn on the LED. below 2.4 V except in the unlikely case that you are
then use the NPN circuit. If you want a logic low to turn trying to drive a logic gate input off the same port pin
on the device, use the PNP circuit. Let's usen NPN for as the transistor. Let's assume an output voltage of
the first example. 2.0 V for calculating the value of our current-limiting
Next, determine how much current you need to flow resistor, R,. The value of this resistor is not very critical
through the LED, lamp, or other device. For our example as long as it lets through enough base current to drive
here, suppose that you want 20 mA to flow through an the transistor. The base of the NPN transistor will be at
LED. You then look through your transistor collection about 0.7 V when the transistor is conducting, and the
to find an NPN transistor which can carry the required output port pin will be at least 2.0 V. This leaves a
current, has a collector-to-emitter breakdown voltage voltage of 1.3 V across Rb . Dividing the 1.3 V across Rb
(V-Eo) greater than the applied supply voltage, and can by the desired base current of 0.4 mA gives an Rb value
dissipate the power generated by the current flowing of 3.25 kfl. A 2.7-kfl or 3.3-kil resistor will work fine
through it. We usually keep some inexpensive 2N3904 here,
NPNs and some 2N3906 PNPs on hand for low-current if you chose to use the PNP circuit in Figure 9-35b.
switch applications such as this. Some alternatives are an output pin on an 8255A could easily sink enough
the 2N2222 NPN and the 2N2907 PNP. current to drive the base of the transistor, The VOL(PER)
When you decide what transistor you are going to use. specification for an 8255A indicates that an output pin
look up its cuj-rent gain. h. on a data sheet. If you can sink at least 1.7 mA and still have an output low
don't have a data sheet, assume a value of 50 for the voltage no greater than 0.45 V. The base of the PNP
current gain of small-signal transistors such as these. transistor in Figure 9-35b will be at about + 4.3 V when
the transistor is on. and the output of the 8255A will
be at about +0.3 V. This means that the R b in Figure
+5 V 9-35b has about 4 V across it. Dividing this voltage by
the required 0.4 mA gives an Rb value of 10 kf1,
When you need to switch currents larger than about
50 mA on and off with an output port line, a single
1500
transistor does not have enough current gain to do this
dependably. One solutton to this problem is to connect
FROM OUTPUT 2.1 K
two transistors in a Darllngton configuration, as shown
PORT PIN >____vMv___tN39o4
In Figure 9-36. A circuit such as this might be used to
drive a small solenoid valve which controls the flow of a
chemical into our printed-circuit-board-making ma-
(a) chine or a small solenoid in the, print heads of a
dot-matrt.x printer. The dotted lines around the two
transistors in Figure 9-36 indicate that both devices
+5 V
are contained in the same package. Here's how this
N,, configuration works.
FROM OUTPUT
POATFIN 2N3906 The output port pin supplies base current to transistor
82K
OR Ql. This base current produces a collector current 13
times as large in Q I. The collector current ofQ I becomes
the base current of Q2 and is amplified by the current
gain of Q2. The result of this is that the device acts like
1150 a single transistor with a current gain of (3 QI x 13 Q2
and a base-emitter voltage of about 1.4 V. The internal
Ib) resistors help turn off the transistors. The TIP 110 device
we show here has a minimum (3 of 1000 at I A, so if we
FIGURE 9-35 Transistor buffer Circuits for driving LED
assume that we need 400 mA to drive the solenoid, then
from 8255A port pin. (a) NPN. (b) PNF'.
the worst-case current that must be supplied by the
if 470
4
1
SWITCH ON
0
0
1
0
1
1
0
1
cCW
1/6 7406
1 N 4002 bI
12
01
470
-':: j 2:r-j.. -
1/67406 EIGHT-STEP INPUT SEQUENCE
HALF-STEP MODE)
1 N 4002
02 —I MJE29S5 __________
STEP SW4 SW3 SW?
470 SW1
I K'" RED/WHITE I - 1 0FF OFF ON ON
1/67406 2 OFF OFF OFF ON
1N4002 3 ON OFF OFF ON
03 Mit 2955 ___________ 4 ON OFF OFF OFF
470
GREEN/WHITE 5 ON ON OFF OFF
6 OFF ON OFF OFF
Rl.- A2
80 7 OFF ON ON OFF
1/6 7406 iow1• 10' OFF OFF
8 OFF ON
1 OFF OFF ON ON
D4
(41
When you step a stepper motor to a new position. it However, for higher stepping rates and more torque
tends to oscillate around the new position before settling while stepping. we use a higher supply voltage and
down. A common software technique to damp out this current-limiting resistors, as shown. The point of this
oscillation is to first send the pattern to step the motor Is that by adding Series resistance, we decrease the LJR
toward the new position. Wher. the motor has rotated time constant, This allows the current in the windings
part of the way to the new position, a word to step the to change more rapidly. For the motor we used, the
motor backward is output for a short time. This is like current per winding is 0.88 A. Since only one winding
putting the brakes on. The step-for-ward word is then on each resistor is ever on at a time, 6.5 V/0.88 A gives
sent again to complete the step to the next position. The a resistor value of 7.4 fl. To be conservatIve, we used
timing for the damping command must be determined 8-ft. iO-W resistors. The optional transistor switch and
experimentally for each motor and load. diode connection to the + 5-V supply arc used as follows.
Before we go on. here are a couple of additional points When the motor is not stepping, the switch to + 12 V is
about the Circuit in Figure 9-39a. in case you want to off. so the motor is held in position by the current from
add a stepper to your robot or some other project. First the +5-V supply. Before you send a step command, you
of all. dont forget the clamp diodes across each winding turn on the transistor to #12 V to give the motor more
to save the transistors from inductive kick. Second. we current for stepping. When stepping is done, you turn
need to explain the function of the current.limiting off the switch to + 12 V. and drop back to the +5-V
resistors, RI and P2. The motor we used here has a supply. This Cuts the power dissipation.
nominal voltage rating of 5.5 V. This means that we In small printers, one or more dedicated microproces-
could have designed the circuit to operate with a voltage sors are used to control the various operations in the
of about 6.5 Von the emitters of the driver transistors printer. In this case, the microprocessors have plenty of
(5.5 V for the motor plus 1 V for the drop across thc time to control the Print-head and line-feed stepper
transistor). For low stepping rates, this would work fine. motors In software, as we described above. For applica-
+29 V
DI AEC
CONTI
STEP
INPUT JL
HI
A
LO
B I I
I ____________ I I
I p I I I
0° 90' 180' 270' 360' 90° 190' 270° 360'
I I I
00 I
10 I ii I 01 I 00 10 I 11
I
I
01 I 00 I 10
0.004 SEC.
ONE CYC(E
FIGURE 9-42 Rhino XR robotics system. (Rhino Robots FIGURE 9-43 Optical-encoder disk slot pattern and
Incorporated) output waveforms.
22??
26. a. What are the major disadvantages of mechani- code, used on many absolute-position shaft
cal relays? encoders?
b. How do solid-state relays solve these problems? b. If a Gray-code wheel has six tracks and each
track represents I binarybit. what is its angular
27. a. 1-low is electrical isolation between the control resolution?
input and the output circuitry achieved -In a
solid-state relay? 30. a. Look at the encoder disk on the Rhino arm in
b. Describe the function of the zero-crossing de- Figure 9-42. Do the waveforms in Figure 9-43
tector used in better-quality solid-state relays. represent clockwise or counterclockwise rota-
C. Why is a snubber circuit required across the tion of the motor shaft as seen from the gear
triac of a solid-state relay when you are driving end of the motor, which is what you care about?
inductive loads? b. Assume the A signal shown in Figure 9-43 Is
connected to bit DO and the B signal is con-
28. Write the algorithm and the program for an 8086 nected to bit DI of port FFF8H. Write a proce-
procedure to drive the stepper motor shown in dure which determines the direction of rotation
Figure 9-39. Assume the desired direction of rota- and passes a I back in AL for clockwise rotation
tion is passed to the procedure in AL (AL = 1 is and a 0 back In AL for counterclockwise
clockwise. AL = 0 is counterclockwise) and the rotation.
number of steps is passed to the procedure in CX.
c. DC motors, such as those on the Rhino arms.
Also assume full-step mode, as shown in Figure are rotated clockwise by passing a current
9-39b. Don't forget to delay 20 ms between step through them in one direction and rotated
commands! counterclockwise by passing a current through
29. a. Why is Gray code, rather than straight binary them in the opposite direction. Assume you
In order to control the machines in our electronics 5. Describe briefly the operation of flash. successive-
factory, medical instruments, or automobiles with mi- approximation, and ramp A/D converters.
crocomputers. we need to determine the values of vari- Draw circuits showing how A/D converters of vari-
6.
ables such as pressure, temperature, and flow. There ous types can be interfaced to a microcomputer.
are usually several steps in getting electrical signals
which represent the values of these variables and con- 7. Write programs to control A/D and D/A converters.
verting the electrical signals to digital forms the micro-
8. Describe how feedback is used to control variables
computer can work with. such as pressure. temperature. flow, motor speed.
The first step involves a sensor, which converts the
etc.
physical pressure. temperature. or other variable to a
proportional voltage or current. The electrical signals 9. Describe the operation of a 'time-slice" factory-
from most sensors are quite small, so they must next control system.
be amplified and perhaps filtered. ThIs Is usually done
with some type of operational-amplifier (op-amp) circuit. 10. Describe the tools arid techniques currently used
to develop a microcomputer-based product.
The final step is to convert the signal to digital form
with an analog-to .digital (A/D) converter. 11. Draw a block diagram of a digital filter and briefly
In this chapter we review some op-amp circuits com- describe its basic operation.
monly used in these steps, show the interface circuitry
for some common sensors, and discuss the operatIon
and interfacing of D/A converters. We also discuss the
REVIEW OF OPERATICNAL-AMPLIFIER
operation and Interfacing of A/D converters and show
how all of these pieces are put together in a microcomput-
CHARACTERISTICS AND CIRCUITS
er-based scale and a microcomputer based machine- Basic Operational-Amplifier Characteristics
control system. As part of these examples, we discuss
the tools and techniques used to develop microcomputer. Figure 101a i-':. e schematic symt-'ol Icr a' op
based products. Finally, we discuss how an A/D con- amp. Herc are the important points for you to remember
verter, a microcomputer, and a D/A converter can be about the basic op amp. First, the pins labeled +V
used to produce a digital filter. and - V represent the power-supply connections. The
voltages applied to these pins will usually be 1- 15 V and
- ISV. or + 12 V arid - 12 V. The op amp also has two
signal inputs. The Input labeled with a - sign is called
the inverting input, and the input labeled with a + sign
OBJECTIVES
is called the noninverting input. The + and - on
At the conclusion of this chapter, you should be able to: these inputs have nothing to do with the power supply
voltages. These signs indicate the phase relationship
I. Recognize several common op-amp circuits, de- between a signal applied to that input and the result
scribe their operation, and predict the voltages at that signal produces on the output. If, for example, the
key points in each. noninverting input is made more positive than the
2. Describe the operation and interfacing of several inverting input, the voltage on the output will move in
common sensors used to measure temperature. a positive direction. In other words, if a signal Is applied
pressure, flow, etc. to the noninverting input. the output signal will be in
phase with the input signal. If the Inverting input is
3. Describe the operation of a D/A converter and define made more positive than the noninverting input, the
D/A data-sheet parameters. such as resolution. output signal will be inverted, or 180 out of phase with
settling time, accuracy, and linearity. the input signal.
4. Draw ctrcuiis showing how to interface D/A convert- The ratio of the voltage out from an amplifier circuit
ers with any number of bits to a microcomputer. to the input voltage is called voltage gain. A. In symbols,
290
OUTPUT. +V -1 V
OIJTPUT.+v-tV IF <V•,,
VI,. >__f .4,,IFV,,. >
* OUTPUT-V+1V
- OUTPUT-V*IV IFV,>V,
F '/,,. <V,.,.,. N,
-Vx
A 0 > 1OO.IO
V,,1,
(b)
NON INVERTING AMP INVERTING AMP ADDER (MIXER)
\I,. A N)
V1...
>-t VI
RI RI
Ni +V A? +V
vr rl (R2 A.. %'. - - OII
L______4 i, > , MEG n
IN PHASE
R1lIA2l)R Z,,. • Al -V
•-
0=180'
Id)
(I)
DIFFERENTIAL AMP INSTRUMENTATION AMP
V..
RI
Ri V-(V-v,t
Rt=R2
H V R3- RI
Va 1* N3, R2'%j NI
'/ N3 /'Hj
('I
A)
INTEGRATOR (RAMP GENERATOR) DI F FERENTIATOR
C
RI
V_>_
,. >—I
* -
v -v
R2 Hi L..JJ.. R2 '.Rl M.. 1J-1J
v0, VO.,
(/) ('I
2ND ORDER LOW PASS FILTER 2ND ORDER HIGH PASS FILTER
V1R3+:Vv: CIA3*V
1.00 H?
Ci - 2C2 -V -1.00
RI a? _______ C1C2
A3..lK2 R2-2RI I
R3 I Kil
1k) I/I
- 39
ANAI.00 INTERFACING AND INDUSTRIAL CN1 Rot 291
= V/V1N. The Av for an op amp is typically 100.000 voltage changes at a different input voltage when the
or more. (The number is variable with temperature and input is gntng in the positive direction than it does
from device to device.) Another useful way of saying this when the input voltage is going in the negative direction.
is that an -OP amp amplifIes the difference in voltage If you have a thermostatically controlled furnace in your
between these two inputs by 100.000 or more. Now let's house, you have seen hysteresis in action. The furnace,
see how much the output changes for a given input for example. may turn on when the room temperature
signal, and see how an op amp Is used as a comparator. drops to 65°F and then not turn off until the temperature
reaches 68° F. Hysteresis Is the difference between the
two temperatures. Without this hysteresis. the furnace
Op-Amp Circuits and Applications
would be turning on and off rapidly lithe room tempera-
OP AMPS AS COMPARATORS ture were near 68°F. Another situation where hysteresis
We said previously that an op amp amplifies the differ- saves the day is the case where you have a slowly
ence in voltage between its inputs by 100.000 or more. changing signal with noise on it. Hysteresis prevents the
noise from Causing the comparator output to oscillate as
Suppose that you power an op amp wIth +15 V and the input signal gets close to the reference voltage.
-15 V. tie the inverting input of the op amp to ground. To determine the amount of hysteresis In a circuit
and apply a signal of +0.01 V dc to the noninverting such as that in Figure I0-lc. assume V = 0 V and
input. The op amp will attempt to ampli1r this signal by = 13 V.A simple voltage-divider calculation will tell
100,000 and produce the result on its output. An input you that the noninverting input is at about 13 mV. The
signal of 0.01 V times a gain of 100,000 predicts an voltage on the inverting input of the amplifier wifi have
output voltage of 100 V. The maximum positive voltage to go more positive than this before the comparator will
the op-amp output can go to, however, is a volt or two changestates. Likewise. if you assume V is -13 V.
less than the positIve supply voltage, so this is as far as the noninverting input will be at about - 13 mV. so the
it goes. A common way of expressing this is to say the voltage on the Inverting input of the amplifier will have
op-amp output "goes into saturation" at about + 13 V. to go below this to change the state of the output. The
Now suppose that you apply . a signal of -0.01 V to hysteresis of this comparator is 26 mV.
the noninverting input. The output will now try to go
to - 100 V as fast as it can. The output, however, goes NONINVERTING AMPLIFIER OP-AMP CIRCUIT
into saturation at about - 13 V. so it stops there.
In this circut the op amp effectively compares the When operating in open-loop mode (no feedback to the
input voltage with the voltage on the inverting input Inverting input), an op amp has a very high, but
and, gives a high or low output. depending on the result unpredictable, gain. This is acceptable for use as a
of the comparison. If the input is more than a few comparator, but not for use as a predictable amplifier.
rnicrovolts above the reference voltage on the inverting Figure 10-Id shows one way negative feedback is added
input, the output will be high (+ 13 V). lithe input to an o amp to produce an amplifier with stable,
voltage is a few microvoIt more negative than the predi'tahle gain. First of all, notice that the input signal
reference voltage, the output will be low H 13 'IL An in this circuit is applied to the noninverting input, so
op amp used in this way is called a comparator. Figure the output will be in phase with the input. Second. note
10-lb shows how a comparator is usually labeled. The that a fraction of the output signal is fed back to the
reference voltage applied to the inverting input does not inverting Input. Now, here's hc .w this works.
have to be ground (0 VI. An input voltage can be To start. assume that V is 0 V. V is 0 V. and the
compared to any voltage within the input range specified voltage on the inverting Input is 0. Now, suppose that
for the particular op amp. you apply a + 0.01-V dc signal to the noninverting input.
As you will see throughout this chapter, comparators Since theO. 1-V difference between the two inputs will
have many applications. We might, for example, connect be amplifIed by 100.000, the output will head toward
a comparator to a temperature sensor on the boiler + 100 V as fast as it can. However, as the output goes
in our electron4m, factory. When the voltage from the positive, some of the output voltage will be fed back to
temperature sensor goes above the voltage on the refer- the inverting input through the resistor divider. This
ence input of the comparator, the output of the compara- feedback to the inverting Input will decrease the differ-
tor will change state and send an interrupt signal to ence in voltage between the two inputs. To make a long
the microprocessor controlling the boiler. Commonly story short, the circuit quickly reaches a predictable
available comparators such as the LM3 19 have TFL- balance point where the voltage on the inverting input.
compatible outputs which can be connected directly to V, is very, very close to the voltage on the noninverting
microcomputer ports or interrupt inputs. input. V,5 . Fora 1 .0-Vdc output, this equilibrium voltage
Figure 10- Ic shows another commonly used compara- difference might be about 10 V. If you assume that the
tor circuit. Note in this circuit that the reference signal voltagcs on the two inputs arc equal. then predicting
is applied to the noninverting input, and the input the output voltage for a given input voltage is 'simply
voltage Is applied to the inverting input. This Connection a voltage-divider problem. V 0 = V (Ri + R2VR1. if
simply inverts the output state from those in the previ- R2 = 99kClandRI = lkfl. then V 0 = V 5 x 100. For
ous circuit. Note also in Figure 10-Ic the positive- a 0.01-V input signal, the output voltage will be 100 V.
feedback resistors from the output to the noninverting The voltage gain of a circuit with feedback is called its
input. This feedback gives the comparator a characteris- closed-loop gain. The closed-loop gain. for this cir-
tic called hysteresis. Hysteresis means that the output cuit is equal to the simple resistor ratio, (Rl + R2VRI.
To see another advantage of feeding some of the output dc gain of 100.000 or store. At hIgher frequencies, the
signal back to the inverting input, let's see what, happens gain decreases until, at some frequency, the open-loop
t
when the load connected to the outpu of the op amp
changes and draws more current from the output. The
gain drops to 1. Figure 1 0-2a shows an open-loop voltage
gain versus frequency graph for a common op amp such
output voltage wilt temporarily drop because of the as a 741 The frequency at which the gain is 1 Is referred
increased load. Part of this voltage drop will he fed back to on data sheets as the unity-gain bandwidth or the
to the inverting input, increasing the difference in gain-bandwidth product. A common value for this is I
voltage between the two inputs. The increased difference MHz. The bandwidth of an amplifier circuit with negative
will cause the op amp to drive its output harder to feedback times the low-frequency closed-loop gain will
correct for the increased load. The feedback then causes be equal to this value. For example. if an op amp with
the op amp to at least partially compensate for the a gain-bandwidth product of 1 MHz t used to build an
increased load on its output. amplifier circuIt with a closed-loop gain of 100, the
Feedback which causes an amplifier to oppose a bandwidth of the circuit, f. will be about I MHz/10Oor
change on its output is called negative feedback, Be- 10 k}1z. as shown in Figure I0-2b.
cause of the negative feedback, the op amp will work The point here is that the gain-bandwidth product of
day and night to keep its output 8tabiluzed and its two the op amp limits the maximum frequency that an
inputs at nearly the same voltage! This is probably the amplifier circuit can amplify.
moat important point you need to know to analyze or
troubleshoot an Op-amp circuit with negative feedback. OP-AMP ADDER CIRCUIT
Draw a box around this point In your mind so you don't Figure 10-if shows a commonly used variation of the
forget it, inverting amplifier described in the previous section.
The noninverting circuit we have Just discussed Is This circuit adds together or mixes two or more input
used mostly as a buffer because it has a very high input' signals. Here's how it works.
impedance, Z,. This means that it will not, load dow-i, Remember from the previous discisslou that in an
a sensor or some other device you onnect to Its input. inverting circuit, the op amp holds the inverting i9put
If it uses a bipolar-transistor input op amp, the circuit at 0 V or virtual ground. The current through each ci
in Figure 10-Id will have an input impedance greater the input resistors will be the same as if it were connected
than 100 Mfl. If a FET input op amp such as the
'about 1ot2
National LF356 is used, the input impedance will be
ft
INVERTING AMPLIFIER OP-AMP CIRCUIT
Figure lO-le shows a somewhat more versatile amplifier 10.0001
SLOPE • -6 dO/OCTAVE OR
dO/DECADE
circuit using negative feedback. Note that in this circuit.
the noninverting input is tied to ground with a resistor.
z
i3O00 E-
and the signal you want to amplify is applied to the 100 I-
inverting Input through a resistor. Since the signal is UNITY GAIN
applied to the inverting input, the output signal will be 'OF-II /rRE,ENCv
180° out of phase with the input signal. For this circuit, 1 11111 I
resistor RI suppltes the negative feedback which keeps 1 10 100 I 10 100
HZ KHZ FREQUEI.4CY
the two inputs at nearly the same voltage. Since the MHZ
noninverting input is tied to ground, the op amp will
sink or source whatever current is needed to hold the
inverting input also at zero volts. Because the op amp
fi
holds the inverting input at zero volts, this node is
100,000
referred to as a virtual ground.
The voltage gain of this circuit is also determined by
the ratio of two resistors. The A 1 for this circuit at low
io - OPEN LOOP
100
N OP AMP
OPEN LOOP
For the high-pass filter, the gain for the flat section of
the response curve is also 1. Assuming that the two
capacitors are equal and the value of R2 is twice the
value of Ri. the critical frequency is determined by the
10 formula shown next to Figure 10-lI. Again. R3 Is for
0 damping.
A low-pass filter can be put in series with a high-pass
0.1 ORDER ifiter to produce a bandpass filter which lets through a
LOW PASS
.01 F I LT ER desired range of frequencies. There are also many differ-
ent single-amplifier circuits which will pass or reject a
band of frequencies.
.0001 I Now that we have refreshed your memory of basic op-
1 10 100 1 10 100 1
HZ KHZ MHZ amp Circuits, we will next discuss some of the different
types of sensors you can use to produce electrical signals
ic
proportional to the values of temperatures. pressures.
(a) position, etc.
100.000
SENSORS AND TRANSDUCERS
10.000
OP AMP It would take a book many times the size of this one to
1,000 LOOP describe the operation and applications of all the differ-
100 ent types of available sensors and transducers. What we
10 want to do here is introduce you to a few of these nd
0 show how they can be used to get data for microcomput-
er-based machines in, for example, our electronics
0.1- factory.
.01 /"1-..2NDORDER
.001-
/ I HIGHPASSFILTER
Light Sensors
I
000• I I One of the simplest light sensors is a light-dependent
1 10 100 I 10 100 1
HZ KHZ MHZ resiStor such as the Clairex CL905 shown in Figure
lO-4a. A glass window allows light to fall on a zigzag
fc
(b)
FIGURE 10-3 Cain versus frequency response for
second-order low-pass and high-pass filters. (a) Low-
pass. (b) High-pass.
(a)
the dc component of an input signal cannot reach the
noninverting input, because of the two capacitors In
series with that input. Therefore, this circuit will not
amplify dc and low-frequency signals. Figure 1O-3b
shows the graph of gain versus frequency for a high-
pass filter such as this. Note that the gain-bandwidth 100 K (At LAY
product of the op amp limits the high-frequency re-
sponse of the circuit.
For the iow-pass circuit in Figure 10-1k, the gain for
the flat part of the response curve is 1. or unity, because
__J_______
the output is fed back directly to the inverting input. At P HO TO A E SISTO A
the critical frequency, f,. the gain will be 0.707. and
above this frequency the gain will drop off. The critical
frequency for the cIrcuit is determined by the equation
next to the circuit. The equation assumes that RI and (b)
R2 are equal and that the value of Cl is twice the value
of C2. R3 is simply a damping resistor. The positive FIGURE 10-4 (a) Cadmium sulfide photocell. (Clawex
feedback supplied by C I is the reason the gain is only E'ectronics) (b) Light-controller relay Circuit using a
down to 0.707 at the critical frequency, rather than photocell.
OFFSET
vs REFERENCE
RG
P5
I 1KO
R3
Lt v
REMOTE
TEMPERATURE-
TO-CURRENT
R- TRANSDUCER,
1 '.A/K 1 KO 01% INSTRUMENTATION
LOW TCR AMPLIFIER,
A0590 IC IS METERING GAIN OF 10.
AVAILABLE IN RESISTOR, 0.00 V TO 1.00 FS
PROBE AS AC2626J lroVlpA lrnV/K ID ntV/0C
I) (b)
connecting wires do not have any effect on the output thermocouples is changing. The problem is that it is
value. If the gain and offset are carefully adjusted, the Impossible to tell which thermocouple caused a change
accuracy of the circuit in Figure l0-6b is ± 10 C using in output voltage. One cure for this is to put the reference
an AD59OK part. junction in an ice bath or a small oven to hold it at a
constant temperature. This solution is us'ually inconve-
THERMOCOUPLES nient, so instead a circuit such as that in Figure 10-7
is used to compensate electronically for changes in the
Whenever two different metals are put in contact, a small
temperature of the reference junction.
voltage is produced between them. The voltage developed
As we discussed In a previous section, the AD590
depends on the type of metals used and the temperature.
shown here produces a current proportional to its
Depending on the metals, the developed voltage in-
temperature. The AD590 Is attached to the reference
creases between 7 and 75 p.V for each degree Celsius
thermocouple so that they are both at the same tempera-
increase in temperature. Different combinations of met-
ture. The current frorp the AD590, when passed through
als are useful for measuring different temperature
the resistor network, produces a voltage which compen-
ranges. A thermocouple junction made of iron and
constantan, commonly called a type J thermocouple.
has a useful temperature range of about - 184 to + 7600
.
sates for changes in the reference thermocouple with
temperature . The signal to the amplifier then is proper-
C. A junction of platinum and an alloy of platinum and
13 percent rhodium has a useful range of 0 to about 7.5V
16000 C. Thermocouples can be made small, rugged,
and stable; however, they have three major problems
which must be overcome.
REPEAEE,CE
JUNCTION
15C < T4<35'C,,
I
,_1!1 0
The first of these is the fact that the output is very OUTPUT
AMPLIFIER
small and must be amplified a great deal to bring it up
MEASURING
into range where it can, for example, drive an A/D JUNCTION
converter.
Second. as shown in Figure 10-7. a reference junction
ii___
made of the same metals must be connected in series 523flI25V
with thejunction being used to make the measurement, I.+P -25V I 866Ki)
Note that the reference junction is connected in the 1TE j 50M1i OVT
reverse direction from the measuring junction. This is I I VALUE I
done so that the output connecting wires are both I I 52311 I
I I 4120 I
constantan. The thermocouples formed by connecting £ I 61412 I NOMiNAL
T 402 I VALUE
these wires to the copper wires going to the amplifier I S. I 5760 91350
will then cancel out. T.he input voltage to the amplifier
will be the difference between the voltages across the FIGURE 10-7 Circuit showing amplification and cold-
two thermocouples. II we simply amplify this voltage. function compensation for thermocouple. (Analog
however, there is a problem if the temperature of both Devices Incorporated)
tional only to changes in the sensor thermocouple. the resistance of the RTD. The resistance of an RTD
Canceling out the effects of ambient temperature varia- increases with an increase in temperature.
tions on the reference Junction is referred to as cold- Thermistors consist of semiconductor material whose
junction compensation. The table in Figure 10-7 shows resistance decreases nonlinearly with temperatjre. De-
the values of R which will provide cold-junction compen- vices with 25° C resistance of tens of ohms to millions
sation for common types of thermocouples. An Instru- of ohms are available for different applications. Thermis-
mentation amplifier such as that In Figure lO-lh is tors are relatively inexpensive, have very fast response
usually used for this application. times, and are useful in applications where precise
The third problem with thermocouples is that their measurement is not required. A circuit similar to that
output voltages do not change linearly with temperature. in Figure 10-8 can be used to produce a voltage propor-
This can be corrected with analog circuitry which tional to the resistance of the thermistor.
changes the gain of an amplifier according to the value
of the signal However, when a thermocouple is used
with a microcomputer-based instrument, the correction
can be easily done using a lookup table in ROM. An Force and Pressure Transducers
A/D Converter converts the voltage from the thermocou- To convert force or pressure (force/area) to a proportional
pie to a digital value. The digital value is then used as electrical signal, the most common methods use strain
a pointer to a ROM location which contains the correct gages or linear variable differential transformers
temperature for that reading. (LVDTs). Both of these methods Involve moving some-
thing. This why we refer to them as transducers rather
RTDS AND THERMISTORS than as sensors. Heres how strain gages work.
Resistance temperature detectors (RTD5) and thermal
sensitive resistors (thermistois) are two other commonly STRAIN GAGES AND LOAD CELLS
used types of temperature sensors. Both of these types A strain gage Is a small resistor whose value changes
are essentially resistors which change value with a when its length is changed. It may be made of thin wire.
change in temperature. RTDs consist of a wire or a thin thin foil, or semiconductor material. Figure 10-9a shows
film of platinum or a nickel wire. The response of a simple setup for measuring force or weight with Strain
RTDs is nonlinear, but they have excellent stability gages. One end of a piece of spring steel is attached to
and repeatability. Therefore, they are often used in a fixed surface, A strain gage is glued on the top of the
applications >vhere very precise temperature measure- flexible bar. The force or weight to be measured is
ment Is needed. RTDs are useful for measures in the applied to the unattached end of the bar. As the applied
range of —250 to +850° C. A circuit such as that In force bends the bar, the strain gage is stretched. Increas-
Figure 10-8 can be used to convert the change in ing its resistance. Since the amount that the bar is bent
resistance of the RTD to a proportional voltage. 0p amp is directly proportional to the aDplted force, the change
Al in this circuit produces a precise reference voltage in resistance will be proportional to the applied force, If
of —6.25 V. This voltage produces a precise current at a current Is passed through the strain gage, then
the inverting input of A2. °p amp A2 pulls this current the change in voltage across the strain gage will be
through the RTD to produce a voltage proportional to proportional to the applied force.
PLATINUM
RTD
loon to 2000
25 K -625 V SPAN
*15 V 500 *15V
...' 2200 -
A2
4.7 KO 1.8 V
FOR 0 TO
266° C
1K
REF. -1.8 V
OFFSET
1.5 KiZ
IbI
Other Sensors
As we mentioned previously, the number of different
types of sensors Is very large. In addition to the types
we have discussed, there are sensors to measure pH,
FIGURE 10-11 LX18O4GBL pressure
concentration of various gases, thickness of materials,
transducer. (Sensym, Incorporated)
presence of an object (proximity), and just about any-
thing else you might want to measure. Often you can
use commonly available transducers in creative ways
• PRIMARY SECONDARIES to solve a particular application problem you have.
1+ Suppose, for example, that you need to accurately deter-
mine the level of a liquid in a large tank. To do this, you
AC - could install a pressure transducer at the bottom of the
EXCITATION tank. The pressure in a liquid is proportional to the
SIGNAL hed of the liquid in the tank, so you can easily convert
20KHZ
a pressure reading to the desired liquid height. ']he
point here Is to check out what is available and then be
1 creative.
MOVEABLE IRON CORE
Flow Sensors
If we are going to control the flow rate of some material FLOW-.
in our electronics factory, we need o be able to measure
it. Depending on the material, flow rate, and tempera- RESISTANCE
ture, we use different methods. 0)
Qn. method used is to put a paddle whcel in the flow.
aown in Figure lO l3a. The rate at which the paddle FIGURE 10-13 Flow sensors. (a) Paddle wheel,
iei turns Is proportional to tJ' rate of flow of a liquid b DI tferen hal pres,u re.
CHAPTE R Tt'J
converter. In many industrial applications where the order to pull this current through the feedback resistor,
sensor Is a long distance from the A/D converter, how- the op amp must put a voltage of 0.05 mA x 10 ku or
ever, the signals from the sensors or transducers are 0.5 Von its output. If you also close switch Dl. it will
converted to currents instead of voltages. Sending a send another 0.1 mA into the summing point. In order
signal as a current has the advantages that the signal to pull the sum of the currents through the feedback
amplitude Is not affected by resistance, induced-voltage resistor, the op amp has to output a voltage of 0.15
noise, or voltage drops in a long connecting line. A mA X 10 kfl or 1.5 V.
common range of currents used to represent analog The point here is that the binary-weighted resistors
signals in industrial environments Is 4 to 20 mA. A produce binary-weighted currents which are summed
current of 4 mA represents a zero output, and a Current by the op amp to produce a proportional output voltage.
of 20 mA represents the full-scale value. The reason the The binary word applied to the switches produces a
current range is offset from zero is so that a current of proportional output voltage. Technically the output volt-
zero is left to represent an open circuit. At the receiving age is "digital" because it can only have certain fixed
end of the line, a resistor or a simple op-amp Circuit is values. Just as the display on a digital voltmeter can.
used to Convert the current to a proportional voltage However, the output simulates an analog signal, so we
which can be applied to the input of the A/D converter. refer to it as analog. Switch D3 in Figure 10-14 repre-
sentS the most significant bit because closIng it produces
the largest current. Note that since V l,.EF is negative, the
output will go positive as switches are closed.
0/A CONVERTER OPERATION, As you see here, the heart of a D/A converter is a
INTERFACING, AND APPI.ICATIONS set of binary-weighted current sources which can be
In the previous sections of this chapter we have dis- switched on or off according to a binary word applied to
cussed how we use sensors to get electrical signals its inputs. Since these current sources are usually inside
proportional to pressure, temperature. etc. and how we an IC. we don't need to discuss the different ways the
use op amps to amplify and filter these electrical signals. binary-weighted currents can be produced. The op-amp
The next logical step would be to show you how to use circuit in Figure 10-14 converts the sum of the currents
an A/D converter to get these signals into digital form to a proportional voltage.
that a microcomputer can work with. However, since
D/A CHARACTERISTICS AND SPECIFICATIONS
D/A converters are simpler and since several types of
A/D converters have D/As as part of their circuitry, we Figure 10-15 shows the circuit for an inexpensive IC
will discuss DIAs first. D/A converter with an op-amp circuit as a current-to-
v 5 V
lOkO v,, fA1A2 +A4++M+A7^
V0 (A0) 1 2 4 8 16 32 64 128 256
I { + +
02 R3 25k0 I 6
1., V f
255
-b y -9961V
03 °2kf
o—,AA
R1
L -
FIGURE 10-15 Molorola MC1408 S-bit D/A with current-
FiGURE 10-14 Simple 4-bit DIA converter. to-voltage converter.
Vcc °1 BYTE 1/
BYTE 2
of the motor will vaxy if the load changes. Later in the SIGNAL
FROM
chapter we show you how to add feedback control to ADDRESS
DECODER
maintain constant motor speed under changing loads.
So far we have talked about using an 8-bit 0/A with SYSTEM WR2
a microprocessor. Interfacing an 8-bit converter Involves STROBE -=. L
simply connecting the inputs of the converter to an
output port or, for some 0/As, simply connecting the II
inputs to the buses as you would a port device. Now,
suppose that for some application you need 12 bits of FULL-SCALE ADJUST
resolution, so YOU need to interface a 12-bit converter.
If you are working with a system which has an 8-bit
data bus, your first thought might be to connect the
lower 8 inputs of the 12-bit converter to one output port
and the upper four inputs to another port. You could OAC12. LF35 Voor
send the lower S bits with one write operation and the .
upper 4 bits with another write operation. However, the
time between the two writes introduces a potential
problem in this approach. A DJ UST M E NT I
Suppose, for example, that you want to change the
output of a 12-bit converter from 0000 1111 1111 to VorHI,, XRI=-2FORO<D.C4095
0001 0000 0000. When you write the lower 8 bits, the
IbI
outputwfllgofrom0000 liii 1111 to000000000000.
When you write the upper 4 bits, the output will go back
FIGURE 10-17 (a) National DAC12O8 12-bit 0/A input
up to the desired 0001 0000 0000. The point here is
block diagram showing internal latches. (b) Analog
that for the time between the two writes the output will
Circuit connections.
go to an unwanted value. In many systems this could
be disastrous. The cure for this problem Is to put latches
on the input lines. The latches can be loaded separately
and then strobed together to pass all 12 bits to the 0/A an SDK-86 board, you can simply connect the DAC 1208
converter at the same time. data inputs to the lower 12 data bus lines, connect the
Many currently available 0/A converters contain built- CS input to an address decoder output, connect the
in latches to make this easier. Figure lO-17a shows a WR1 input to the system WR line and tie the WR2 and
block diagram of the National DAC 1230- and DAC 1208- XPER inputs to the ground. The BYTE 1/BYTE input is
type 12-bit Converters. Note the Internal latches and the tied high. You then write words to the converter just as
register. The DAC 1230 serIes of parts has the upper 4 lilt were a 16-bi' port. The timing parameters for the
input bits connected to the lower 4 bits so that the 12 DAC 1208 are acceptable for an 8086 operating with a
bits can be written with two write operations from clock frequency of 5 MHz or less. For higher 8086 clock
an 8-bit port or data bus such as that of the 8088 frequencies, you would have to add a one-shot or other
microprocessor. The DAC 1208 series of parts has the circuitry that inserts a WAIT state each time you write
upper 4 data inputs available separately so they can be to the 0/A. Here are a few notes about the analog
connected directly to the bus in a system which has a connections for these devices.
16-bit data bus, as shown in Figure l0-l7a. If, for These 0/A converters require a precision voltage refer-
example. you want to connect a DAC 1208 converter to ence. The circuit in Figure l0-l7b uses a - 10.000-V
CLOCK
INPUT
-15 V
14)
74LS374
1 DATA
OUTPUTS
10
.,1
-iSV -5V
12113)1
+5V -iSV
OpF
-
2.5k
Vfiff F
output codes. The best way to make sense out of these +FS-1 LSB 9.9609 1111 fl)
tOYS 6.I) 10IXJO 0111 1111
different codes is to see them all together with represen- .SYS -1 LOB 4.9609 0711 III) 0900000
tative values, as shown in Figure 10-21. The values 4) LSB 0.039) )00001 TIll 17)0
TIlT Ill 00O)0 1111 1711
shown here are for an 8-bit converter, but you can ZERO 0.0)390 0(030000
extend them to any number of bits, -1 LOB -0,039) 000(10001 1)11 1110
_'/, FS + I LOB -49609 0(17 1111 100000OS
For an A/D converteE with only a positive input range -'SF0 -5.0000 )CO00 0111 11)1
-FS,t LOB -99609 (Ill ill)
(urotpolar). a straight binary code or inverted binary j
code is usually used. If the output of an A/D converter
UNIPOLAR BINARY CODED DECIMAL CODES
is going to drive a display, then it is convenient to have
COMPLEMENTARY INVERTED INVERTED
the output coded 1n BCD. For applications where the 70 BINARY BINARY COMPLEMENTARY
BINARY
VOLTS COOED BINARY CODED
VALUE COOED COOED
input range of the converter has both a negative and a PULL DECIMAL
DECIMAL OECIU,'IL DECIMAL
SCALE BCO)
positive range (bipolar), we usually use offset-binary CBCD) IIBCOI )ICBCDI
coding. As you can see in Figure 10-21, the values of 4FS-. I LOB 9,9 10011001 07100110
''SF5 5,0 OIOIOOCO 70701111
00000000 to 11111111 are simply shifted downward so +1 LSB - 0.1 0000) 1111 1110
that 00000000 represents the most negative input value ZERO 00 00000009 (111 III 0000 1111 1)11
and 10000000 represents an input value of zero. This -I ISS -01 I1 tIll 1110
coding scheme has the advantage that the 2's comple- -ISFS -5.0 0l0l 10101111
-FS +1 LOB --99 1097 7001 07100770
ment representation can be produced by simply in-
verting the most significant bit. Some bipolar converters
BIPOLAR BINAR y CODES
output the digital value directly In 2's complement form. COMPLEMENTARY'
IOVOLI'S OFFSET TWOS
OFFSET
VALUE FULL SCALE BINARY COMPLEMENT
BINARY
RANGE IDE)
(COOl
lId
Interfacing Different Types of A/D +FS 50000'
Converters to Microcomputers .1-S-I LOB 49609 III) 1111 00000 DIII Ill)
000OI
+1 CSB 00391 0)100091 0(11 1110
s-ews and that we want to package 100 of them per the weight after it is read into the microprocessor. The
box. We can pass the boxes over the load cell on a 0.1 -ixF capacitor between pins 15 and 16 of the amplifier
,weyor belt and fill them from a chute until the we4ght. reduces the bandwidth of the amplifier to about 7.5 Hz.
P . 1 therefore the Count, reaches some entered value. This removes 60 Hz and any high-frequency noise that
'1 . ,c point here is that the combination of intelligence might have been induced in the signal lines.
some simple interface circuitry gies you an Instru- The MC14433 A/D converter used here is an Inexpen-
r-ent with as many uses as your imagination can come sive dual-slope device intended for use In 3k-digit digital
p with. voltmeters. etc. Because the load cell output changes
slowly, a fzs& converter isn't needed here. The voltage
across an LM329 6.9-V precision reference diode is
vart-Scale Input Circt" amplified by iC4 to produce the 10.00-V excitation
re lO10 shows a picl;: 1' 1e rras, :.... ;, l.c. vclage for the load cell and a 2.000-V reference for the
-f .ei C46-1 #-ION load JD. With a 2.000-V reference voltage, the full-scale Input
ce built this scale. 'V.ei a p. :e o oig: for tie A/D Is 2.000 V. Conversion rate and
top of the load c-eli to ,.-. -o Irma fn., muipi'lng frequency for the converter are determined
.Thisloadcellhases a- by i internal oscillator and Ru. An Rh of 300 kfl
X,O. or 0.01 lb over the & . oge for 1;ch a elock [requency of 66 kFlz. a multiplex frequency
designed. 1 0. kh-lz. and about four conversions per second.
)s shown In Figure 10-2.3, the lo i cnsls's c/ Ac-uIat) of the converter is ±0.05 percent and ± 1
350-fl resistors connected In hr/dge 'fiiuratiou. count, which is comparable to the accuracy of the load
;-t.blc lO.00v excitation vtage i . spt tc, the top cell. in ,ther words, the last digit of toe displayed weight
ie bridge. With no load on the ceb. the cutpus frcn may be off by 1 or 2 counts. As we described in a previous
: bridge are at aut th same .altage. 5 V. When a scctio. the Output from this converter is in multiplexed
v'i is applitd to thc bridge. - msstance of one of the '-iCD form.
-ar resiatora will bc chanc: Tht produces a small
entIal output voltage t :tfl tIe bricige. The maxi- An Mgorthm for the Smart Scale
m differential output voltage for thts 10-lb load cell Figure 1024 shows the flowchart for our smart scale.
it/V per volt of excitation, so with 10.00 V excitatIon Note that, as indicated by the double-ended boxes In the
s shown, the maximum differential-output voltage is flowchart, most major parts of the program are written
0 mV. as procedures. This is an example of the structured.
To ampl1li this small differential signal, we use a modular programming approach we have stressed
National LM363 tpsti-umentation amplifier. This device throughout the book. Here's how it all works.
contains all the circuitry shown for the instrumentation The output of the A/D is in multiplexed BCD form.
unpllfier In Figure 10-lh. The closed-loop gain of the The Converter outputs the BCD code for a digit on its
:rnplifier is programmable with Jumpers on pins 2, 3, Q0—Q3 lines and outputs the strobe for that digit on
'd 4 for fixed values of 5, 100, and 500. We have the corresponding digit strobe line. DSI—DS4. To read
;3rpered it for a gain of 100 so that the 20-mV maximum the data for a digit, that digit strobe is polled until it
I,iial from the load cell will give a maximum voltage of goes high; then the BCD code for that digit is read in.
200 V to the A'D converter input. A precision voltage After the four BCD values are read in from the converter.
divider on the output of the amplifier divides this signal a display procedure is called to display these values on
in half so that a weight of 10.00 lb produces an output the address field displays of the SDK-86. The letters "Lb'
voltage of 1.000 V. This scaling simplifies the display of are displayed in the data field displays.
+12 V + iSV
START
LM329'' 21LM30, k_
_j,jv INITIALIZE
LOADCELL 1+15 V
BALANC I
6.4 K _DJUST
1% 1% KO
I GET WEIGHT I
FROMAJO
' -- CONVERT
PRICE/LB
TO BINARY
j35Ofl 350r) ': DUMB II DISPLAY II
SCALE
3N,6i3
MODULE
IIII WEIGHT AND LBIIIi CONVERT
14 WEIGHT
TO BINARY
350 350)') I 12 IOKO
0.1% 7N0
I 2i-J 2
MULTIPLY
WEIGHT X
N iV PRICE/LB
WHITE I 0.1%
DISPLAY 'SP.
FOR
SELLING PRICE CONVERT
________ J6 TOTAL PRICE
10 (UI (24) +5V - TO BCD
(1) 8255
.4:-J- ___?
2) 48 —PA3
Q3)23) DISPLAY KEY ROUND OFF
470 K)) [ (3) Q2 22) - 44 - PA2 PRESSED 'OTAL PRICE T(
NEAREST CENT
_A,A./y_.__ (4) 01(21) 40 - PAl
_______ 00(20) - 36 —PAO
(5)
Cl 0.1 pF READ DISPLAY
0S1 (19) - 50 - PA4
GET KEYBOARD TOTAL PRICE
-}------- (6) DS2 (18) - 46 - PA5 PRICE/LB
________
(7) MODULE
0S3 (17) - 42 - PA6
2 \1 0i HF ,,
— 7r-- ' •' - NO KEY WAIT
PRESSED 3 SECONDS
7NO
YES
FIGURE 10-23 Circuit diagram for load-cell interface
circuitry and AID converter for smart scale, FIGURE 10-24 Flowchart for smart-scale program.
Next, a check is made to see if any keys have been equivalents and then multiplied the binary numbers.
pressed by the user. If a key has been pressed, the letters Another procedure converts the binary result of the
"SP," which represent selling price, are displayed in the multiplication to RCD. The BCD result Is rounded to
address field. Keycodes are read from the 8279 as entered the nearest cent and displyed in the data field. The
and displayed on the data field display. Keys can be letters Pr" are displayed in the address field to indicate
pressed until the desired price per pound shows on that this is the total price. After a few seconds the
the display. When a nonnumeric key is pressed, it is program goes back to reading and displaying weight
assumed that the entered price per pound is correct, over and over, until a key is pressed.
and the program goes on to compute the total price.
Computing the price involves multiplying the weight
in BCD form times the price per pound in BCD form. It
The Microprocessor-Based Scale Program
is not easy to do a BCD x BCD multiply directly, so we Figure 10-25. p. 310-15, shows the complete program
took an alternate route to get there. We converted both for our microprocessor-based scale. It is important for
the weight and the price per pound to their binary you not to be overwhelmed by a multipage program such
FIGURE 10-25 Assembly language program for smart scale. (Cont,nued on pages 311-75.)
314 CHAPTERTEN
365 020A B6 19 OH, 19W Bit co,Jter for 24 BITS
366 020c EB 0014 CALL CNVT1 Produce next 2 BCD digits in CII
367 020F 51 PUSH CX Save tower 4 BcD digits on stack
368 0210 B6 19 NOV OH, 19W Bit Co,ter for 4 bits
369 0212 ER 000E CALL CNVT1 Produce next 2 8cO digits in CII
370 0215 BAco -v CL, CH Position in CL
371 0217 B6 19 NOV OH, 19H Set bit counter for 24 bits
372 0219 E8 0007 CALL CNVT1 Produce last 2 BCD digits in CH
373 021C 88 Dl NOV 0X,CX Position 4 MS BCD DIGITS for return
374 021E SB Pop BX Four LS BCD digits back from stack
375 021F 59 Pop CX for return
376 0220 58 Pop AX
377 0221 90 POPF
378 0222 C3 RET
379 0223 BINCVT EPIDP
380
381 PROCEDURE CNVT1
382 0223 CNVT1 PROC WEAR
383 0223 32 CO XOR AL, AL Clear AL and carry as workspace
38.4 0225 8A E8 NOV CH, AL Clear CII
385 0227 32 CO CNVT2: NOR AL, AL Clear AL and CARRY
386 0229 FE CE DEC DII Decrement bit counter
387 022B 75 01 JWZ CONTINUE Do all bits
388 0220 C3 RET Done if OH down to zero
389 022E Dl 03 CONTIWUE:RCI BX, 1 BX left one bit, MSB to carry
390 0230 Do 02 RCL DL, 1 HSB from BX to 158 of DL, NSB of DI. to carry
391 0232 8A C5 NOV AL, CH Hove BCO digit being built to AL
392 0234 12 CO ADC AL, AL. Dote AL and add carry from DL shift
393 0236 27 DAA Keep result in BCD form
394 0237 8A E8 NOV CII, AL Put back in CII for next time through
395 0239 73 EC INC CNVT2 No carry from DAA, continue
396 023B 83 03 00 ADC BX, 0000W If carry, propagate to BIt and DL
397 023E 80 D2 00 ADC DL, DOW for future terms
398 0241 ER E4 JNP CWVT2 Cant i flue conversion
399 0243 CNVT1 ENDP
400 0243 CE ENDS
401 END
FIGURE 10-25 (Continued)
as this, If you use the 5-minute rule and work your way field or address field and a 1 or a 0 in AH to specify a
through this program one module at a time, you should decimal point in the middle of the display or no decimal
pick up some more useful programming techniques and point. You then load BX with the offset of the memory
procedures you can use in your programs. buffer containing the unpacked codes for the digits to
Three 4-byte buffers set up at the start of the program be displayed. A program loop in the display procedure
are used to Store the unpacked BCD values of the weight. uses the XLAT instruction and the SEVEN_SEG table
the price per pound. and the computed total price. to convert these codes to the required 7-segment values
These buffers will be used to pass values to the display and send the values to the 8279 display RAM. For
procedure. The SEVEN_SEG table in the data segment displaying the weight, BX is simply loaded with the
contains the 7-segment codes for BCD digits, hex digits. offset of WEIGHT_BUFFER. AL is loaded with 01 to
and some letters we use to indicate which value is being display the weight in the address field, and All is loaded
displayed. in the display procedure you will see how with 01 to insert a decimal point at the appropriate
these codes are accessed. place.
After Initializing everything, the program polls the To display the letters Lb in the data field. BX is loaded
digit strobe for the most significant digit from the AID with the offset of the string named LB. and the display
converter. Since this A/D converter isa 3)-digit unit, the procedure is callçd. Again, the XLAT instruction loop
MSD can be only a 0 or a I. The value for this digit is converts the code4 from the LB string to the required 7-
sent in the third bit, (bit 2) of the 4-bit digit read in. If segment codes and sends them out of the 8279 display
this bit isa 1. then Otis loaded into the buffer location. RAM. The codes in the string named LB represent the
If the bit is a 0. then the value which will access the 7- offsets from the start of the SEVEN_SEC table for the
segment code for a blank (141-1) is loaded into the buffer desired 7-segment codes. For example, the 7-segment
location. Each of the other digit strobes is then polled code for a P is at offset 12H in the SEVEN_SEG table.
in turn, and the values for those digits are read in. When Therefore, if you want to display a P. you put l2H in the
all the BCL) digits for the weight are in the WEIGHT_ appropriate location in the character string in memory.
BUFFER, the display procedure Is called to show the The XLAT Instruction will then use the value l2H to
weight on the address field. access the 7-segment code for P in the SEVEN_SEC.
To use the display procedure we wrote for this pro- table.
gram. you first load a 0 or a 1 intO AL to specify data After displaying the weight. the program reads the
318 CI-IAPTERT[N
MOTOR
to tly to correct the error. When the error signal is first to changes in load or set point. Process control 1oops
applied to the dltTerentiator circuit, the capacitor In that use all three types of feedback are called proportional
series with the input is not charged, so it acts like a integral derivative or PID control loops. Because process
short circuit. This Initially lets a large current flow, so variables change much more slowly than the microsec-
the amplifier has a sizable output. As the capacitor ond operation of a microcomputer, a microcomputer
charges, the current decreases, so the feedback from the with some simple input and output circuitry can perform
differentiator decreases. The differentiator essentially all the functions of the analog circuitry in Figure 10-29
gives the amplifier a quick pulse of feedback to help for several PID loops.
correct for the increased load. Too much derivative Figure 10-30 shows a block diagram of .a microcom-
feedback can cause the system to overshoot and oscillate. puter-based process-control system. Data acquisition
The point here Is that by using a combination of some systems convert the analog signals from various sensors
or all of these types of feedback, a given feedback- to digital values that can be read in and processed by
Controlled system can be adjusted for optimum response the microcomputer. A keyboard and display in the
DISPLAY
PRESSURE SENSORS I—I RELAYS I
TEMPERATURE SENSORS
FLOW METER
I-
0
2 MICRO-
COMPUTER
A-
a.
z I-
Io
0
LOAD CELLS
LIQUID-LEVEL SENSORS DAS
RN METERS KEYBOARD
CONTAO'L
SIGNALS
NI TODAS
_J.
I
Chapter 8 does. If you program the timer to produce a
pulse every I ms, and you want the controller to Service
another loop every 20 ms, for example. you can simply
have the interrupt procedure count 20 interrupts before
going on to update the next loop. Once 20 interrupts
have been counted down, the program falls into a
decision structure which determines which loop is to
be updated next. Every 20 ms a new loop is updated. so
with eight loops, each loop gets updated every 160 ms.
This system is an example of a time-slice system, because
each loop gets a 20-ms "slice" of time every 160 ms.
INITIALIZE ON TIME
PORTS COUNTED
GET NEW
UNMASK AND TEMPERATUR
ENABLE READING
INTERRUPTS
CONVERT TO
BCD FOR
DISPLAY
LOOK FOR
USER
COMMAND
KEY PRESSED DISPLAY
FIND T
YES
SERVICE çAro
USER
COMMAND
-J COMPUTE
HEATER
DUTY CYCLE
I.'
SET DUTY
CYCLE FOR ON
TURN HEATER
ON
SET DUTY
CYCLE FOR
OFF
AETI
TOINTR .)
II
+5V +5V
*5 V
t +15v -
HEATING PIN NUMBERS FOR
10k
CONNECTOR iS OF
SOK-86
PORT 2C
28 P2CS
-15V 26 P2C4
[ -15V '=•
6 P2C3
1K
l0k 4 P2C2
TEMPERATURE SENSOR
2 P2C1
STATE 24 P2CG
74C14 RELAY 12OVAC
+5V
1k 0.1,F 4 PORT 28
+5 V ADCOSO8
°- B P267
OUTPUT END OF
ENABLE CONVERSION - NC- 12 P266
START
INPUT 0 - CONVERSION 22 - 20 P265
27 INPUT I ALE - 16 P264
18 23
O.J1pF OSCILLATOR INPUT 2 ADC - NC- 14 P263
DAS 24
INPUT 3 A08 - - 16 P262
INPUT 4 ADA - 22 P281
+15 V INPUTS DATA 7 - 10 P260
INPUT 6 DATA 6
[_5 INPUT7 OATA5 I! PORT 2A
CLOCK DATA 4 18 36 P2A7
V OATA3 40 P2A6
_Li1i *REF DATA 2 P2A5
LM329 16 -REF DATA 1 P2A4
JII GND DATAO 17 46 P2A3
42 P2A2
38 P2A1
10k 10sF
SOLID 34 P2AO
TANTALUM
REFERENCE
FOR A/D
each increase of 10 C above that. The 30O-kf resistor zener is buffered by an LM308 amplifier to produce a
connecting the output of the LM35 to - ISV allows the Vcc and a V REF 015.12 V for the A/D converter. With this
output to go negative for tmperatures below 0° C. (If reference voltage, the A/D converter will have 256 steps
you are operating with ± 12-V supplies, use a 240- of 20 mV each. Since the temperature sensor si$nal is
kft resistor.) This makes the circuit able to measure amplified by 2. each degree Celsius of temperature
temperatures over the range of —55 to + 150° C. For change will produce an output change of 20 my . or one
our application here, we use only the positive part of the step on the AID converter. This gives us a resolution of
output range, but we thought you might find this circuit 1°C, which is about equal to the typical accuracy of the
useful for some of your other projects. An LM308 buffers sensor. The advantage of using V 5 as the Vcc for the
and amplifies the signal from the sensor by 2 so that aevice is that this voltage will not have the switching
the signal uses a greater part of the input range of the noise that the digital V line has. The control inputs and
A/D converter. This improves the noise immunity and dataoutputsoftheA/Dconverterareslmplyconnectedto
resolution. SDK-86 ports as shown.
The ADCO8O8 A/D converter used here is an 8-input Figure 10-34 shows the timing waveforms and param-
data acquisition system. You tell the device which Input eters for the ADCO8O8, Note the sequence in which
signal you want digitized with a 3-bit address you send control signals must be sent to the device. The 3-bit
to the ADC, ADB. and ADA inputs. This 8-input device address of the desired input channel Is first sent to the
was chosen so that other control loops could be added multiplexer inputs. After at least 50 ns. the ALE input
later. Some Schmitt-trigger inverters In a 74C14 are is sent high. After another 2.5s. the START CONVER-
connected as an oscillator to produce a 300-kl-lz clock SION input is sent high and then low. Then the ALE
for the DAS. The voltage drop across an LM329 low-drift input is brought low again. When the END OF CONVER-
FIGURE 10-34 Timing waveforms for the ADCO8O8 data acquisition system.
SION signal from the A/D Converter is found to be high. pin from the relay, and holds the relay in the off position
the 8-bit data value which represents the temperature when the power is first turned on. Port pins, remember.
can be read in. are in a floating state after a reset, so some method
To control the power delivered to the heater, we used must be used to hold external circuitry In a known state
a 25-A. 0-V turn-on, solid-state relay, such as the Potter until the port is Initialized and the desired value is
Brumfield unit described in Chapter 9. WIth this relay output to the port. Now that you know how the hardware
we can control a 120- or 240-V ac-powered hot plate or is connected, we can explain the operation of the control-
immersion heater. To control the amount of heat put ler program.
out by the heater, we vary the duty cycle of pulses sent
(o the relay.
For very low power .applicat ions, a D/A converter and The Controller System Program
a power amplifier could be used to drive the heater.
However. In high-power applications this is not very THE MAINLINE OR EXECUTIVE SECTION
practical because the power amplifier may dissipate as Figure 10-35. pp. 324-29, shows the assembly language
much or more power than the load. For example, an program for our controller system. Refer to the 1lowchart
amplifier intended to control a 5000-W heater over its in Figure 10-32 as you work your way through this
lull range must be able to dissipate more than 5000W. program. The mainline or executive part of the program
The D/A-converter apprpach has the added disadvantage starts by initializing port FFFAI-I for output, the 8259A
that it cannot directly hse (he available ac line voltage. to receive interrupt inputs from the timer and the
l'hc driver transistor on the Input of the solid-state keyboard, and tht 8254 to produce a I -kHz square wave
rela y supplies the drive for the relay, isolates the port on its counter 0 output. In Chapter 8 we described all
73 ;lnitialize 8254 counter 0 for 1-kHz .utput, LSB then MSB, square wave, 8CD
74 0042 BC 37 NOV AL, OO11O11IB
75 0044 BA FF07 NOV DX, OFFO7H ;Point 8254 controL addr
76 0047 EE JT DX. AL ;Send counter 0 comand word
77 0048 80 58 NOV AL, 58H ;Load LSB of count
78 004* BA FF01 NOV DX, OFFO1N ;Point at counter 0 data addr
79 0040 EE JT DX. AL ;Send LSB of count
80 DO4E 80 24 NOV AL, 24H ;Load MSB of count
81 0050 EE JT . DX, AL ;Send MSB of count
82 ;Irutialize variables
83 0051 C6 06 000Sr 3C NOV SETPO!NT, 3CH ;Initialze fina teas, at 60
84 0056 C6 06 0000r 14 NOV CIXINTER, 14H ;Intiatize time counter
85 005B C6 06 0003r 00 NOV L0OPNI, OOH ;Start at first toop
86 0060 C6 06 000lr 01 NOV TIMEHI, O1H
87 0065 C6 06 0002r Cl NOV TIMELO, O1H
88006* C6.060004r00 NOV CURTEMP, OOH
89 ;Enabte interrupt input of 8086
90006F FB STI
91 0070 EB FE I4ERE:JMP HERE Wait for interrupt, if required,
92 0072 90 MOP ; can put more instructions here
93 0073 CE ENDS
94 END
49 OO1D 8A IE 0000e NOV BL. IOOPNtJ q ;service and service that Loop
50 0021 FF 9F 0000r
CALL DWORD P18 LOOP ADOR TABLE (BXJ
51 0025 80 06 0000e 04 ADO LN(1, 04 ;Point at next . p address
52 002A 80 3E 0000e 20 CMP LNL84, 20H ;Was this the Last loop?
53 002F 75 05 .JWE EXIIZ ;Ilo, exit
54 0031 C6 06 0000e 00 NOV LOOPNIJ, 00 ;Yes, get back to first loop
55 0036 iF EXIT2:POP OS ;Restore registers
56 0037 5A Pop DX
57 0038 58 Pop 8)1
58 0039 58 POP AX
59 003A CF IRET
60 0038 CLOCK_Il CX ENDP
61
62 ;DUNMY INTERRUPT PROCEDURE TO SERVICE KEYBOARD
63 003B KEYBOARD PROC FAR
64 ;Keyboard procedure intruct ions
65 0038 BO 20 NOV AL, 001000008 ;OCW2 for non-specific (CI
66 0030 BA FF00 NOV OX, OFFOOH ;Load address for OCW2
67 0040 EE .JT DX, AL ;arid send OCW2 for end of interrupt
68 0041 CF IRET
69 0042 KEYBOARD ENOP
70
71 0042 INT_PROC ENDS
72. END
(b)
;8086 MOOULE 3 PROCEDURES: F10-35C.ASM
2 ;ABSTRACT: Module 3 contains the procedures to service each Loop
3
4 0000 DATA SEGMENT WORD PUBLIC
5 EXTRN TIMEHI :BYTE, TIMELO :BYIE ;1norted into this
6 EXTRN CURTENP:BYTE, SETPOINT:BYTE ;modu(e from the mainline
7 0000 DATA ENDS
8
9 PUBLIC LOOPO, LOOP1, L2, LOOP3, 100P4, LOOP5, LOOP6. 100P7
10
11 0000 COOE SEGMENT WORD PUBLIC
12 EXTRN DISPLAY IT HEAR These procedures can be
13 EXIRN A_D_READ : HEAR found in MOOULE 4 which
14 EXIRN BIPICVT
: HEAR will be linked this module
15 0000 COOt ENDS and MC)ULES 1 and 2
16
17 0000 COOE SEGMENT WORD PUBLIC
18 ASSUME CSCOOE. DS:DATA
1.9
20 ;8086 PROCEDURE - LOOPO
21 ;ABSTRACT This procedure services the teirerature controller
22 ;REGISTERS: Destroys none
23 :PORTS: Uses bit 7 of P2B (FFFAH) as output port control heater.
24 ;PROCEDURES: Uses DISPLAY_IT, A_D_READ, BINCVT from Module 4
25
26 0000 LOOPO PROC FAR
PUSH
27 0000 9C F ;Save registers
28 0001 50 PUSH AX
P29USH
0002 53 8)1
30 0003 51 PUSH CX
31 0004 52 PUSH DX
D32 0005
ECFE CE 0000e TIME HI ;Decrement time for heater on
33 0009 75 50 JNZ EXIT ;Return to interrupt procedure
34 0008 C6 06 OQOOe 01 NOV TIMEHL, 01 ;Reset time high to fall through value
35 0010 BA FFFA NOV DX, OFFFAH ;Point at output port P28 &
36 0013 80 80 NOV AL, 80H ;turn off heater
3? 0015 EE JT DX, AL
38 0016 FE OE 0000e
DEC TIMELO ;Decrement time for heater off
39 OO1A 75 3F JNZ EXIT ;Return to interrupt procedure
40 OO1C B3 00
NOV BL, 00 ;Load charnel address (0)
41 DOlE E8 0000e
CALL A 0 READ ;Do A/D conversion
42 0021 A2 0000e NOV CURTEMP, AL Save current teff,erature
43 0024 (8 0000e CALL BINCVT ;Convert to BCD
44 0027 8A C8 NOV CL, AL ;Put result in CX to display
45 0029 85 00 NOV CH, 00
46 0028 BO CO NOV AL, 00 tee in data field of SOK-86
IC)
these operations in detail, SO we won't dwell on them which allow the user to change set points, stop a process.
here. Also In the mainline we initialize some process or examine the value of process variables at any lime.
variables. We will explain these initializations later when Due to severe space limitations, we can't show here the
they will have more meaning. implementation of the keyboard interrupt procedure.
After enabling the 8086 INTR input with an STI but we will show you how the timer interrupt procedure
instruction, the program then enters a loop and watts and the example PID loop procedure work.
for an interrupt from the user via the keyboard, or
an interrupt from the timer. The keyboard-interrupt THE CLOCK.TICK IN1TERRPT HANDLER
procedure would normally contain a command recog- As we said before, the 8254 is programmed to send a
nizer and subprocedures to implement commands pulse to an interrupt input of the 8259A every millisec-
—44
ANALOC NT[RFACING AND NDUSTRIAL CONTROl 331
structure. By inserting breakpoints with the emulator. Smart machines such as these usually use specially
we were able to see whether execution was getting to designed microprocessors called embedded controllers
each of the eigIt loops. When all this was working. Instead of a general-purpose microprocessor such as
we went on to build and test the temperature-control the 6086 which we usii in the scale and the factory
section. controller examples. The main differences of these em-
For the temperature-control section. we first built the bedded controlled microprocessors is that they have
analog circuitry and tested it. Then we wrote a small additional functions included on the chip with the basic
program to read the temperature from the A/I) converter CPU and they have special instructions for working with
and display the result on the SDK-86 displays. Initially individual bits in a word. In the following sections
then, the loop 0 procedure simply read in the tempera- we give you an overview of a few common embedded
ture, displayed it in binary (hex) form, and returned. controller families. Consult the appropriate Intel hand-
This worked the first time, so we went on to add the books for additk,nal details when you need them.
binary-to-BCD conversion routine and run the result
with the emulator. This was a previously written and
tested module, and when it was added, the result worked The Intel 8051 Embedded Controller Family
fine. Figure iO-37a shows a block diagram of a basic 8051
Next we added a couple of instructions to turn the family controller and Figure l0-37b summarizes some
heater on during one execution of loop 0 and turn the of the features of the members of the family. These
heater off during the next time through loop 0. We then controllers are 8-bit units which can address up to 64
used an oscilloscope to check that the solid-state relay Kbytes of memoxy. All of the family members have some
was getting turned on and off correctly. RAM on the chip, and different members of the family
Finally, we added the actual duty cycle and control have some ROM or EPROM also Included on the chip.
Instructions and sat back waiting for the system to heat As shown in Figure lO-37b. members of this family
up a big container of water for tea. also have programmable timers and priority interrupt
The actual development cycle will obviously be some- controllers Included on the chips.
what different for every instrument developed. The main 11 an application does not require any memory other
points here are to develop and test both the hardware than that included on the chip, then all four parts are
and the software in small modules. To speed up the available for use as input or output ports. If additional
debugging process, take the time to learn to use all or memory is needed, then port 0 and port 2 can be
most of the power of the emulator and system you are programriied to function as a multiplexed address/data
working with. bus. When used with extern memo two lines on port
3 are used to generate the RD and WR signals.
The devices in the 8051 family also contain serial
ROBOTICS AND EMBEDDED CONTROL data interface circuitry. When this feature is used.
In recent years the term robot has become a "buzzword' two pins on port 3 function as the RxD and TxD
In the media and in many people's minds. Science fiction lines. Figure l0-37c shows the special uses of the port
movies have helped us form an image of robots as 3 pins.
mobile, rational companions. Robots, however, have
many forms, and In operation they are simply a combina- The Intel 8096 Embedded Controller Family
tion of feedback control systems such as we described
In the previous section. This Is why we have not Included Figure l0-38a. p. 334, shows a block diagram for the
a chapter dedIcated Just to robotics. The controller for Intel 8096 family of 16-bit microcontrollers. One of the
the Rhino robot arm shown In Figure 9-42. br example. most important features of the members of this family
uses optical encoders to detect the position of Its differ- is the 232-byte register file and the register ALU (RALU)
ent Joints, motors (actuators) to move each Joint to a shown in the center of Figure lO-38a. Instead of using
desired position, and a microcomputer to control the a single accumulator register as the 8086 does, the ALU
motors based on feedback from the sensors. Large in the 8096 famil, devices can perform most operations
Industrial robots such as those that weld or spray-paint on any of the registers in the register file. This structure
cars may also use tactile or visual sensors, and the is referred to as register-to-regIster architecture. The
actuators maybe hydraulic or pneumatic, but the control large number of registers makes it possible to have many
principle is the same, A microcomputer or several micro- data bytes in registers where they can be very quickly
computers use feedback from the various sensors to accessed. Also, since tire contents of any register can be
control one or more actuators. output to a port, the 110 'bottleneck" of 8086-type
Most of you have probably used some simple robots processors Is elimInated.The 8086, remember, requires
around your home without realizing it. One example Is that data be output from or input to AL/AX.
an electric garage door openei which starts to open or Other features found in all the 8096 family devices
close when you tell it to and then stops when a sensor are five ports which can be programmed for use in a
indicates that it is open or closed as desired. Common variety of ways. In addition to their use as standard
household examples of microcomputer-controlled robots ports. ports 3 and 4 can be used as a multiplexed
are a microwave oven with a temperature probe, a address/data bus to access external memory and ports.
programmable sewing machine, a remote-control Stereo Port 2 can be programmed for use as a serial port and!
system. etc. or an output for the pulse-width-modulated signal. The
INTRUPT I 25 Vylts
TR4cB I
Th4ER 0
cOuNTtR
JPuTs
Iin
osc Bus SLRIAL
4 I/O PORTS
PORT
II 1*0 XD
P0 P2 Pt PS
ADORESS/DATA
270251-I
(a)
Device ________
___________ intirnal Memory
_______ Tkn.j,/
Progrm o.t. Event Counter,
8052AH OKx8AOM 256xORAM 3*16-Bit 6
OO51AH 4Kx8R0M 1288 RAM 2*16-Bit
8051 4Kx$ROM 5
I28xBRAM 2*16-Bit 5
8032AH none 258x8RAM 3*16-Bit
8031AH 6
flOfl 128*0 RAM 2*16-Bit 5
8031 none 128X8RAM 2*16-Bit
87511-I 4Kx8EPROM 5
128x6flAM 2*16-Bit 5
875lH-8 4Kx8EPROM I28x8flAM 2*16-Bit 5
fbi
Poll 3 also serves the functions of va41ous spec*al
features ot the MCS-51 Family, as listed below Figure 10-38b shows the numbering for the different
port
members of the 8096 family so you can see the options
pin AJt.rnativ. Function available in different parts. As you can see, devices are
P3.0 RXD (serial input poll) available with 8 Kbytes of internal EPROM. 8 Kbyte, of
P3.1 TXO (serial output port) Internal mask-programmed ROM. or no internal ROM.
P3.2 IT (external Intemjpt 0) Also note that some members of the 8096 family contain
P3.3 INTl (external lnternt 1) a 10-bit successive-approximation AID converter. As
P3.4 TO crner 0 external input) shown in Figure l0-38c. this AID has a sample-and-hold
P3.5 Ti(Thter I external Input)
P3.6 WP (external data memory wflte trobe) and an 8-input analog multiplexer on its input. This
P3.7 (external data memory read strobe) allows it to digitize any of eight input signals under
program control.
Ic) The 8096 instructions are designed for fast operations
FIGURE 10-37 8051 family. (a) Block diagram. (b) Family on registers and for easily working with individual bits
features, (c) Port pin uses. (Intel Corporation) in data words. The 8096 also has multiply and divide
instructions. From the scale and temperature controller
examples earlier in the chapter. you should see that
these features optimize the devices for use in hardware
PWM signal is software programmable and cart be used control applications.
to control the speed of a small motor or the duty cycle
of a heater, as we described earlier In the chapter.
The 8096 family devices also have two programmable The 80186 and 80188 Microprocessors
counters and 2) hardware and software Interrupt types. The 8051 and 8096 embedded controllers we described
Note that both the clock generator and the baud-rate lit the preceding sections have different Instruction sets
generator are included in the basic architecture. and vety different architectures from the 8086, which
ANAI.OG INTERFACING AND INDUSTRIAL CONTROL 333
POWE6 FREQUENCY
VRO AHGNO DOWN REFERENCE
Ow-cHP
EPRO676xeH
nJ
I
232 tuENOfi
________________________________
BYTE GSTER SGNAI.S
CONL
IWATcH000I. u :iiiwi
klux
Flu
___ I I
_________ __________ II
F
•l I I 6
1
•POQT3
: _________
• OATA
Jeus
Put.St SERIAL IJ_/IADOR
wiDTh PORT ;—) RT4
SPEED
I/O
P21
2702a6- 13
lc(
FIGURE 10-38 80% family. (a) Block diagram. (b) Family features.
334 (C) ND converter diagram. (Intel Corporatiol
INT3/INTAI
f
TMROUT1 TMAOUTO
CLKOUT GNO TMR IN TMR IN
PROGRAMMABLE 1
TIMERS
0 1 2
16-BIT MAX COUNT L'\
ALL) PROGRAMMABLE REGISTER B
INTERRUPT MAX COUNT
CONTROLLER REGISTER A
CLOCK
NE R AT 16-BIT
GENERAL I
1 i CONTROL REGISTERS
PURPOSE I I CONT AOL 16-BIT
REGISSJ j REGISTERS COUNT REGISTER
_________ ORGO
DRO1
(PROGRAMMABLE
I I DMA UNIT
CHIP-SELECT
UNIT RCE
SR DY - _________
AR))? BUS INTERFACE l6-BIT DESTINATION
rEST UNIT SEGMENT
HOLD REGISTEA5 PROGRAMMABLE
HLOA 6-BYTE CONTROL
4ES PREFETCH REGISTERS
UCS
I +5D V
LCS
L6 RD ADO- A16/S3
A015 A19/S6
01/N BHE/S7
'l _
This digital filter approach can easily produce a filter
response which is difficult, if not impossible, to produce "s, 3fo
with analog Circuitry. The digital approach has the
further advantage that the filter response can be changed V0
'\., Sb
under program control.
To most people it is not Intuitively obvious how an
"s.d ito
A/D converter. microcomputer. and 1)/A converter can
produce the same effect on a signal as. for example, an
RC low-pass filter. Before we can show you how digital
filters work, we need to review some basic signal relation-
ships and analog filter characteristics.
iv:-n-n--n-n- (a) (b)
-
*10
Time-Domain and Frequency-Domain
View of a Square Wave
There are two ways of producing or describing a wave-
to
form such as the square wave. One way is with a circuit
such as that in Figure 1 O-40a. If the switch is repeatedly
flipped up for one-half the period and down for one-half
the period, the output waveform will be a square wave —10
centered around 0 V. This way of producing or describing 3.33
a square wave is referred to as the time-domaf it method.
310 C
The second way of producing a square wave of a given
frequency is by adding together a series of sine-wave —3-33
signals which have Just the right amplitude and phase
relationships. This method of producing or describing
a square wave is called the frequency-domain method.
Figure lO-40b shows a circuit which generates a square A £4! A
wave by adding sine-wave signals.
Remember from basic electric circuits that when
voltage sources are connected in series, the output
voltage at any time is the sum of the individual voltages.
The lowest-frequency sine-wave signal added here has
the same frequency as the desired square-wave signal.
Added to this is a signal with a frequency 3 times the
frequency of the fundamental frequency, a signal with
a.frequency 5 times the frequency of the fundamental
frequency, a signal with a frequency 7 times the funda-
mental frequency. etc. These multiples of the fundamen-
tal frequency are called harmonics. As we said before.
Cd)
the added harmonics must have the right amplitude
and phase relationships to produce a square wave when
added. Figure 1O-40c shows the required phase and
relative amplitude relationships.
To help you further visualize this addition process.
Figure l0-40d shows the resultant wave1om that will
be produced by adding Just the fundamental frequency
and the third harmonic. It is somewhat difficult to show.
but the more harmonics you add, the more the resultant 0
waveform approaches. a square wave.
Ce)
Mathematically, the equation for this square wave can
be expressed in terms of a fundamental frequency and FIGURE 10-40 (a) Time-domain method of producing a
harmonics as square wave. (b) Frequency-domain method of
producing a square wave. (C) Amplitude and phase
sin (217fl) + sin 3(27rJ1) relationships of first, third, fifth, and seventh harmonics
which produce a square wave when added. (d I Addition
of first and third harmonics. (e) Amplitude versus
+ sin 5(2rJtl + Sifl 7(2lTJt) + frequency graph (frequency spectrum) for square wave.
I
the upper harmonies will be attenuated more than the the oldest sample and use the latest 100 samples to
compute the next output value. The output value at any
T'.
time then will be a sort of "average" of the last 100
R
samples. To help visualize this, you might think of the
process as sliding a window of 100 samples along the
waveform and using some algorithm to compute an
C VOr "average" of the samples.
If the window is positioned so that all 100 samples
come from a section of the square wave where the
(a) waveform is at —V. as shown in Figure 10'42a. then
the computed output value will be —V. As the window
slides to the right so that it includes some samples
from the high section, as shown in Figure lO-42b, the
"average" will increase, so the computed output value
U' will increase. The risetime of the output or the rate at
which the output value increases is determined by the
FIGURE 10-41 Ia) Simple RC circuit. (b) Output weight given to new samples versus the weight given to
waveform trom RC circuit. old samples in computing the 'average." Using a low-
RDY
MST RB
M M lOST RB
STAB -'K 1 U U P X R,W
x x E
XO(31-0(
0(31-0) 7p XA112-O)
Al 23-01
H
E
R
32.j. ..24j24 32j. J24 A
32-j_ -f.24 L
PROGRAM COUNTER/ DMA a
INSTRUCTION REGISTER CPU CONTROLLER U
I_______________
DSP microcompoter I Digilil-to- _______ Low-pux YeW
-_] Clock I
After the anti-alias filter a sample-and-hold is used to Several DOS-compatible software packages are avail-
keep the value on the input of the A/D constant during able to help perform these tasks. Examples are the
conversion. A simple low-pass analog filter Is connected Digital Filter Design Package-2 (DFDP2) from Atlanta
to the output of the 0/A converter to "sniooth' the output Signal Processors Inc. and the Filter Design and Analysis
signal. System (FDAS2) from Momentum Data Systems. When
For development purposes and experimentation with given the desired filter type, break frequencies, and
a PC-type microcomputer, the DSP-16 board from Arid attenuation rates, these packages tell you if the desired
Corp. has two 16-bit, 50-kHz A/D converters, a 40-MHz filter can be implemented and generate the required
TMS320C25, and two 16-bit 50-kllz 0/A converters. The coefficients. After the coefficients are generated, another
dual channels allow two signals to be processed at the module in these software packages can be used to
same time. Other boards allow different combinations produce the assembly language progam for the DSP
of sampling rate and number of channels. microprocessor. To give you an example of how simple
the actual program is. Figure 10-46 shows a procedure
Digital Filter Software and Development Tools which implements a bandpass filter on the TMS320C25
in Figure 10-45. The RPTK 68 Instruction in this
As perhaps you can guess from the algorithms in Figure procedure causes the following instruction to be re-
10-43. developing the program for a digital filter involves peated 68 times. The MACD FDATA+ >FDOO. - that Is
two main tasks. The first task is to determine the repeated 68 times will multiply a data memory value
coefficients by which the terms in the equation will be by a program memory value, add the result to an
multiplied to implement the desired filter. The second accumulator, and decrement the pointer to point to the
task is to write a program which reads in values from next operands. These two Instructions then do most of
the A/fl converter, computes an output value, and sends the work of computing an output value.
the completed value to the D/A converter at the right Once the coefficients and program for a digital flltr
time. have been produced. the next step is to test the result.
MPYK 0 aP 0
ZAC *AC = 0
LRLK DELAR,ZLAST *INDEX POINTS TO Z-N
RPTK 68
MACI FDATA+>F000, * - *MULTXPLY,ACCUM.artd DELAY
APAC *FORM RESULT
SACH VSAMPL,O *SAVE OUTPUT
RET * RETURN
*
PENO
END
FIGURE 10-46 TMS320C25 digital filter program. (R.W. Schafer. "The Math
Behind the MIPS: DSP Basics." Electronic Design, September 1988)