Tải bản đầy đủ (.pdf) (513 trang)

lipovski, g. j. (1999). single- and multi-chip microcontroller interfacing

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (40.02 MB, 513 trang )

Single-
and
Multi-Chip
Microcontroller Interfacing
For the
Motorola 68HC12
Academic
Press Series
in
Engineering
Series Editor
J.
David Irwin
Auburn
University
Designed
to
bring together interdependent topics
in
electrical engineering, mechanical
engineering, computer engineering,
and
manufacturing,
the
Academic Press
Series
in
Engineering provides state-of-the-art handbooks, textbooks,
and
professional reference


books
for
researchers, students,
and
engineers. This series provides readers with
a
com-
prehensive group
of
books essential
for
success
in
modern industry.
A
particular emphasis
is
given
to the
applications
of
cutting-edge research. Engineers, researchers,
and
students
alike
will
find
the
Academic Press Series
in

Engineering
to be an
indispensable part
of
their
design
toolkit.
Published books
in the
series:
Industrial
Controls
and
Manufacturing,
1999,
E.
Kamen
DSP
Integrated Circuits,
1999,
L.
Wanhammar
Time
Domain Electromagnetics,
1999,
S. M. Rao
Single-
and
Multi-Chip Microcontroller
Interfacing

for the
Motorola 68HC12,
1999,
G. J.
Lipovski
Control
in
Robotics
and
Automation,
1999,
B. K.
Ghosh,
N. Xi, T. J.
Tarn
Single-
and
Multi-Chip
Microcontroller Interfacing
For the
Motorola 68HC12
G.
Jack Lipovski
Department
of
Electrical
and
Computer Engineering
University
of

Texas
Austin,
Texas
ACADEMIC
PRESS
San
Diego London Boston
New
York Sydney Tokyo Toronto
This
book
is
printed
on
acid-free paper.
©
Copyright
©
1999
by
Academic Press
All
rights reserved.
No
part
of
this publication
may be
reproduced
or

transmitted
in any
form
or by any
means, electronic
or
mechanical, including photocopy, recording,
or
any
information storage
and
retrieval system, without
permission
in
writing
from
the
publisher.
Academic Press
525 B.
St., Suite 1900,
San
Diego, California 92101-4495,
USA

Academic Press
24–28
Oval Road, London
NW1
7DX,

UK
/>Library
of
Congress
Catalog
Card Number: 98-89451
ISBN:
0-12-451830-3
Printed
in the
United
States
of
America
99
00 01 02 03 MV 9 8 7 6 5 4 3 2 1
Disclaimer:
This eBook does not include the ancillary media that was
packaged with the original printed version of the book.
Dedicated
to my
mother,
Mary
Lipovski
G.
Jack Lipovski
has
taught
electrical
engineering

and
computer science
at the
University
of
Texas since 1976.
He is a
computer architect internationally recognized
for his
design
of the
pioneering database computer, CASSM,
and the
parallel computer, TRAC.
His
expertise
in
microcomputers
is
also internationally
recognized
by his
being
a
past director
of
Euromicro
and an
editor
of

IEEE
Micro.
Dr.
Lipovski
has
published more
than
70
papers,
largely
in the
proceedings
of the
annual symposium
on
computer architecture,
the
IEEE
transactions
on
computers
and the
national computer conference.
He
holds eight patents,
generally
in the
design
of
logic-in-memory integrated circuits

for
database
and
graphics
geometry processing.
He has
authored seven books
and
edited three.
He has
served
as
chair-
man
of the
IEEE Computer Society Technical Committee
on
Computer Architecture, mem-
ber of the
Computer Society Governing Board,
and
chairman
of the
Special
Interest Group
on
Computer Architecture
of the
Association
for

Computer Machinery.
He has
been
elected
Fellow
of the
IEEE
and a
Golden Core Member
of the
IEEE Computer Society.
He
received
his
Ph.D. degree
from
the
University
of
Illinois, 1969,
and has
taught
at the
University
of
Florida,
and at the
Naval
Postgraduate
School,

where
he
held
the
Grace Hopper chair
in
Computer
Science.
He has
consulted
for
Harris Semiconductor, designing
a
microcomputer,
and
for the
Microelectronics
and
Computer Corporation, studying parallel computers.
He
founded
the
company Linden Technology Ltd.,
and is the
chairman
of its
board.
His
current
interests include parallel computing, database computer architectures, artificial intelligence

computer
architectures,
and
microcomputers.
About the Author
Contents
Preface
x i i i
List
of
Figures xvii
List
of
Tables
xxi
Acknowledgments
xxi
About
the
Author
xxiii
I
Microcomputer
Architecture
I
1.1 An
Introduction
to the
Microcomputer
I

1.1.1 Computer Architecture
2
1.1.2
The
Instruction
6
1.1.3 Microcomputers
9
1.2 The
6812 Instruction
Set 11
1.2.1 6812 Addressing Modes
11
1.2.2
6812 Data Operator Instructions
16
1.2.3 6812 Control Instructions
23
1.3
Assembly-Language Directives
28
1.4
Organization
of
6812 Microcontrollers
31
1.4.1 Notation
for
Block Diagrams
31

.,4.2 6812 Microcontroller
I/O and
Memory Organization
31
1.4.3
The
MC68HC812A4
and
MC68HC912B32 Memory Maps
35
1.5
Conclusions
36
Problems
38
2
Programming
Microcomputers
45
2.1
Introduction
to C 46
2.2
Data Structures
55
2.2.1
Indexable Data Structures
57
2.2.2 Sequential Data Structures
59

2.3
Writing Clear
C
Programs
66
2.3.1
C
Procedures
and
Their Arguments
66
2.3.2 Programming Style
72
2.3.3 Object-Oriented Programming
73
2.3.4 Optimizing
C
Programs Using Declarations
83
2.3.5 Optimizing
C
Programs with Assembly Language
83
2.4
Conclusions
84
Problems
86
vu
Contents

3 Bus
Hardware
and
Signals
93
3.1
Digital Hardware
94
3.1.1 Modules
and
Signals
94
3.1.2
Drivers, Registers,
and
Memories
97
3.2
Address
and
Control Signals
in
6812 Microcontrollers
104
3.2.1 Address
and
Control Timing
104
3.2.2
Address

and
Control Signal Decoding
108
3.3
Conclusions
113
Problems
115
4.
Parallel
and
Serial
Input/Output
4.1 I/O
Devices
and
Ports
4.1.1
Generic Port Architecture
4.1.2 Generic Port Classes
4.1.3 Debugging Tools
4.2
6812 Parallel Ports
4.2.1 MC68HC812A4
Port
Architecture
4.2.2 MC68HC912B32 Port Architecture
4.2.3 Programming
of
PORTA

4.2.4
A
Class
for
Ports with Direction Control
4.3
Input/Output
Software
4.3.1
A
Wire
4.3.2
A
Movie
4.3.3
A
Traffic
Light Controller
4.3.4
A
Sequential Machine
4.3.5
An IC
Tester
4.3.6 Object-oriented Vector Functions
and
Interpreters
4.4
Input/Output Indirection
4.4.1

Indirect
Input/Output
4.4.2
LCD
Interfacing
4.4.3 Synchronous Serial Input/Output
4.4.4
The
6812
SPI
Module
4.4.5 Accessing Devices Using Vectors
and
structs
4.4.5.1 Vector Access
to
Ports
4.4.5.2
Vector Pointer Access
to
Ports
4.4.5.3
Using #defines
to
Name Ports
4.4.5.4
Struct Pointer Access
to
Ports
4.4.5.5 Struct Access

to
Ports
4.4.6
Indirect
and
Serial
I/O
Objects
4.5 A
Designer's Selection
of I/O
Ports
and
Software
4.6
Conclusions
Problems
Contents
5
Interrupts
and
Alternatives
193
5.1
Programmed Synchronization
196
5.1.1
Real-time Synchronization
196
5.1.2 Gadfly Synchronization

198
5.1.2.1 MC68HC812A4
Gadfly
Synchronization
199
5.1.2.2 MC68HC912B32
Gadfly
Synchronization
200
5.1.2.3
Gadfly Synchronization Characteristics
200
5.1.3
Handshaking
200
5.1.4 Some Examples
of
Programmed
I/O 201
5.1.5 Object-oriented
Classes
for
Programmed
I/O 204
5.2
Interrupt Synchronization
210
5.2.1 Steps
in an
Interrupt

210
5.2.1.1
Steps
in an
Interrupt
in the
MC68HC812A4
211
5.2.1.2 Steps
in an
Interrupt
in the
MC68HC912B32
212
5.2.1.3 Properties
of
Interrupt Synchronization
213
5.2.2 Interrupt
Handlers
and
Critical
Sections
214
5.2.2.1
A
Handler That Changes
a
Global Variable
214

5.2.2.2
A
Handler That Fills
or
Empties
a
Buffer
216
5.2.2.3
A
Handler That Uses
a
Queue
for
Input
218
5.2.2.4
A
Handler That Uses
a
Queue
for
Output
220
5.2.2.5 Critical Sections
221
5.2.3
Polled
Interrupts
222

5.2.3.1 Polled Interrupts
in the
MC68HC812A4
222
5.2.3.2 Polled Interrupts
in the
MC68HC912B32
224
5.2.3.3
Service Routines
225
5.2.3.4 Round-robin Handlers
226
5.2.4 Vectored Interrupts
228
5.2.4.1
Vectored Interrupts
in the
MC68HC812A4
228
5.2.4.2 Vectored Interrupts
in the
MC68HC912B32
230
5.2.4.3
Vectored Interrupts
for
Other Devices
231
5.2.5 Examples

of
Interrupt Synchronization
231
5.2.5.1 Keyboard Handling
23 i
5.2.5.2
Interrupts
for SPI
Systems
235
5.2.5.3
Histograms
and
Histories
for the
MC68HC812A4
236
5.2.6 Object-oriented Classes
for
Interrupts
237
5.2.6.1
An
IQFPort Class
237
5.2.6.2
An
OQFPort
Class
239

5.2.6.3
Polling IQFPort
and
OQFPort Classes
240
5.2.6.4 Bar-code Class
240
5.2.6.5
An
X-10 Class
241
5.3
Time-Sharing
244
5.3.1 Real-time Interrupts
244
5.3.2 Multithread Scheduling
245
5.3.3 Threads
for
Time-sharing
248
5.3.4
An
Efficient
Time Schedular
252
5.3.5 Special Instructions
for
Time-sharing

256
Contents
5.3.6 Object-oriented Classes
for
Time-sharing
5.4
Fast Synchronization Mechanisms
5.4.1 Direct Memory Access
5.4.2 Context Switching
5.4.3 Memory
Buffer
Synchronization
5.5
Conclusions
Problems
6
System
Control
277
6.1
6812 Chip Modes
277
6.1.1 MODE Control Port
277
6.1.2 Port
E
Assignment
279
6.2
6812 Memory

Map
Control
280
6.2.1 Internal Memory
Map
Control
280
6.2.2 MC68HC812A4 Chip
Selects
281
6.2.3 MC68HC812A4 Memory Expansion
284
6.2.4 Object-oriented Programming
of
Memory Expansion
285
6.3
EEPROM
and
Flash Memory Programming
286
6.4
MC68HC812A4 Timing Control
289
6.5 An
External Memory
for the
MC68HC812A4
290
6.6 The

6812 Background Debug Module
295
6.7
6812 Reset Handler
299
6.8
Conclusions
301
Problems
302
7
Analog Interfacing
307
7.1
Input
and
Output Transducers
309
7.1.1 Positional Transducers
309
7.1.2
Radiant Energy Transducers
311
7.1.3 Other Transducers
313
7.2
Basic Analog Processing Components
314
7.2.1
Transistors

and
Silicon Controlled Rectifiers
314
7.2.2
Basic
Linear Integrated Circuits
317
7.2.3
Practical Linear Integrated Circuits
321
7.3 OP AMP and
Analog Switch Signal Conditioning
326
7.3.1
Filters
327
7.3.2
Selection
of
Inputs
and
Control
of
Gain
329
7.3.3 Nonlinear
Amplification
332
7.4
Converters

334
7.4.1
Digital-to-Analog Converters
334
7.4.2 Analog-to-Digital Converters
338
7.4.3 Voltage Conversion
to or from
Frequency
340
7.5
Data Acquisition Systems
344
7.5.1
Basic Operation
of a
Data Acquisition System
345
7.5.2
The
MC145040 A-to-D Converter
346
7.5.3
The
MC68HC812A4 A-to-D Converter
347
7.5.4 Object-oriented Programming
of
Converters
349

7.5.5 Applications
in
Control Systems
350
7.6
Conclusions
352
Problems
354
8
Counters
and
Timers
361
8.1 The
MC68HC812A4 Counter/Timer Subsystem
362
8.2
Signal Generation
363
8.2.1 Output Compare
Logic
364
8.2.2
The
Counter/Timer Square-Wave Generator
365
8.2.3
The
MC68HC912B32

PWM
Signal Generator
366
8.2.4
A
Touch-tone Signal Generator
369
8.2.5
The
Pulse Generator
373
8.2.6
A
Rotary Dialer
374
8.2.7
Real-Time Clock
and
Trace Mechanism
375
8.2.8 Output Compare
7 376
8.2.9 Object-oriented Square-wave
and
Pulse Generation
378
8.3
Frequency
and
Period Measurement

381
8.3.1
The
Input Capture Mechanism
and
Period Measurement
381
8.3.2
Pulse-width Measurement
383
8.3.3 Triac Control
385
8.3.4 Pulse Accumulation
and
Frequency Measurement
385
8.3.5 Object-oriented
Period,
Pulse-width,
and
Frequency Measurements
387
8.4
Conclusions
388
Problems
390
9.
Communications
Systems

397
9.1
Communications Principles
398
9.2
Signal Transmission
401
9.2.1 Voltage
and
Current Linkages
401
9.2.2
Frequency
Shift-Keyed
Links Using Modems
405
9.2.3
Infrared
Links
407
9.3
UART Link Protocol
408
9.3.1 UART Transmission
and
Reception
by
Software
408
9.3.2

The
UART
411
9.3.3
The
ACIA
414
9.3.4
The
M14469
417
9.3.5
The
Serial Communication Interface System
in the
6812
420
9.3.6 Object-oriented Interfacing
to the SCI 423
9.4
Other Protocols
427
9.4.1 Synchronous Bit-oriented Protocols
427
9.4.2
MC68HC912B32
BDLC Device
430
9.4.3 IEEE-488
Bus

Standard
432
9.4.4
The
Small Computer System Interface (SCSI)
435
9.5
Conclusions
436
Problems
438
xii
Contents
10.
Display
and
Storage
Systems
445
10.1 Display Systems
446
10.1.1
NTSC Television Signals
446
10.1.2
A
6812
SPI
Display
448

10.1.3
A
Bitmapped Display
450
10.1.4
An
Object-oriented Display
453
10.2 Storage Systems
455
10.2.1 Floppy Disk Format
455
10.2.2.
The
Western Digital 37C65C Floppy Disk
Controller
459
10.2.3. Floppy Disk Interfacing Procedures
462
10.2.4
Personal
Computer Disk Data Organization
466
10.2.5 Object-oriented Disk
I/O 470
10.3 Conclusions
473
Problems
475
Appendix. Using

the
HIWAVE CD-ROM
481
Index
483
Preface
By
1980,
the
microcomputer
had
changed
so
many aspects
of
engineering that
the
cliche "microcomputer revolution" echoed
from
almost every other magazine article
and
learned
paper
in the field. It is a
great tool. This
book's
ancestor,
Microcomputer
Interfacing:
Principles

and
Practices,
was
written
at
that time
to
establish some design
theory
for
this dynamic
field.
A
successor book, which
is
this
book's
predecessor,
Single-
and
Multiple-Chip Microcomputer Interfacing,
was
motivated
by two
significant
changes:
the
evolution
of
powerful

single-chip microcomputers
and the
IEEE
Computer
Society Curriculum Committee recommendation
for a
course
on
microcomputer
interfacing
and
communication.
The
development
of
powerful
single-chip
microcomputers introduces
a new
design choice:
to use
either
a
microprocessor
in a
personal computer
with
some
16M
bytes

of
memory
and an
operating system
or a
less
costly single-chip microcomputer with much less overhead. This decision
is
largely
based
on the
designer's understanding
of the
capabilities
and
limitations
of the
single-
chip
microcomputer.
The
development
of a
standard curriculum
for a
course lent stability
to
this field.
The
book aimed

to
teach
the
principles
and
practices
of
microcomputer
systems
design
in
general,
and
interfacing
in
particular,
and to
foster
an
understanding
of
single-chip microcomputers within
the
guidelines
of the
IEEE Computer Society
Curriculum
Committee's recommendations. This book
was
motivated

by the
development
of the
Motorola 6812,
and its
need
for
more sophisticated
software.
Since
the
6812 featured
so
many
on-chip
I/O
devices, which were already connected
to the
6812's
address
and
data buses,
but
which each
had to be
programmed,
and
managing
a
number

of I/O
devices
often
necessitated
the use of
time sharing, this book features
programming
in C and
C++. However,
the
designer must
be
wary
of
high-level language
statements
that
do not do
what
he or she
intended. High-level languages
are
designed
for
algorithms,
not for I/O
interfacing,
and
optimizing high-level language compilers
can

"optimize
you
right
out of
business."
The
designer
is
shown
how
each
high-level
language statement
is
implemented
in
assembler language.
This book's predecessor evolved
from
a set of
notes
for a
senior level course
in
microcomputer
design.
The
course
-
which

is
still taught
-
focuses
on the
combined
hardware/software design
of
microcomputer systems.
It
emphasizes
principles
of
design
because theory
is as
necessary
for a
solid foundation
in
design
as
theory
is in any
engineering discipline. However,
it
also emphasizes
the
practices
- the

details
of
how
to
get
a
system
to
work
-
because microcomputer system
design
requires
hands-on
experience. There
is a
remarkable
difference
between
a
student
who
merely reads about
microcomputers
and a
student
who has
worked
with
one -

clear evidence that theory
has
to be
taught with
practice.
Practical
experience
is
desirable
in
almost
any
engineering
course. This
is not
always possible.
But
microcomputer systems
are
inexpensive enough
that
the
school
or the
student
can
afford
this hands-on opportunity;
and the joy of
seeing

the
principles work
is so
attractive that
the
student
often
can't
get
enough
of the
material
to be
satiated.
The
development
of
very
powerful,
inexpensive single-chip
microcomputers
furthers
this opportunity.
So the
course, this book's predecessor,
and
this book,
all
emphasize both
the

principles
and
practices
of
microcomputer design.
xiv
Preface
The
principles
and
practices
of
microcomputer design have
to
cover both hardware
and
software.
A
purely hardware approach might appeal
to a
seasoned digital system
designer
or an
electrical engineering student,
but it
leads
to
poor choices that either
do
not

take
full
advantage
of
software's tremendous power
or
force
unnecessary constraints
and
therefore higher costs
on its
development. However,
a
purely software approach
misses
the
opportunity
to
understand
how and why
things happen,
and how to
take
advantage
of the
hardware.
The
IEEE Computer Society Curriculum Committee
recommends
a

combined
hardware/software
approach.
A
combined hardware/software approach does require more background.
The
course
this book
is
based
on is the
second
of a
two-course sequence.
The first
course teaches
how
C
statements
are
implemented
in
assembler-language programming.
The
second
course builds
on
that background
and
also presumes some background

in
logic design,
as
would
be
obtained
in a
standard introductory course
on
that topic. This book, however,
has
three chapters that survey
these
topics. These chapters
can be
skimmed
as a
review
of
the
required background material
or
carefully
studied
as a
condensed tutorial
if the
reader
has
not had the

earlier courses
or
their equivalents. Because they
are
intended
as
review
or
intensive
tutorial material,
to
prepare
the
readers
for the
book's
main subject, these three
chapters
are
comparatively compressed
and
terse.
We
make
the
practices discussed
in
this book concrete through detailed discussion
of
the

6812 microcontroller. However, these products
are
used
as a
means
to the end of
teaching
principles
and
practices
in
general, rather than
to
promote
the use of
specific
Motorola products. Applications notes
and
catalogues
are
available
from
Motorola
to
that
end.
Specific,
detailed discussion encourages
and
aids

the
reader
in
learning through
hands-on
experience
and
vitally contributes
to his or her
enthusiasm
for and
understanding
of the
principles.
The
6812
is
used primarily because
the
MC68HC812A4
(abbreviated 'A4)
and the
MC68HC912B32 (abbreviated 'B32)
are
believed
to be the
most
easily taught single-chip microcomputer.
The
6812 instruction

set is as
complete
as
that
of any
other comparable machine, supporting
C and C++
well enough
to
teach
the
intelligent
use of
data structures,
and it is
symmetrical
and
comparatively
free
of
quirks
and
warts that
detract
from
the
subject under discussion. Moreover,
the
'A4's
4 K

bytes
of
EEPROM, which
can be
written
10,000
times,
is
well suited
to
university-level
lab
and
project use.
The
less costly 'B32,
with
its 1 K
bytes
of
SRAM,
is
also
suited
to
simple
lab
experiments. Nevertheless,
we
stress that detailed comparisons between

the
'
A4
and
other well-designed microcomputers clearly show that others
may be
better than
the
'A4 or
'B32
for
different applications. However,
a
comparative study
of
different
microcomputers
and
applications
is
beyond
the
scope
of
this book.
As
mentioned,
the first
three chapters quickly survey
the

background needed
for the
remainder
of the
book. Chapter
1 is a
survey that covers computer architecture
and the
instruction
set of the
6812. Chapter
2
covers some software techniques, including
subroutine
parameter
and
local variable conventions, that
are
very
useful
in
microcomputers. Object-oriented programming
is
introduced
for
readers with advanced
programming
skills.
Chapter
3

covers basic computer
organization,
but
confines itself
to
those
aspects
of the
topic that
are
particularly germane
to
microcomputers.
For
example,
basic
computer organization traditionally covers floating point arithmetic,
but
this
chapter
doesn't;
and
this chapter dwells
on
address decoders,
a
topic
not
often
covered

in
computer
organization.
Preface
The
rest
of the
book covers three basic themes: input-output (I/O) hardware
and
software,
analog signals,
and
communications. Parallel
I/O
ports
are
covered
in
Chapter
4,
interrupts
and
alternatives
in
Chapter
5,
analog components
in
Chapters
7 and 8,

communications devices
in
Chapter
9, and
disk storage
and CRT
display devices
in
Chapter
10. The
simple parallel
I/O
port
and the
synchronous serial
I/O
port
-
especially
attractive
in
interfacing slow
devices
because
it
economizes
on
pins
- are
displayed

in
Chapter
4.
Hardware
and
software
aspects
are
studied
side
by
side.
The
reader
need
no
longer
be
intimidated
by the
basic interface. Chapter
5
discusses interrupts
and
their
alternatives. Hardware/software tradeoffs
are
analyzed,
and
different

techniques
are
exemplified.
Chapter
6
describes system configuration ports
for
control over memory
and
time. Chapter
7
surveys
the
traditional (voltage) analog components that
are
commonly
used
in
microcomputer
I/O
systems.
Sufficient
information
is
provided that
the
reader
can
understand
the

uses, limitations,
and
advantages
of
analog components,
and
can
springboard
from
this chapter
to
other texts, magazine articles,
or
discussions
with
colleagues
to a
fuller
understanding
of
analog design. Chapter
8
introduces
the
counter/timer
as an
interface
to
frequency-analog signals. Techniques
to

generate signals
with
a
frequency
or to
measure
a
signal with
a
frequency
that
is
analog
to
some
quantity
are
discussed. Moreover,
the
hardware/software alternatives
to
using this most interesting
integrated circuit
are
discussed
and
evaluated. Chapter
9
describes communications
devices.

The
universal asynchronous receiver transmitter
and its
cousins
are
thoroughly
studied,
and
other communications techniques
are
described.
Finally, Chapter
10
introduces
the
magnetic storage device
and the CRT
display device.
This book emphasizes
the
software
aspect
of
interfacing because software design
costs dominate
the
cost
of
designing microcontroller systems. Software design cost
is

reduced
by
using abstraction, which
is
facilitated
by
using high-level languages.
Programming
is in C and
C++,
and the
latter utilizes object-oriented techniques
for I/O
software.
Generally, techniques
are
first
introduced
in C,
with explanations
of
what
is
done, using assembler language. Then,
in
separately designated
and
optional
sections,
the

techniques
are
extended using C++.
The
optional sections
are
clearly designated
by the
use
of
"object-oriented"
in
their title.
These
optional
sections
can be
skipped without
loosing
any
necessary background
in
later non-optional sections
and
chapters. However,
if
a
given
optional section
is to be

understood, previous optional sections need
to be
read
for
the
necessary background
for it. We
hope that this organization
will
permit
a
reader
who
does
not
have extensive skills
in
programming
to
understand
the
use,
in I/O
interfacing,
of any
high-level language
in
general
and C in
particular, while

the
reader
who
does have extensive skills
in
programming
to
understand
the use of
objects
in
C++.
Object-oriented programming will
be
shown generally
as an
alternative
to
using
operating systems
in a
microcontroller, although
it can
also
be
used
in
conjunction
with
operating

systems. Objects obviously provide encapsulation, protection,
and
factoring
of
common
code,
but
they
can
also provide
the
entire
infrastructure
that
is
also provided
by
an
operating system, such
as
memory
and
time management, including time-sharing.
Frankly,
an
operating system
is
often overkill, white object-oriented programming
can
provide

just
the
right amount
of
infrastructure
for an
application
in a
microcontroller
such
as the
6812.
An
operating system
is
desirable
if the
infrastructure that
it
provides,
such
as
managing disks
and
networks,
is
useful.
However,
if the
application merely

needs
some I/O,
such
as an
A-to-D converter,
using
an
operating system device driver
is
xvi
.Preface-
unnecessary,
and the
overhead
it
incurs
is
often objectionable.
In
this book,
we
show
you
how to
write what amounts
to a
tailor-made real-time operating system
for
your
microcontroller system.

We
show
you how to
write
classes
that are,
in
effect,
operating
system device drivers,
and we
provide
an
efficient
time-sharing kernel, which
is
adequate
for
the
kind
of
well-defined threads that
run in a
typical 6812 microcontroller.
After
hearing
a
presentation about
the use of
object-oriented programming

in
this
book, someone remarked that
it was
nice
to
show students
who
have learned object-
oriented programming
how it can be
used
in
microcontroller interfacing. There
is
much
more
to it. In
this book
we use
object-oriented programming
to
elevate
the
programmer
to
a
level
of
systems design. This object-oriented methodology promises

not
only
to
save software design cost,
but
also
to
integrate software
and
microcontroller organization
to
make more intelligent hardware/software trade-offs
and to
more
effficiently
implement
changes that might
be
indicated
after
these
trade-offs
are
evaluated.
Some remarks
on
this
book's
style
are

offered.
On the one
hand, terms
are
formally
introduced
and
used
as
carefully
as
possible. This
is
really necessary
to
make some sense
out
of a
subject
as
broad
and rich as
microcomputer system design. There
is no
room
for
muddy
terminology
or the use of
undefined jargon. Even though

the
terminology used
in
trade journals
and
manufacturers' applications notes
is
inconsistent
and
often
contradictory,
the
terminology used
in a
text must
be
clear
and
consistent.
On the
other
hand,
a
book
full
of
definitions
is too
hard
to

read.
The
first
version
of the
course
notes
that lead
to
this book tended
to be
ponderous. Moreover, students
are
more tuned
to
television
colloquialism today,
and are
turned
off by
"third person boring" that
is
often
the
style
of
many learned textbooks.
So we
condescend
to

"first
person conversational",
and we
enjoy
it. The
"we"
in
this book stands
not
only
for the
author
but
also
for his
colleagues
and his
teachers,
as
well
as his
students
- who
have taught
him a
great deal
and
who
have collectively inspired
and

developed
the
principles
and
practices discussed
in
the
book.
But we
admit
to
occasionally exploring Webster's Collegiate
for
just
the
right
word because
we
enjoy
the
challenge
and
even allowing
a pun or two
where
it
does
not
interfere
with

the
presentation
of the
material.
We
can't deny
it:
microcomputer
design
is
fun,
and we
have
fun
talking about
it.
Please
forgive
us if it
shows
in
this
book's
style.
G. J.
Lipovski
Austin,
Texas
February
3999

Figure Title Page
Figure 1,1. Analogy
to the von
Neumann Computer
3
Figure 1.2. Registers
in the
6812
13
Figure 1.3, Block Diagram Showing
the
Effect
of an
Instruction
31
Figure 1.4. Organization
of a von
Neumann Computer
32
Figure 1.5. Single-Chip Mode
of the
MC68HC812A4
32
Figure 1.6. Expanded Wide Multiplexed
Bus
Mode
of the
MC68HC812A4
33
Figure 1.7. Single-Chip Mode

of the
MC68HC912B32
34
Figure 1.8. Expanded Wide Multiplexed
Bus
Mode
of the
MC68HC912B32
34
Figure
1.9. Memory
Map of
6812 Microcontrollers
35
Figure 2.1. Conditional Statements
50
Figure
2.2. Case Statements
52
Figure 2.3. Loop Statements
53
Figure 2.4.
A
Huffman
Coding Tree
62
Figure 2.5.
An
Object
and its

Pointers
78
Figure
2.6. Other
Huffman
Codes
88
Figure 3.1. Voltage
Waveforms,
Signals,
and
Variables
94
Figure 3.2. Some Common Gates
98
Figure
3.3. Logic Diagrams
for a
Popular Driver
and
Register
99
Figure
3.4. 16R4
PAL
Used
in
Microcomputer Designs
103
Figure

3.5. Some Timing Relationships
105
Figure 3.6. Timing Relationships
for the
MC68HC812A4
105
Figure 3,7. Timing Relationships
for the
MC68HC912B32
107
Figure 3.8. Address
and
Data
Bus
Signals
109
Figure
3.9. Decoding
for
Table
3.1 110
Figure 3.10. Common Integrated Circuits Used
in
Decoders
111
Figure 3.11. Logic Design
of
Minimal Complete Decoder
112
Figure

3.12.
A
74HC74
116
Figure
3.13. Some
MSI I/O
Chips
117
Figure 4.1. Logic Diagrams
for I/O
Devices
for a
Narrow Expanded
Bus 124
Figure 4.2.
A
Readable Output Port
for a
Narrow Expanded
Bus 124
Figure 4.3.
An
Unusual
I/O
Port
127
Figure 4.4.
A Set
Port

129
Figure 4.5. Address Output Techniques
130
Figure 4.6. MC68HC812A4
Parallel
I/O
Ports.
138
Figure 4.7. MC68HC812A4 Parallel
I/O
Control Ports
139
Figure 4.7. MC68HC912B32 Parallel
I/O
Ports
140
Figure 4.9.
MC68HC812B32
Parallel
I/O
Control Ports
141
Figure 4.10.
Traffic
Light
148
Figure
4.11. Mealy Sequential Machine
151
xvn

List of Figures
xvHi
List
of
Figures
Figure 4.12.
A
Linked-List Structure
152
Figure 4.13. Connections
for a
Chip
Tester
154
Figure 4.14.
The74HCOO
155
Figure 4.15. M68ISA Time-of-Day Chip
163
Figure 4.16. Connections
to an LCD
Display
165
Figure 4.17. Simple Serial Input/Output Ports
167
Figure 4.18. Configurations
of
Simple Serial Input/Output Registers
168
Figure 4.19. Flow Chart

for
Series Serial Data Output
170
Figure 4.20. Dallas Semiconductor 1620 Digital Thermometer
171
Figure 4.21.
SPI
Data, Control,
and
Status Ports
173
Figure 4.22. Multicomputer Communication System Using
the SPI 174
Figure
4.23. Some
ICs for I/O 185
Figure 5.1. Paper Tape Hardware
194
Figure
5.2. State Diagram
for I/O
Devices
195
Figure 5.3. Flow charts
for
Programmed
I/O 198
Figure
5.4.
Key

Wakeup Ports
for the
MC68HC812A4
199
Figure
5.5.
Infrared
Control
202
Figure
5.6. Magnetic Card Reader
202
Figure 5.7. BSRX-10
203
Figure 5.8.
Key
Wakeup Interrupt Request Path
210
Figure 5.9.
IRQ
Interrupt Request Path
213
Figure 5.10. MC68HC812A4 Polled Interrupt Request Path
224
Figure 5.11. MC68HC912B32 Polled Interrupt Request Path
225
Figure 5.12. Flow Chart
for
Round-robin Interrupt Polling
226

Figure 5.13. MC68HC812A4 Vector Interrupt Request Path
229
Figure 5.14. MC68HC912B32 Vector Interrupt Request Path
230
Figure 5.15. Keys
and
Keyboards
232
Figure 5.16.
SPI
Network
235
Figure 5.17.
Bar
Code Frames
236
Figure
5.18. X-10 Frame
242
Figure
5.19. Periodic Interrupt Device
244
Figure 5.20. Centronics Parallel Printer Port
250
Figure 5.21. Connections
for
Context Switching
261
Figure 5.22. Fast Synchronization Mechanisms Using Memory Organizations
263

Figure 5.23. Indirect Memory Using
an
MCM6264D-45
264
Figure 5.24. Synchronization Mechanisms Summarized
267
Figure
5.25. 74HC266
271
Figure 6.1. MC68HC812A4 Mode Port
278
Figure 6.2. Memory Maps
278
Figure 6.3. MC68HC812A4 Port
E
Assignment
Register
280
Figure 6.4. MC68HC812A4 Internal Memory
Map
Control Ports
280
Figure 6.5. MC68HC812A4 Chip
Select
Memory
Map 281
Figure 6.6. MC68HC812A4 Chip
Select
Registers
282

Figure 6.7.
Use of
Chip Select Lines
282
Figure
6.8. MC68HC812A4 Memory Expansion Ports
284
List
of
Figures
xix
Figure 6.9. MC68HC812A4 Memory Expansion Mapping
285
Figure
6.10. MC68HC812A4 EEPROM Control Ports
287
Figure 6,11. MC68HC912B32 Flash Control Ports
288
Figure
6.12. MC68HC812A4 Clock Control
289
Figure 6.13.
An
MCM6264D-45 Memory System
29!
Figure 6.14. Analysis
of
Memory Timing
292
Figure 6.15. MC68HC912B32 Address

Demultiplexing
293
Figure 6.16. 8K-by-16-bit Word Wide Expanded Mode Memory System
294
Figure
6.17. Background Debug Module
295
Figure 7.1. Waveforms
of
Alternating Voltage Signals
307
Figure
7,2. Position Transducers
310
Figure
7.3. Power Output Devices
315
Figure
7.4. Triac Control Techniques
316
Figure 7.5. Operational Amplifier Circuits
318
Figure 7.6. Analog Switches
320
Figure 7.7.
A
Practical Voltage Follower
324
Figure
7.8.

Practical
Analog Switches
325
Figure
7.9.
555
Timer
326
Figure
7.10. Some Filters
328
Figure 7.11. Selecting Inputs
for a
Stereo Preamplifier
329
Figure
7.12. Computer Control
of
Amplification
331
Figure 7.13. Nonlinear Signal Conditioning
333
Figure
7.14. D-to-A Converters
336
Figure 7.15.
The
Crystal Semiconductor
CS4330
337

Figure 7.16. A-to-D Converters
339
Figure
7.17. Frequency-to-Voltage Conversion
342
Figure
7.18. Voltage-to-Frequency Conversion
342
Figure 7.19. Phase-locked Loop
344
Figure 7.20. Data Acquisition System
345
Figure 7.21. Serial Interface A-to-D Converter
347
Figure 7.22. A-to-D Subsystem
of the
MC68HC812A4
348
Figure
7.23.
The
Spline Technique
35!
Figure 7.24. Feedback Control
351
Figure 7.25. Some
OP AMP
Circuits
356
Figure 8.1.

The
Counter/Timer Subsystem
363
Figure 8.2. Output Compare Logic
364
Figure
8.3. Pulse-Width Module Ports
367
Figure 8.4.
A
Touch-Tone Generator
371
Figure
8.5. Timing
of
Some
Pulses
377
Figure 8.6.
Output
Compare
7
Ports
377
Figure 8.7.
Input
Capture
382
Figure 8.8.
The

Pulse Accumulator
386
List
of
Figures
Figure 9.1.
Peer-to-Peer
Communication
in
Different
Levels
398
Figure 9.2. Drivers
and
Receivers
402
Figure 9.3. Originating
a
Call
on a
Modem
407
Figure 9.4. Frame Format
for
UART Signals
408
Figure 9.5. Block Diagram
of a
UART (IM6403)
411

Figure 9.6. Transmitter Signals
414
Figure 9.7. TheACIA
416
Figure 9.8. Output
of a
Test
Program
417
Figure 9.9.
TheM14469
417
Figure 9.10. 6812 Serial Communication Interface
421
Figure
9.11. Synchronous Formats
428
Figure 9.12. BDLC Formats
431
Figure 9.13. BDLC Ports
432
Figure 9.14. IEEE-488
Bus
Handshaking Cycle
433
Figure
9.15, SCSI Timing
435
Figure 10.1.
The

Raster-Scan Display Used
in
Television
446
Figure 10.2. Character Display
447
Figure
10.3.
The
Composite Video Signal
448
Figure 10.4. Screen Display
449
Figure 10.5. Circuit Used
for TV
Generation
449
Figure 10.6. Display Hardware
451
Figure 10.7.
Bit and
Byte Storage
for FM and MFM
Encoding
456
Figure 10.8. Organization
of
Sectors
and
Tracks

on a
Disk Surface
458
Figure 10.9.
A
Special Byte (Data=OxAl, Clock Pulse Missing Between Bits
4 and 5)
459
Figure
10.10.
The
Western Digital WD37C65C
460
Figure 10.11. File Dump
465
Figure
10.12.
PC
Disk Organization
466
Figure 10.13. Dump
of a
Boot Sector
466
Figure 10.14. PC-file Organization
467
Figure 10.15. Dump
of a
Directory
468

Figure
10.16. Dump
of an
Initial
FAT
sector
469
Table Title
Page
Table 1.1. Addressing Modes
for the
6812
II
Table
1.2. 6812 Move Instructions
17
Table 1.3. 6812 Arithmetic Instructions
19
Table 1.4. 6812 Logic Instructions
21
Table
1.5. 6812 Edit Instructions
22
Table 1.6. 6812 Control Instructions
23
Table
1.7. Assembly-Language Directives
for the
6812
29

Table
2.1. Conventional
C
Operators Used
in
Expressions
47
Table
2.2. Special
C
Operators
49
Table 2.3. Conditional Expression Operators
49
Table 2.4, ASCII Codes
60
Table 3.1. Address
Map for a
Microcomputer
108
Table 3.2. Outputs
of a
Gate
116
Table 3.3. Another Address
Map for a
Microcomputer
117
Table 4.1.
Traffic

Light Sequence
149
Table
4.2.
LCD
Commands
165
Table 5.1. Interrupt Vectors
in the
6812
228
Table 7.1. Characteristics
of the
CA3140
321
Table
8.1.
PWM
Channel Ports
369
Table 8.2. Touch-tone Codes
370
Table 9.1. RS-232
Pin
Connections
for
D25P
and
D25S Connectors
404

Table
9.2. ACIA Control
and
Status Bits
415
xxi
List of Tables
Acknowledgments
The
author would like
to
express
his
deepest
gratitude
to
everyone
who
contributed
to the
development
of
this book.
The
students
of EE
345L
at the
University
of

Texas
at
Austin
during
Fall
1998 significantly
helped
correct this
book;
special
thanks
are due to
Levent
Og,
Ed
Limbaugh,
and
Greg McCasKill,
who
located most
of the
errors. This text
was
prepared
and run off
using
a
Macintosh
and
LaserWriter, running WriteNow.

I am
pleased
to
write this description
of the
Motorola 6812,
which
is an
incredibly
powerful
component
and a
vehicle
for
teaching
a
considerable range
of
concepts.
G.
J. L.
xxu
1
Microcomputer
Architecture
Microcomputers, microprocessors,
and
microprocessing
are at
once quite

familiar
and a bit
fuzzy
to
most
engineers
and
computer
scientists.
When
we ask the
question:
"What
is a
microcomputer?"
we get a
wide range
of
answers. This chapter aims
to
clear
up
these
terms. Also,
the
designer
needs
to be
sufficiently familiar with
the

microcomputer
instruction
set to be
able
to
read
the
object code generated
by a C
compiler. Clearly,
we
have
to
understand these concepts
to be
able
to
discuss
and
design
I/O
interfaces. This
chapter contains essential material
on
microcomputers
and
microprocessors needed
as a
basis
for

understanding
the
discussion
of
interfacing
in the
rest
of the
book.
We
recognize
that
the
designer must know
a lot
about basic computer architecture
and
organization.
But the
goal
of
this book
is to
impart enough knowledge
so
that
the
reader,
on
completing

it,
should
be
ready
to
design
good hardware
and
software
for
microcomputer interfaces.
We
have
to
trade material devoted
to
basics
for
material needed
to
design interface systems. There
is so
much
to
cover
and so
little space that
we
will
simply

offer
a
summary
of the
main ideas.
If you
have
had
this material
in
other courses
or
absorbed
it
from
your work
or
from
reading those
fine
trade journals
and
hobby
magazines devoted
to
microcomputers, this chapter should bring
it all
together. Some
of
you

can
pick
up the
material just
by
reading this condensed version. Others
will
get an
idea
of the
amount
of
background needed
to
read
the
rest
of the
book.
For
this chapter,
we
assume
the
reader
is
fairly
familiar
with some kind
of

assembly
language
on a
large
or
small computer
or is
able
to
pick
it up
quickly.
The
chapter
will
present
an
overview
of
microcomputers
in
general
and the
MC68HC812A4,
or
MC68HC912B32, single-chip microcomputer
in
particular.
1.1
An

Introduction
to the
Microcomputer*
Just
what
is a
microcomputer
and a
microprocessor,
and
what
is the
meaning
of
"microprogramming"
-
which
is
often
confused with "microcomputers"? This section will
survey
these concepts
and
other commonly misunderstood terms
in
digital systems
*Portions
of
§1.1 were adapted
with

permission
from
"Digital Computer Architecture"
pp.
298-327
by
G.
J.
Lipovski,
and
"Microcomputers,"
pp.
397-480
by G. J.
Lipovski
and T. K.
Agerwala,
in the
Encyclopedia
of
Computer Science
and
Technology, 1978, Belzer
et
al., courtesy
of
Marcel Dekker, Inc.
1
2
Chapter

1
Microcomputer Architecture
design.
It
describes
the
architecture
of
digital computers
and
gives
a
definition
of
architecture. Note that
all
italicized words
are in the
index,
and are
listed
at the end of
each chapter; these serve
as a
glossary
to
help
you
find
terms that

you may
need later.
Because
the
microcomputer
is
pretty much like other computers except
it is
smaller
and
less expensive, these concepts apply
to
large computers
as
well
as
microcomputers.
The
concept
of the
computer
is
presented first,
and the
idea
of an
instruction
is
scrutinized
next.

The
special characteristics
of
microcomputers
will
be
delineated
last.
1.1.1
Computer
Architecture
Actually,
the
first
and
perhaps
the
best paper
on
computer architecture, "Preliminary
discussion
of the
logical design
of an
electronic computing instrument,"
by A. W.
Burks,
H. H.
Goldstein,
and J. von

Neumann,
was
written
15
years before
the
term
was
coined.
We
find
it
fascinating
to
compare
the
design therein with
all
computers produced
to
date.
It is a
tribute
to von
Neumann's genius that this design, originally intended
to
solve
nonlinear
differential
equations,

has
been
successfully
used
in
business
data
processing, information handling,
and
industrial control,
as
well
as in
numeric problems.
His
design
is so
well defined that most computers
-
from
large computers
to
microcomputers
- are
based
on it, and
they
are
called
von

Neumann
computers.
In
the
early
1960s
a
group
of
computer
designers
at
IBM
-
including
Fred
Brooks
-
coined
the
term "architecture"
to
describe
the
"blueprint"
of the IBM 360
family
of
computers,
from

which several computers with
different
costs
and
speeds (for example,
the
IBM
360/50)
would
be
designed.
The
architecture
of a
computer
is,
strictly
speaking,
its
instruction
set and the
input/output (I/O) connection capabilities. More
generally,
the
architecture
is the
view
of the
hardware
as

seen
by the
programmer.
Computers with
the
same architecture
can
execute
the
same programs
and
have
the
same
I/O
devices connected
to
them. Designing
a
collection
of
computers with
the
same
blueprint
or
architecture
has
been done
by

several manufacturers. This definition
of the
term "computer
architecture"
applies
to
this fundamental level
of
design,
as
used
in
this
book. However outside
of
this book,
the
term "computer architecture"
has
become very
popular
and is
also rather loosely used
to
describe
the
computer system
in
general,
including

the
implementation techniques
and
organization discussed next.
The
organization
of a
digital
system like
a
computer
is
usually shown
by a
block
diagram
that shows
the
registers,
buses,
and
data operators
in the
computer.
Two
computers have
the
same organization
if
they have

the
same block diagram.
For
instance,
Motorola manufactures several microcomputers having
the
same architecture
but
different
organizations
to
suit
different
applications. Incidentally,
the
organization
of a
computer
is
also called
its
implementation. Finally,
the
realization
of the
computer
is its
actual
hardware
interconnection

and
construction.
It is
entirely reasonable
for a
company
to
change
the
realization
of one of its
computers
by
replacing
the
hardware
in a
section
of
its
block diagram with
a
newer type
of
hardware, which might
be
faster
or
cheaper.
In

this
case
the
implementation
or
organization remains
the
same while
the
realization
is
different.
In
this book, when
we
want
to
discuss
an
actual realization,
we
will name
the
component
by its
full
part number, like MC68HC812A4PV8.
But we are
usually
interested

in the
organization
or the
architecture only.
In
these cases,
we
will
refer

×