8051 Boolean
8051 Boolean
NOTE
AP-70
ApiiI 1980
Using the Intel MCS-51
Boolean Processing
Capabilities
JOHN WHARTON
MICROCONTROLLFR APPLICATIONS
Order Number 203830-001
Information in this document is provided in connection with Intel products Intel assumes no liability whatsoev-
er including infringement of any patent or copyright for sale and use of Intel products except as provided in
Intels Terms and Conditions of Sale for such products
Intel retains the right to make changes to these specifications at any time without notice Microcomputer
Products may have minor variations to this specification known as errata
Other brands and names are the property of their respective owners
Since publication of documents referenced in this document registration of the Pentium OverDrive and
iCOMP trademarks has been issued to Intel Corporation
Contact your local Intel sales office or your distributor to obtain the latest specifications before placing your
product order
Copies of documents which have an ordering number and are referenced in this document or other Intel
literature may be obtained from
Intel Corporation
PO Box 7641
Mt Prospect IL 60056-7641
or call 1-800-879-4683
COPYRIGHT INTEL CORPORATION 1996
USING THE INTEL MCS-51
BOOLEAN PROCESSING
CAPABILITIES
CONTENTS PAGE
10 INTRODUCTION 1
20 BOOLEAN PROCESSOR
OPERATION 2
Processing Elements 3
Direct Bit Addressing 5
Instruction Set 8
Simple Instruction Combinations 10
30 BOOLEAN PROCESSOR
APPLICATIONS 12
Design Example 1Bit Permutation 12
Design Example 2Software Serial
IO 17
Design Example 3Combinational
Logic Equations 19
Design Example 4Automotive
Dashboard Functions 23
Design Example 5Complex Control
Functions 30
Additional Functions and Uses 39
40 SUMMARY 40
APPENDIX A A-1
AP-70
10 INTRODUCTION
The InteI miciocontioIIei famiIy now has thiee new
membeis: the InteI 8031, 8051, and 8751 singIe-chip
miciocomputeis. These devices, shown in Figuie 1, wiII
aIIow whoIe new cIasses of pioducts to benefit fiom
iecent advances in Integiated FIectionics. Thanks to
InteIs new HMOS technoIogy, they piovide Iaigei pio-
giam and data memoiy spaces, moie fIexibIe I/O and
peiipheiaI capabiIities, gieatei speed, and Iowei system
cost than any pievious-geneiation singIe-chip micio-
computei.
2038301
Figure 1 8051 Family Pinout Diagram
TabIe 1 summaiizes the quantitative diffeiences be-
tween the membeis of the MCS-48 and 8051 famiIies.
The 8751 contains 4K bytes of FPROM piogiam mem-
oiy fabiicated on-chip, whiIe the 8051 iepIaces the
FPROM with 4K bytes of Iowei-cost mask-
piogiammed ROM. The 8031 has no piogiam memoiy
on-chip, instead, it accesses up to 64K bytes of piogiam
memoiy fiom exteinaI memoiy. Otheiwise, the thiee
new famiIy membeis aie identicaI. Thioughout this
Note, the teim 8051 wiII iepiesent aII membeis of the
8051 FamiIy, unIess specificaIIy stated otheiwise.
The CPU in each miciocomputei is one of the indus-
tiys fastest and most efficient foi numeiicaI caIcuIa-
tions on byte opeiands. But contioIIeis often deaI with
bits, not bytes: in the ieaI woiId, switch contacts can
onIy be open oi cIosed, indicatois shouId be eithei Iit oi
daik, motois aie eithei tuined on oi off, and so foith.
Foi such contioI situations the most significant aspect
of the MCS-51 aichitectuie is its compIete haidwaie
suppoit foi one-bit, oi Boolean vaiiabIes (named in
honoi of Mathematician Oeoige BooIe) as a sepaiate
data type.
The 8051 incoipoiates a numbei of speciaI featuies
which suppoit the diiect manipuIation and testing of
individuaI bits and aIIow the use of singIe-bit vaiiabIes
in peifoiming IogicaI opeiations. Taken togethei, these
featuies aie iefeiied to as the MCS-51 Boolean Proces-
sor. WhiIe the bit-piocessing capabiIities aIone wouId be
adequate to soIve many contioI appIications, theii tiue
powei comes when they aie used in conjunction with
the miciocomputeis byte-piocessing and numeiicaI ca-
pabiIities.
Many concepts embodied by the BooIean Piocessoi wiII
ceitainIy be new even to expeiienced miciocomputei
system designeis. The puipose of this AppIication Note
is to expIain these concepts and show how they aie
used.
Foi detaiIed infoimation on these paits iefei to the In-
tel Microcontroller Handbook oidei numbei 210918.
The instiuction set, assembIy Ianguage, and use of the
8051 assembIei (ASM51) aie fuithei desciibed in the
MCS-51 Macro Assembler Users Guide for DOS
Systems oidei numbei 122753.
Table 1 Features of Intels Single-Chip Microcomputers
EPROM ROM External Program Data Instr Input
Interrupt Reg
Program Program Program Memory Memory Cycle Output
Sources Banks
Memory Memory Memory (IntMax) (Bytes) Time Pins
8748 8048 8035 1K 4K 64 25 ms 27 2 2
8049 8039 2K 4K 128 136 ms 27 2 2
8751 8051 8031 4K 64K 128 10 ms 32 5 4
1
AP-70
20 BOOLEAN PROCESSOR
OPERATION
The BooIean Piocessing capabiIities of the 8051 aie
based on concepts which have been aiound foi some
time. DigitaI computei systems of wideIy vaiying de-
signs aII have foui functionaI eIements in common (Fig-
uie 2):
a centiaI piocessoi (CPU) with the contioI, timing,
and Iogic ciicuits needed to execute stoied instiuc-
tions:
a memoiy to stoie the sequence of instiuctions mak-
ing up a piogiam oi aIgoiithm:
data memoiy to stoie vaiiabIes used by the pio-
giam:
and
some means of communicating with the outside
woiId.
The CPU usuaIIy incIudes one oi moie accumuIatois oi
speciaI iegisteis foi computing oi stoiing vaIues duiing
piogiam execution. The instiuction set of such a
piocessoi geneiaIIy incIudes, at a minimum, opeiation
cIasses to peifoim aiithmetic oi IogicaI functions on
piogiam vaiiabIes, move vaiiabIes fiom one pIace to
anothei, cause piogiam execution to jump oi condi-
tionaIIy bianch based on iegistei oi vaiiabIe states, and
instiuctions to caII and ietuin fiom subioutines. The
piogiam and data memoiy functions sometimes shaie a
singIe memoiy space, but this is not aIways the case.
When the addiess spaces aie sepaiated, piogiam and
data memoiy need not even have the same basic woid
width.
A digitaI computeis fIexibiIity comes in pait fiom
combining simpIe fast opeiations to pioduce moie com-
pIex (aIbeit sIowei) ones, which in tuin Iink togethei
eventuaIIy soIving the piobIem at hand. A foui-bit CPU
executing muItipIe piecision subioutines can, foi exam-
pIe, peifoim 64-bit addition and subtiaction. The sub-
ioutines couId in tuin be buiIding bIocks foi fIoating-
point muItipIication and division ioutines. FventuaIIy,
the foui-bit CPU can simuIate a fai moie compIex vii-
tuaI machine.
In fact, any digitaI computei with the above foui func-
tionaI eIements can (given time) compIete any aIgo-
iithm (though the pioveibiaI ioom fuII of chimpanzees
at woid piocessois might fiist ie-cieate Shakespeaies
cIassics and this AppIication Note)! This fact offeis Iit-
tIe consoIation to pioduct designeis who want pio-
giams to iun as quickIy as possibIe. By definition, a
ieaI-time contioI aIgoiithm must pioceed quickIy
enough to meet the pieoidained speed constiaints of
othei equipment.
One of the factois deteimining how Iong it wiII take a
miciocomputei to compIete a given choie is the num-
bei of instiuctions it must execute. What makes a given
computei aichitectuie paiticuIaiIy weII- oi pooiIy-suit-
ed foi a cIass of piobIems is how weII its instiuction set
matches the tasks to be peifoimed. The bettei the
piimitive opeiations coiiespond to the steps taken by
the contioI aIgoiithm, the Iowei the numbei of instiuc-
tions needed, and the quickei the piogiam wiII iun. AII
eIse being equaI, a CPU suppoiting 64-bit aiithmetic
diiectIy couId cIeaiIy peifoim fIoating-point math fast-
ei than a machine bogged-down by muItipIe-piecision
subioutines. In the same way, diiect suppoit foi bit
manipuIation natuiaIIy Ieads to moie efficient pio-
giams handIing the binaiy input and output conditions
inheient in digitaI contioI piobIems.
2038302
Figure 2 Block Diagram for Abstract Digital Computer
2
AP-70
Processing Elements
The intioduction stated that the 8051s bit-handIing ca-
pabiIities aIone wouId be sufficient to soIve some con-
tioI appIications. Lets see how the foui basic eIements
of a digitaI computei-a CPU with associated iegisteis,
piogiam memoiy, addiessabIe data RAM, and I/O ca-
pabiIity-ieIate to BooIean vaiiabIes.
CPU. The 8051 CPU incoipoiates speciaI Iogic devoted
to executing seveiaI bit-wide opeiations. AII toId, theie
aie 17 such instiuctions, aII Iisted in TabIe 2. Not
shown aie 94 othei (mostIy byte-oiiented) 8051 instiuc-
tions.
Program Memory. Bit-piocessing instiuctions aie
fetched fiom the same piogiam memoiy as othei aiith-
metic and IogicaI opeiations. In addition to the instiuc-
Table 2 MCS-51 Boolean
Processing Instruction Subset
Mnemonic Description Byte Cyc
SETB C Set Carry flag 1 1
SETB bit Set direct Bit 2 1
CLR C Clear Carry flag 1 1
CLR bit Clear direct bit 2 1
CPL C Complement Carry flag 1 1
CPL bit Complement direct bit 2 1
MOV Cbit Move direct bit to Carry flag 2 1
MOV bitC Move Carry flag to direct bit 2 2
ANL Cbit AND direct bit to Carry flag 2 2
ANL Cbit AND complement of direct 2 2
bit to Carry flag
ORL Cbit OR direct bit to Carry flag 2 2
ORL Cbit OR complement of direct 2 2
bit to Carry flag
JC rel Jump if Carry is flag is set 2 2
JNC rel Jump if No Carry flag 2 2
JB bitrel Jump if direct Bit set 3 2
JNB bitrel Jump if direct Bit Not set 3 2
JBC bitrel Jump if direct Bit is set 3 2
Clear bit
Address mode abbreviations
CCarry flag
bit128 software flags any IO pin control or status
bit
relAll conditional jumps include an 8-bit offset byte
Range is a127 b128 bytes relative to first byte of the
following instruction
AII mnemonics copyiighted
R0
ANL AFLGMASK
MOV
R0A
e
6 6 150 CLR USER
FLG 2 1
11
AP-70
Table 4b Replacing 8048 Instruction Sequences with Single 8x51 Instructions (Continued)
8048
Bytes Cycles mSec
8x51
Bytes Cycles mSec
Instruction Instruction
Flag Testing
Jump if Software Flag is 0
JF0 $
a
4
JMP offset
e
4 4 100 JNB F0rel 3 2
Jump if Accumulator bit is 0
CPL A
JB7 offset
CPL A
e
4 4 100 JNB ACC7rel 3 2
Peripheral Polling
Test if Input Pin is Grounded
IN AP1
CPL A
JB3 offset
e
4 5 125 JNB P13rel 3 2
Test if Interrupt Pin is High
JN1 $
a
4
JMP offset
e
4 4 100 JB INT0rel 3 2
30 BOOLEAN PROCESSOR
APPLICATIONS
So what! Then what does aII this buy you!
Qualitatively nothing. AII the same capabiIities could
be (and often have been) impIemented on othei ma-
chines using awkwaid sequences of othei basic opeia-
tions. As mentioned eaiIiei, any CPU can soIve any
piobIem given enough time.
Quantitatively the diffeiences between a soIution aI-
Iowed by the 8051 and those iequiied by pievious ai-
chitectuies aie numeious. What the 8051 FamiIy buys
you is a fastei, cIeanei, Iowei-cost soIution to micio-
contioIIei appIications.
The opcode space fieed by condensing many specific
8048 instiuctions into a few geneiaI opeiations has been
used to add new functionaIity to the MCS-51 aichitec-
tuie-both foi byte and bit opeiations. 144 softwaie
fIags iepIace the 8048s two. These fIags (and the caiiy)
may be diiectIy set, not just cIeaied and compIemented,
and aII can be tested foi eithei state, not just one. Opei-
ating mode bits pieviousIy inaccessibIe may be iead,
tested, oi saved. Situations wheie the 8051 instiuction
set piovides new capabiIities aie contiasted with 8048
instiuction sequences in TabIe 4b. Heie the 8051 speed
advantage ianges fiom 5x to 15x!
Combining BooIean and byte-wide instiuctions can
pioduce gieat syneigy. An MCS-51 based appIication
wiII piove to be:
simpIei to wiite since the aichitectuie coiieIates
moie cIoseIy with the piobIems being soIved:
easiei to debug because moie individuaI instiuctions
have no unexpected oi undesiiabIe side-effects:
moie byte efficient due to diiect bit addiessing and
piogiam countei ieIative bianching:
fastei iunning because fewei bytes of instiuction
need to be fetched and fewei conditionaI jumps aie
piocessed:
Iowei cost because of the high IeveI of system-inte-
giation within one component.
These iathei unabashed cIaims of exceIIence shaII not
go unsubstantiated. The iest of this chaptei examines
Iess tiiviaI tasks simpIified by the BooIean piocessoi.
The fiist thiee compaie the 8051 with othei micio-
piocessois, the Iast two go into 8051-based system de-
signs in much gieatei depth.
Design Example 1Bit Permutation
Fiist off, weII use the bit-tiansfei instiuctions to pei-
mute a Iengthy pattein of bits.
12
AP-70
A steadiIy incieasing numbei of data communication
pioducts use encoding methods to piotect the secuiity
of sensitive infoimation. By Iaw, inteistate financiaI
tiansactions invoIving the FedeiaI banking system must
be tiansmitted using the FedeiaI Infoimation Pio-
cessing Data Encryption Standard (DFS).
BasicaIIy, the DFS combines eight bytes of pIaintext
data (in binaiy, ASCII, oi any othei foimat) with a 56-
bit key, pioducing a 64-bit enciypted vaIue foi tians-
mission. At the ieceiving end the same aIgoiithm is
appIied to the incoming data using the same key, iepio-
ducing the oiiginaI eight byte message. The aIgoiithm
used foi these peimutations is fixed, diffeient usei-de-
fined keys ensuie data piivacy.
It is not the puipose of this note to desciibe the DFS in
any detaiI. Suffice it to say that enciyption/deciyption
is a Iong, iteiative piocess consisting of iotations, excIu-
sive -OR opeiations, function tabIe Iook-ups, and an
extensive (and quite bizaiie) sequence of bit peimuta-
tion, packing, and unpacking steps. (Foi fuithei detaiIs
iefei to the June 21, 1979 issue of Electronics maga-
zine.) The bit manipuIation steps aie incIuded, it is iu-
moied, to impede a geneiaI puipose digitaI supeicom-
putei tiying to bieak the code. Any aIgoiithm impIe-
menting the DFS with pievious geneiation micio-
piocessois wouId spend viituaIIy aII of its time diddIing
bits.
The bit manipuIation peifoimed is typified by the Key
ScheduIe CaIcuIation iepiesented in Figuie 9. This step
is iepeated 16 times foi each key used in the couise of a
tiansmission. In essence, a seven-byte, 56-bit Shifted
Key Buffei is tiansfoimed into an eight-byte, Peimu-
tation Buffei without aIteiing the shifted Key. The
aiiows in Figuie 9 indicate a few of the tiansIation
steps. OnIy six bits of each byte of the Peimutation
Buffei aie used, the two high-oidei bits of each byte aie
cIeaied. This means onIy 48 of the 56 Shifted Key Buff-
ei bits aie used in any one iteiation.
Diffeient miciopiocessoi aichitectuies wouId best im-
pIement this type of peimutation in diffeient ways.
Most appioaches wouId shaie the steps of Figuie 10a:
InitiaIize the Peimutation Buffei to defauIt state
(ones oi zeioes):
IsoIate the state of a bit of a byte fiom the Key
Buffei. Depending on the CPU, this might be ac-
compIished by iotating a woid of the Key Buffei
thiough a caiiy fIag oi testing a bit in memoiy oi an
accumuIatoi against a mask byte:
Peifoim a conditionaI jump based on the caiiy oi
zeio fIag if the Peimutation Buffei defauIt state is
coiiect:
Otheiwise ieveise the coiiesponding bit in the pei-
mutation buffei with IogicaI opeiations and mask
bytes.
Fach step above may iequiie seveiaI instiuctions. The
Iast thiee steps must be iepeated foi aII 48 bits. Most
miciopiocessois wouId spend 300 to 3,000 miciosec-
onds on each of the 16 iteiations.
Notice, though, that this fIow chait Iooks a Iot Iike
Figuie 8. The BooIean Piocessoi can peimute bits by
simpIy moving them fiom the souice to the caiiy to the
destination-a totaI of two instiuctions taking foui
bytes and thiee micioseconds pei bit. Assume the Shift-
ed Key Buffei and Peimutation Buffei both ieside in
bit-addiessabIe RAM, with the bits of the foimei as-
signed symboIic names SKB
-
1, SKB
-
2, . . . SKB
-
56, and that the bytes of the Iattei aie named PB
-
1,
. . . PB
-
8. Then woiking fiom Figuie 9, the softwaie
foi the peimutation aIgoiithm wouId be that of Fxam-
pIe 1a. The totaI ioutine Iength wouId be 192 bytes,
iequiiing 144 micioseconds.
Permuted and Shifted 56-Bit Key Buffer
2038305
48-Bit Key K
I
Figure 9 DES Key Schedule Transformation
13
AP-70
2038306
Figure 10a Flowchart for Key Permutation Attempted with a Byte Processor
14
AP-70
2038307
Figure 10b DES Key Permutation with Boolean Processor
15
AP-70
The aIgoiithm of Figuie 10b is just sIightIy moie effi-
cient in this time-ciiticaI appIication and iIIustiates the
syneigy of an integiated byte and bit piocessoi. The
bits needed foi each byte of the Peimutation Buffei aie
assimiIated by Ioading each bit into the caiiy (1 ms.)
and shifting it into the accumuIatoi (1 ms.). Fach byte
is stoied in RAM when compIeted. Foity-eight bits
thus need a totaI of 112 instiuctions, some of which aie
Iisted in FxampIe 1b.
Woist-case execution time wouId be 112 micioseconds,
since each instiuction takes a singIe cycIe. Routine
Iength wouId aIso deciease, to 168 bytes. (ActuaIIy, in
the context of the compIete enciyption aIgoiithm, each
peimuted byte wouId be piocessed as soon as it is as-
simiIated-saving memoiy and cutting execution time
by anothei 8 ms.)
To date, most banking teiminaIs and othei systems us-
ing the DFS have needed speciaI boaids oi peiipheiaI
contioIIei chips just foi the enciyption/deciyption pio-
cess, and stiII moie haidwaie to foim a seiiaI bit stieam
foi tiansmission (Figuie 11a). An 8051 soIution couId
pack most of the entiie system onto the one chip (Fig-
uie 11b). The whoIe DFS aIgoiithm wouId iequiie Iess
than one-fouith of the on-chip piogiam memoiy, with
the iemaining bytes fiee foi opeiating the banking tei-
minaI (oi whatevei) itseIf.
Moieovei, since tiansmission and ieception of data is
peifoimed thiough the on-boaid UART, the unen-
ciypted data (pIaintext) nevei even exists outside the
miciocomputei! NatuiaIIy, this wouId affoid a high de-
giee of secuiity fiom data inteiception.
FxampIe 1. DFS Key Peimutation Softwaie.
a.) Biute Foice technique
N07 0,$KB l
N07 FB l.l,0
N07 0,$KB 2
N07 FB 4.0,0
N07 0,$KB
N07 FB 2.5,0
N07 0,$KB 4
N07 FB l.0,0
... .....
... .....
N07 0,$KB 55
N07 FB 5.0,0
N07 0,$KB 58
N07 FB 7.2,0
b.) Using AccumuIatoi to CoIIect Bits
0LR A
N07 0,$KB l4
RL0 A
N07 0,$KB l7
RL0 A
N07 0,$KB ll
RL0 A
N07 0,$KB 24
RL0 A
N07 0,$KB l
RL0 A
N07 0,$KB 5
RL0 A
N07 FB l,A
... .....
... .....
N07 0,$KB 29
RL0 A
N07 0,$KB 2
RL0 A
N07 FB 3,A
16
AP-70
2038308
a) Using Multi-Chip Processor Technology
2038309
b) Using One Single-Chip Microcomputer
Figure 11 Secure Banking Terminal Block Diagram
Design Example 2Software
Serial IO
An exeicise often imposed on beginning miciocomput-
ei students is to wiite a piogiam simuIating a UART.
Though doing this with the 8051 FamiIy may appeai to
be a moot point (given that the haidwaie foi a fuII
UART is on-chip), it is stiII instiuctive to see how it
wouId be done, and maintains a pioduct Iine tiadition.
As it tuins out, the 8051 miciocomputeis can ieceive oi
tiansmit seiiaI data via softwaie veiy efficientIy using
the BooIean instiuction set. Since any I/O pin may be a
seiiaI input oi output, seveiaI seiiaI Iinks couId be
maintained at once.
Figuies 12a and 12b show aIgoiithms foi ieceiving oi
tiansmitting a byte of data. (Anothei section of pio-
giam wouId invoke this aIgoiithm eight times, synchio-
nizing it with a stait bit, cIock signaI, softwaie deIay, oi
timei inteiiupt.) Data is ieceived by testing an input
pin, setting the caiiy to the same state, shifting the
caiiy into a data buffei, and saving the paitiaI fiame in
inteinaI RAM. Data is tiansmitted by shifting an out-
put buffei thiough the caiiy, and geneiating each bit
on an output pin.
A side-by-side compaiison of the softwaie foi this com-
mon bit-banging appIication with thiee diffeient mi-
ciopiocessoi aichitectuies is shown in TabIe 5a and 5b.
The 8051 soIution is moie efficient than the otheis on
eveiy count!
17
AP-70
20383010
a) Reception
20383011
b) Transmission
Figure 12 Serial IO Algorithms
18
AP-70
Table 5 Serial IO Programs for Various Microprocessors
20383030
Design Example 3Combinatorial
Logic Equations
Next weII Iook at some simpIe uses foi bit-test instiuc-
tions and IogicaI opeiations. (This exampIe is aIso pie-
sented in AppIication Note AP-69.)
ViituaIIy aII haidwaie designeis have soIved compIex
functions using combinatoiiaI Iogic. WhiIe the haid-
waie invoIved may vaiy fiom ieIay Iogic, vacuum
tubes, oi TTL oi to moie esoteiic technoIogies Iike fIu-
idics, in each case the goaI is the same: to soIve a piob-
Iem iepiesented by a IogicaI function of seveiaI BooIean
vaiiabIes.
Figuie 13 shows TTL and ieIay Iogic diagiams foi a
function of the six vaiiabIes U thiough Z. Fach is a
soIution of the equation.
Q e (U (V a W)) a (X Y) a Z
Fquations of this soit might be ieduced using Kai-
naugh Maps oi aIgebiaic techniques, but that is not the
puipose of this exampIe. As the Iogic compIexity in-
cieases, so does the difficuIty of the ieduction piocess.
Fven a minoi change to the function equations as the
design evoIves wouId iequiie tedious ie-ieduction fiom
sciatch.
19
AP-70
20383012
Q e (U (V a W)) a (X Y) a Z
a) Using TTL
20383013
b) Using Relay Logic
Figure 13 Hardware Implementations of Boolean Functions
Foi the sake of compaiison we wiII impIement this
function thiee ways, iestiicting the softwaie to thiee
piopei subsets of the MCS-51 instiuction set. We wiII
aIso assume that U and V aie input pins fiom diffeient
input poits, W and X aie status bits foi two peiipheiaI
contioIIeis, and Y and Z aie softwaie fIags set up eaiIi-
ei in the piogiam. The end iesuIt must be wiitten
to an output pin on some thiid poit. The fiist two im-
pIementations foIIow the fIow-chait shown in Figuie
14. Piogiam fIow wouId embaik on a ioute down a
test-and-bianch tiee and Ieaves eithei the Tiue oi
Not Tiue exit ASAP-as soon as the piopei iesuIt
has been deteimined. These exits then iewiite the out-
put poit with the iesuIt bit iespectiveIy one oi zeio.
20
AP-70
20383014
Figure 14 Flow Chart for
Tree-Branching Algorithm
Othei digitaI computeis must soIve equations of this
type with standaid woid-wide IogicaI instiuctions and
conditionaI jumps. So foi the fiist impIementation, we
wont use any geneiaIized bit-addiessing instiuctions.
As we shaII soon see, being constiained to such an in-
stiuction subset pioduces somewhat sIoppy softwaie
soIutions. MCS-51 mnemonics aie used in FxampIe 2a:
othei machines might fuithei cIoud the situation by
iequiiing opeiation-specific mnemonics Iike INPUT,
OUTPUT, LOAD, STORF, etc., instead of the MOV
mnemonic used foi aII vaiiabIe tiansfeis in the 8051
instiuction set.
The code which iesuIts is cumbeisome and eiioi pione.
It wouId be difficuIt to piove whethei the softwaie
woiked foi aII input combinations in piogiams of this
soit. Fuitheimoie, execution time wiII vaiy wideIy with
input data.
Thanks to the diiect bit-test opeiations, a singIe in-
stiuction can iepIace each move mask conditionaI jump
sequence in FxampIe 2a, but the aIgoiithm wouId be
equaIIy convoIuted (see FxampIe 2b). To Iessen the
confusion a bit each input vaiiabIe is assigned a sym-
boIic name.
A moie eIegant and efficient impIementation (FxampIe
2c) stiings togethei the BooIean ANL and ORL func-
tions to geneiate the output function with stiaight-Iine
code. When finished, the caiiy fIag contains the iesuIt,
which is simpIy copied out to the destination pin. No
fIow chait is needed-code can be wiitten diiectIy fiom
the Iogic diagiams in Figuie 14. The iesuIt is simpIicity
itseIf: fast, fIexibIe, ieIiabIe, easy to design, and easy to
debug.
An 8051 piogiam can simuIate an N-input AND oi
OR gate with at most N
a
1 Iines of souice piogiam-
one foi each input and one Iine to stoie the iesuIts. To
simuIate NAND and NOR gates, compIement the cai-
iy aftei computing the function. When some inputs to
the gate have inveision bubbIes, peifoim the ANL oi
ORL opeiation on inveited opeiands. When the fiist
input is inveited, eithei Ioad the opeiand into the caiiy
and then compIement it, oi use DeMoigans Theoiem
to conveit the gate to a diffeient foim.
FxampIe 2. Softwaie SoIutions to Logic Function of
Figuie 13.
a.) Using onIy byte-wide IogicaI instiuctions
:BFUR0l $0L7E RARI0N L00l0
_ FUR01l0R 0F 8 7ARlABLE$
_ BY L0AIlR0 ARI NA$KlR0
_ 1HE AFFR0FRlA1E Bl1$ lR
_ 1HE A00UNULA10R. 1HER
_ EXE0U1lR0 00RIl1l0RAL
_ 5UNF$ BA$EI 0R 7ER0
_ 00RIl1l0R. |AFFR0A0H U$EI
_ BY BY1E-0RlER1EI
_ AR0Hl1E01URE$., BY1E ARI
_ NA$K 7ALUE$ 00RRE$F0RI 10
_ RE$FE01l7E BY1E AIIRE$$
_ ARI Bl1 F0$l1l0R$.
_
0U1BUF IA1A 22H
_0U1FU1 FlR $1A1E NAF
_
21
AP-70
1E$17: N07 A,F2
ARL A,00000l00B
5R7 1E$1U
N07 A,100R
ARL A,00l00000B
57 1E$1X
1E$1U: N07 A,Fl
ARL A,000000l0B
5R7 $E1@
1E$1X: N07 A,100R
ARL A,0000l000B
57 1E$17
N07 A,20H
ARL A,0000000lB
57 $E1@
1E$17: N07 A,2lH
ARL A,000000l0B
57 $E1@
0LR@: N07 A,0U1BUF
ARL A,llll0lllB
5NF 0U1@
$E1@: N07 A,0U1BUF
0RL A,0000l000B
0U1@: N07 0U1BUF,A
N07 F,A
b.) Using onIy bit-test instiuctions
:BFUR02 $0L7E A RARI0N L00l0
_ FUR01l0R 0F 8 7ARlABLE$
_ BY IlRE01LY F0LLlR0 EA0H
_ Bl1. |AFFR0A0H U$lR0
_ N0$-5l URl@UE Bl1-1E$1
_ lR$1RU01l0R 0AFABlLl1Y.,
_ $YNB0L$ U$EI lR L00l0
_ IlA0RAN A$$l0REI 10
_ 00RRE$F0RIlR0 3x5l Bl1
_ AIIRE$$E$.
_
U Bl1 Fl.l
7 Bl1 F2.2
Bl1 1F0
X Bl1 lEl
Y Bl1 20H.0
7 Bl1 2lH.l
@ Bl1 F.
_ ... ....
1E$1 7: 5B 7,1E$1 U
5RB ,1E$1 X
1E$1 U: 5B U,$E1 @
1E$1 X: 5RB X,1E$1 7
5RB Y,$E1 @
1E$1 7: 5RB 7,$E1 @
0LR @: 0LR @
5NF RX11$1
$E1 @: $E1B @
RX11$1:|00R1lRUA1l0R 0F
:FR00RAN,
c.) Using IogicaI opeiations on BooIean vaiiabIes
:FUR0 $0L7E A RARI0N L00l0
_ FUR01l0R 0F 8 7ARlABLE$
_ U$lR0 $1RAl0H1 LlRE
_ L00l0AL lR$1RU01l0R$ 0R
_ N0$-5l B00LEAR 7ARlABLE$.
_
N07 0,7
0RL 0, _0U1FU1 0F 0R 0A1E
ARL 0,U _0U1FU1 0F 10F ARI 0A1E
N07 F0,0 _$A7E lR1ERNEIlA1E $1A1E
N07 0,X
ARL 0,Y _0U1FU1 0F B0110N ARI 0A1E
0RL 0,F0 _lR0LUIE 7ALUE $A7EI AB07E
0RL 0,7 _lR0LUIE LA$1 lRFU1
_7ARlABLE
N07 @,0 _0U1FU1 00NFU1EI RE$UL1
22
AP-70
An uppei-Iimit can be pIaced on the compIexity of soft-
waie to simuIate a Iaige numbei of gates by summing
the totaI numbei of inputs and outputs. The actual totaI
shouId be somewhat shoitei, since caIcuIations can be
chained, as shown. The output of one gate is often
the fiist input to anothei, bypassing the inteimediate
vaiiabIe to eIiminate two Iines of souice.
Design Example 4Automotive
Dashboard Functions
Now Iets appIy these techniques to designing the soft-
waie foi a compIete contioIIei system. This appIication
is patteined aftei a famiIiai ieaI-woiId appIication
which isnt neaiIy as tiiviaI as it might fiist appeai:
automobiIe tuin signaIs.
Imagine the thiee position tuin Ievei on the steeiing
coIumn as a singIe-poIe, tiipIe-thiow toggIe switch. In
its centiaI position aII contacts aie open. In the up oi
down positions contacts cIose causing coiiesponding
Iights in the ieai of the cai to bIink. So fai veiy simpIe.
Two moie tuin signaIs bIink in the fiont of the cai, and
two otheis in the dashboaid. AII six buIbs fIash when
an emeigency switch is cIosed. A theimo-mechanicaI
ieIay (accessibIe undei the dashboaid in case it weais
out) causes the bIinking.
AppIying the biake pedaI tuins the taiI Iight fiIaments
on constantIy . . . unIess a tuin is in piogiess, in which
case the bIinking taiI Iight is not affected. (Of couise,
the fiont tuin signaIs and dashboaid indicatois aie not
affected by the biake pedaI.) TabIe 6 summaiizes these
opeiating modes.
Table 6 Truth Table for Turn-Signal Operation
Input Signals Output Signals
Brake Emerg
Left Right Left Right
Left Right
Switch Switch
Turn Turn Front Front
Rear Rear
Switch Switch Dash Dash
0 0 0 0 Off Off Off Off
0 0 0 1 Off Blink Off Blink
0 0 1 0 Blink Off Blink Off
0 1 0 0 Blink Blink Blink Blink
0 1 0 1 Blink Blink Blink Blink
0 1 1 0 Blink Blink Blink Blink
1 0 0 0 Off Off On On
1 0 0 1 Off Blink On Blink
1 0 1 0 Blink Off Blink On
1 1 0 0 Blink Blink On On
1 1 0 1 Blink Blink On Blink
1 1 1 0 Blink Blink Blink On
23
AP-70
But weie not done yet. Fach of the exteiioi tuin signaI
(but not the dashboaid) buIbs has a second, somewhat
dimmei fiIament foi the paiking Iights. Figuie 15
shows TTL ciicuitiy which couId contioI aII six buIbs.
The signaIs IabeIed High Fieq. and Low Fieq. iep-
iesent two squaie-wave inputs. BasicaIIy, when one of
the tuin switches is cIosed oi the emeigency switch is
activated the Iow fiequency signaI (about 1 Hz) is gated
thiough to the appiopiiate dashboaid indicatoi(s) and
tuin signaI(s). The ieai signaIs aie aIso activated when
the biake pedaI is depiessed piovided a tuin is not be-
ing made in the same diiection. When the paiking Iight
switch is cIosed the highei fiequency osciIIatoi is gated
to each fiont and ieai tuin signaI, sustaining a Iow-in-
tensity backgiound IeveI. (This is to eIiminate the need
foi additionaI paiking Iight fiIaments.)
In most cais, the switching Iogic to geneiate these func-
tions iequiies a numbei of muItipIe-thiow contacts. As
many as 18 conductois thiead the steeiing coIumn of
some automobiIes soIeIy foi tuin-signaI and emeigency
bIinkei functions. (The authoi discoveied this iecentIy
to his astonishment and dismay when iepIacing the
whoIe assembIy because of one buined contact.)
A muItipIe-conductoi wiiing hainess iuns to each coi-
nei of the cai, behind the dash, up the steeiing coIumn,
and down to the bIinkei ieIay beIow. Connectois at
each teimination foi each fiIament Iead to extia cost
and Iaboi duiing constiuction, Iowei ieIiabiIity and
safety, and moie costIy iepaiis. And consideiing the
systems piesent compIexity, incieasing its ieIiabiIity oi
detecting faiIuies wouId be quite difficuIt.
Theie aie two ieasons foi going into such painfuI detaiI
desciibing this exampIe. Fiist, to show that the messiest
pait of many system designs is deteimining what the
contioIIei shouId do. Wiiting the softwaie to soIve
these functions wiII be compaiativeIy easy. SecondIy, to
show the many potentiaI faiIuie points in the system.
Latei weII see how the peiipheiaI functions and inteIIi-
gence buiIt into a miciocomputei (with a IittIe cieativi-
ty) can gieatIy ieduce exteinaI inteiconnections and
mechanicaI pait count.
The Single-Chip Solution
The ciicuit shown in Figuie 16 indicates five input pins
to the five input vaiiabIes-Ieft-tuin seIect, iight-tuin
seIect, biake pedaI down, emeigency switch on, and
paiking Iights on. Six output pins tuin on the fiont,
ieai, and dashboaid indicatois foi each side. The mi-
ciocomputei impIements aII IogicaI functions thiough
softwaie, which peiiodicaIIy updates the output signaIs
as time eIapses and input conditions change.
20383015
Figure 15 TTL Logic Implementation of Automotive Turn Signals
24
AP-70
20383016
Figure 16 Microcomputer Turn-Signal Connections
Design FxampIe 3 demonstiated that symboIic ad-
diessing with usei-defined bit names makes code and
documentation easiei to wiite and maintain. Accoid-
ingIy, weII assign these I/O pins names foi use
thioughout the piogiam. (The foimat of this exampIe
wiII diffei somewhat fiom the otheis. Segments of the
oveiaII piogiam wiII be piesented in sequence as each is
desciibed.)
_
_ lRFU1 FlR IE0LARA1l0R$:
_|ALL lRFU1$ ARE F0$l1l7E-1RUE L00l0,
_
BRAKE Bl1 Fl.0 _BRAKE FEIAL
_IEFRE$$EI
ENER0 Bl1 Fl.l _ENER0ER0Y BLlRKER
_A01l7A1EI
FARK Bl1 Fl.2 _FARKlR0 Ll0H1$ 0R
l 1URR Bl1 Fl. _1URR LE7ER I0R
R 1URR Bl1 Fl.4 _1URR LE7ER UF
_
_ 0U1FU1 FlR IE0LARA1l0R$:
_
l FRR1 Bl1 Fl.5 _FR0R1 LEF1-1URR
_lRIl0A10R
R FRR1 Bl1 Fl.8 _FR0R1 Rl0H1-1URR
_lRIl0A10R
l IA$H Bl1 Fl.7 _IA$HB0ARI LEF1-1URR
_lRIl0A10R
R IA$H Bl1 F2.0 _IA$HB0ARI Rl0H1-
_1URR lRIl0A10R
l REAR Bl1 F2.l _REAR LEF1-1URR
_lRIl0A10R
R REAR Bl1 F2.2 _REAR Rl0H1-1URR
_lRIl0A10R
_
Anothei key advantage of symboIic addiessing wiII ap-
peai fuithei on in the design cycIe. The Iocations of
cabIe connectois, signaI conditioning ciicuitiy, voItage
ieguIatois, heat sinks, and the Iike aII affect P.C. boaid
Iayout. Its quite IikeIy that the somewhat aibitiaiy pin
assignment defined eaiIy in the softwaie design cycIe
wiII piove to be Iess than optimum, ieaiianging the I/O
pin assignment couId weII aIIow a moie compact mod-
uIe, oi eIiminate costIy jumpeis on a singIe-sided boaid.
(These consideiations appIy especiaIIy to automotive
and othei cost-sensitive appIications needing singIe-
chip contioIIeis.) Since othei aichitectuies mask bytes
oi use cIevei aIgoiithms to isoIate bits by iotating
them into the caiiy, ie-iouting an input signaI (fiom bit
1 of poit 1, foi exampIe, to bit 4 of poit 3) couId iequiie
extensive modifications thioughout the softwaie.
The BooIean Piocessois diiect bit addiessing makes
such changes absoIuteIy tiiviaI. The numbei of the poit
containing the pin is iiieIevent, and masks and compIex
25
AP-70
piogiam stiuctuies aie not needed. OnIy the initiaI
BooIean vaiiabIe decIaiations need to be changed,
ASM51 automaticaIIy adjusts aII addiesses and symboI-
ic iefeiences to the ieassigned vaiiabIes. The usei is
assuied that no additionaI debugging oi softwaie veiifi-
cation wiII be iequiied.
_ ... .....
_lR1ERRUF1 RA1E $UBIl7lIER
$UB Il7 IA1A 20H
_Hl0H-FRE@UER0Y 0$0lLLA10R Bl1
Hl FRE@ Bl1 $UB Il7,0
_L0-FRE@UER0Y 0$0lLLA10R Bl1
L0 FRE@ Bl1 $UB Il7,7
_ ...
0R0 0000H
5NF lRl1
_ ... .....
0R0 l00H
_FU1 1lNER 0 lR N0IE l
lRl1_ N07 1N0I,0000000lB
_lRl1lALl7E 1lNER RE0l$1ER$
N07 1L0,0
N07 1H0,1l8
_$UBIl7lIE lR1ERRUF1 RA1E BY 244
N07 $UB Il7,244
_ERABLE 1lNER lR1ERRUF1$
$E1B E10
_0L0BALLY ERABLE ALL lR1ERRUF1$
$E1B EA
_$1AR1 1lNER
$E1B 1R0
_
_|00R1lRUE l1H BA0K0R0URI FR00RAN,
_
_FU1 1lNER 0 lR N0IE l
_lRl1lALl7E 1lNER RE0l$1ER$
_$UBIl7lIE lR1ERRUF1 RA1E BY 244
_ERABLE 1lNER lR1ERRUF1$
_0L0BALLY ERABLE ALL lR1ERRUF1$
_$1AR1 1lNER
Timei 0 (one of the two on-chip timei counteis) ie-
pIaces the theimo-mechanicaI bIinkei ieIay in the dash-
boaid contioIIei. Duiing system initiaIization it is con-
figuied as a timei in mode 1 by setting the Ieast signifi-
cant bit of the timei mode iegistei (TMOD). In this
configuiation the Iow-oidei byte (TL0) is inciemented
eveiy machine cycIe, oveifIowing and inciementing the
high-oidei byte (TH0) eveiy 256 ms. Timei inteiiupt 0
is enabIed so that a haidwaie inteiiupt wiII occui each
time TH0 oveifIows.
An eight-bit vaiiabIe in the bit-addiessabIe RAM aiiay
wiII be needed to fuithei subdivide the inteiiupts via
softwaie. The Iowest-oidei bit of this countei toggIes
veiy fast to moduIate the paiking Iights: bit 7 wiII be
tuned to appioximateIy 1 Hz foi the tuin- and emei-
gency-indicatoi bIinking iate.
Loading TH0 with -16 wiII cause an inteiiupt aftei
4.096 ms. The inteiiupt seivice ioutine ieIoads the
high-oidei byte of timei 0 foi the next inteivaI, saves
the CPU iegisteis IikeIy to be affected on the stack, and
then deciements SUB
-
DIV. Loading SUB
-
DIV.
with 244 initiaIIy and each time it deciements to zeio
wiII pioduce a 0.999 second peiiod foi the highest-oi-
dei bit.
0R0 000BH _1lNER 0 $ER7l0E 7E010R
N07 1H0,-l8
FU$H F$
FU$H A00
FU$H B
I5R7 $UB Il7,10$ER7
N07 $UB Il7,244
The code to sampIe inputs, peifoim caIcuIations, and
update outputs-the ieaI meat of the signaI contioI-
Iei aIgoiithm-may be peifoimed eithei as pait of the
inteiiupt seivice ioutine oi as pait of a backgiound
piogiam Ioop. The onIy concein is that it must be exe-
cuted at Ieast seiveiaI dozen times pei second to pie-
vent paiking Iight fIickeiing. We wiII assume the foi-
mei case, and inseit the code into the timei 0 seivice
ioutine.
Fiist, notice fiom the Iogic diagiam (Figuie 15) that
the subteim (PARK H
-
FRFQ), asseited when the
paiking Iights aie to be on dimIy, figuies into foui of
the six output functions. AccoidingIy, we wiII fiist
compute that teim and save it in a tempoiaiy Iocation
named DIM. The PSW contains two geneiaI puipose
fIags: F0, which coiiesponds to the 8048 fIag of the
same name, and PSW.1. Since the PSW has been saved
and wiII be iestoied to its pievious state aftei seivicing
the inteiiupt, we can use eithei bit foi tempoiaiy stoi-
age.
IlN Bl1 F$.l _IE0LARE 1ENF
_$10RA0E FLA0
_ ... .....
N07 0,FARK _0A1E FARKlR0
_Ll0H1 $l10H
ARL Hl FRE@ _l1H Hl0H
_FRE@UER0Y
_$l0RAL
N07 IlN,0 _ARI $A7E lR
_1ENF. 7ARlABLE
This simpIe thiee-Iine section of code iIIustiates a ie-
maikabIe point. The softwaie indicates in veiy abstiact
teims exactIy what function is being peifoimed, inde-
26
AP-70
pendent of the haidwaie configuiation. The fact that
these thiee bits incIude an input pin, a bit within a
piogiam vaiiabIe, and a softwaie fIag in the PSW is
totaIIy invisibIe to the piogiammei.
Now geneiate and output the dashboaid Ieft tuin sig-
naI.
_
N07 0,L 1URR _$E1 0ARRY lF
_1URR
0RL 0,ENER0 _0R ENER0ER0Y
_$ELE01EI
ARL 0,L0 FRE@ _0A1E lR l H7
_$l0RAL
N07 l IA$H,0 _ARI 0U1FU1 10
_IA$HB0ARI
To geneiate the Ieft fiont tuin signaI we onIy need to
add the paiking Iight function in F0. But notice that the
function in the caiiy wiII aIso be needed foi the ieai
signaI. We can save effoit Iatei by saving its cuiient
state in F0.
_
N07 F0,0 _$A7E FUR01l0R
_$0 FAR
0RL 0,IlN _AII lR FARKlR0
_Ll0H1 FUR01l0R
N07 L FRR1,0 _ARI 0U1FU1 10
_1URR $l0RAL
FinaIIy, the ieai Ieft tuin signaI shouId aIso be on when
the biake pedaI is depiessed, piovided a Ieft tuin is not
in piogiess.
N07 0,BRAKE _0A1E BRAKE
_FEIAL $l10H
ARL 0,L 1URR _l1H 1URR
_LE7ER
0RL 0,F0 _lR0LUIE 1ENF.
_7ARlABLE FR0N IA$H
0RL 0,IlN _ARI FARKlR0
_Ll0H1 FUR01l0R
N07 L REAR,0 _ARI 0U1FU1 10
_1URR $l0RAL
Now we have to go thiough a simiIai sequence foi the
iight-hand equivaIents to aII the Ieft-tuin Iights. This
aIso gives us a chance to see how the code segments
above Iook when combined.
N07 0.R 1URR _$E1 0ARRY H-
_1URR
0RL 0.ENER0 _0R ENER0ER0Y
_$ELE01EI
ARL 0,L0 FRE@ _lF $0. 0A1E lR l
_H7 $l0RAL
N07 R IA$H.0 _ARI 0U1FU1 10
_IA$HB0ARI
N07 F0.0 _$A7E FUR01l0R
_$0 FAR
0RL 0.IlN _AII lR FARKlR0
_Ll0H1 FUR01l0R
N07 R FRR1.0 _ARI 0U1FU1 10
_1URR $l0RAL
N07 0.BRAKE _0A1E BRAKE
_FEIAL $l10H
ARL 0. R 1URR _l1H 1URR
_LE7ER
0RL 0.F0 _lR0LUIE 1ENF.
_7ARlABLE FR0N
_IA$H
0RL 0.IlN _ARI FARKlR0
_Ll0H1 FUR01l0R
N07 R REAR.0 _ARI 0U1FU1 10
_1URR $l0RAL
(The peiceptive ieadei may notice that simpIy ieai-
ianging the steps couId eIiminate one instiuction fiom
each sequence.)
Now that aII six buIbs aie in the piopei states, we can
ietuin fiom the inteiiupt ioutine, and the piogiam is
finished. This code essentiaIIy needs to ieveise the
status saving steps at the beginning of the inteiiupt.
Table 7 Non-Trivial Duty Cycles
Sub
2
6
A-1
AP-70
2
0
3
8
3
0
2
7
A-2
AP-70
2
0
3
8
3
0
2
8
A-3
AP-70
2
0
3
8
3
0
2
9
INTEL CORPORATION 2200 Mission College Blvd Santa Clara CA 95052 Tel (408) 765-8080
INTEL CORPORATION (UK) Ltd Swindon United Kingdom Tel (0793) 696 000
INTEL JAPAN kk Ibaraki-ken Tel 029747-8511
Printed in USAxxxx0296B10Mxx xx