Download Microcontrollers - Goodheart

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Fault tolerance wikipedia , lookup

Microprocessor wikipedia , lookup

Emulator wikipedia , lookup

Manchester Mark 1 wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Transcript
This sample chapter is for review purposes only. Copyright © The Goodheart-Willcox Co., Inc. All rights reserved.
456
26
Objectives
After studying this chapter, you will be able to:
K Define microcontroller.
K Explain the difference between a computer and a
microcontroller.
K Explain the function of main microcontroller parts.
K Identify various types of ROM.
K Name the general types of programming languages.
K Name some common components found on a
microcontroller module.
Key Words and Terms
The following words and terms will become important pieces of your electricity and electronics vocabulary.
Look for them as you read this chapter.
arithmetic/logic unit
(ALU)
assembler program
assembly language
editor
electrically erasable
programmable read only
memory (EEPROM)
erasable programmable
read only memory
(EPROM)
high-level language
interpreter
library
machine code
machine language
macro
masked ROM
microcontroller
microcontroller interface
object code
opcode
operand
port
program counter
programmable read only
memory (PROM)
register
source code
stack
stack pointer
watchdog timer
word size
This chapter introduces you to the microcontroller.
The microcontroller is a programmable integrated circuit.
This means that the microcontroller can be programmed
to perform a set of specific functions. The microcontroller
Microcontrollers
is, therefore, the merging of programming languages and
digital logic components. Complex controls that once
required thousands or even millions of individual components are duplicated in a single chip. The information in
this chapter builds a bridge of technical knowledge
between software programs and digital ICs. You will discover how computer software can be used to control integrated circuit devices to achieve the same results as control systems that require many discrete devices. The
microcontroller has dramatically changed the way control
circuitry is designed.
26.1 OVERVIEW OF THE
MICROCONTROLLER
A microcontroller is an integrated circuit (IC) that
can be programmed to perform a set of functions to control a collection of electronic devices. Being programmable is what makes the microcontroller unique. Often
thought of as a “tiny computer” on a single chip, the
microcontroller is used in many applications and can be
found in almost every electronic device we encounter
daily. In this section, you will learn what makes the
microcontroller comparable to the computer. You will
also learn why the microcontroller has replaced many
relays and solid-state devices in industry.
Electronic Communication and Data Systems
See Figure 26-1. Although the microcontroller and
the computer share many common components, there are
many distinct differences between them. The major differences between a computer and a microcontroller are
•
The limitations of the microprocessor.
•
The amount of memory available for data and
program manipulation.
•
The type of storage available to permanently
store a program.
A microprocessor inside a computer is more powerful and more versatile than that in a microcontroller. The
computer’s microprocessor allows the computer to run a
variety of programs and applications. A single computer
can be used to perform a variety of functions such as
word processing, playing a DVD, playing an interactive
game over the Internet, monitoring security systems, and
making a CAD drawing.
A microprocessor in a microcontroller has a limited
instruction set that allows it to run a specific application.
In other words, the microcontroller can only understand a
limited number of commands, and these commands are
specific to the functions the processor is designed to handle. For example, a microcontroller may be designed
specifically to monitor and control a fuel injection system
on an automobile. In fact, every car manufactured today
has many microcontrollers incorporated into its electrical
system—antilock brakes, fuel control, air conditioning,
heating, and keyless security locks. Each microcontroller
on a vehicle is designed to handle a specific function.
The microcontroller, like the computer, uses RAM to
store programs and data temporarily. The amount of
RAM built into a microcontroller is minimal and is usually enough for its intended use. The RAM in a computer
is ample enough to run a variety of memory intensive
applications.
All computers and microcontrollers must have some
sort of ROM that contains a permanent set of instructions
so that they can communicate with other devices. The
microcontroller stores in its ROM the program it needs to
perform a specific job. The computer has a ROM, called
the BIOS, that contains BASIC programming functions.
The computer stores the rest of its programs, operating
system and applications, in other storage devices such as
a hard drive. A larger capacity for program storage makes
the computer more versatile and powerful than a microcontroller and allows it to run many different programs.
Although computers are versatile and powerful, the
microcontroller has its advantages. Microcontrollers are
small and can fit inside other devices like an appliance or
a vehicle. Microcontrollers cost less to produce, and they
Microprocessor
ROM
(BIOS)
Program and
data
storage
RAM
Microprocessor
Comparison and Contrast of the
Microcontroller and the Computer
RAM
ROM
You will find that many of the terms you will learn
in this chapter can also be applied to computers. This is
because the microcontroller and the computer share many
common components:
•
A microprocessor to process instructions.
•
Memory locations to store data and programming information.
•
Input/output facilities to move data between
itself and another device.
455
RAM
Figure 26-1. The microcontroller and the computer share similar components.
Chapter 26
consume less power. Microcontrollers are used in almost
all types of electronic equipment, from coffeepots to laser
printers. They are even incorporated into computer systems. They can be found in floppy drives, CD-ROM
drives, and video cards. A microcontroller is often
embedded into the electronic circuit boards of these computer devices. A microcontroller is usually a single chip,
but when incorporated into a large control system, it is
referred to as an embedded controller, Figure 26-2. An
embedded controller often depends on other components
in the system, such as additional memory, to perform
its function.
Microcontrollers are also used heavily in industry.
They have taken the place of relays, solid-state devices,
and other discrete components.
Microcontrollers
457
Reusable
The typical microcontroller is programmable, which
means it is reusable. This is especially advantageous for
prototyping control circuitry. When developing a complex control system, it is not unusual for it to fail when
first applied. As a matter of fact, a complex control project may need to be rewritten and/or rewired many times
before it meets design expectations. The fact that the control circuit can be modified by programming rather than
rewiring is very advantageous for fast project prototype
development.
Integrated circuits, such as the microcontroller, are
much more dependable than relays. Before microcontrollers, control circuitry relied on many electromechanical relays and timers to control the system. Relays depend
on electromagnets to move armature and contact parts, so
they eventually wear out due to mechanical friction.
Relays are also susceptible to damage caused by
dust, dirt, corrosion, rust, insects, and other contaminants
that can interfere with the moving parts. Microcontrollers
have no moving parts. This provides a much higher rate
of reliability. Relays and high-power transistors can be
incorporated for final applications to motors, but the
actual timing and control logic does not need to rely on
the mechanical action of relays.
Microcontrollers can be produced at lower costs than
their electromechanical predecessors. Also, microcontrollers can be reprogrammed if the designed application
does not work correctly or if the application for its use
changes.
Energy efficient
Advantages of Using Microcontrollers in
Industry
There are a number of advantages to using microcontrollers in industry. Some of the major advantages of
microcontrollers are that they are reusable, dependable,
cost effective, and energy efficient.
Electronic Communication and Data Systems
need for skilled programmers and the sensitivity of the
controllers to electrostatic charges.
EPROM
ROM
Programming complexity
Special skills are required to program the microcontrollers. This requires a higher level of training for some
personnel. In addition, there are many different programming languages to choose from. This can lead to a compatibility problem when attempting to merge two dissimilar systems into one control system.
Stack
area
Program
counter
Bus
Instructional
register
Oscillator
General
purpose
register
Instruction
decoder
Watchdog
timer
Electrostatic sensitivity
Dependable
Cost effective
Figure 26-2. The microcontroller inside this refrigerator
contributes to energy and cost savings by monitoring temperature and controlling the compressor. Courtesy of
Motorola, Inc.
458
Because the majority of the circuitry is made from
integrated circuits, the energy cost of using a microcontroller is much less than if using individual components
of a relay-type logic circuit. Relay logic uses numerous
relays wired in series and parallel to form control circuit
conditions similar in function to logic gates. A microcontroller consumes less electrical energy than conventional
electromechanical devices.
Disadvantages of Using Microcontrollers
in Industry
There are a few disadvantages to using microcontrollers. The two most prominent disadvantages are the
Most microcontrollers are composed of complimentary metal-oxide semiconductor (CMOS) integrated circuitry. CMOS can be damaged easily by a static charge.
Static precautions must be strictly obeyed.
Review Questions for Section 26.1
1. Name three differences between a computer and a
microcontroller.
2. What types of devices are controlled by a microcontroller?
3. What is an embedded controller?
4. What are four advantages of using microcontrollers
in industry?
5. Name two disadvantages of using microcontrollers
in industry.
26.2 MICROCONTROLLER
COMPONENTS
The microcontroller consists of thousands of digital
circuits. These digital circuits are combined into areas that
provide specific functions. Examine Figure 26-3. The
simplified block diagram illustrates how the major
sections inside the microcontroller work together to
process the program instructions. The parts of the microcontroller are used to save data and programs, perform
math and logic functions, and generate timing signals. The
different areas are connected by a bus system. The bus
system contains tiny parallel circuits that carry the digital
pulse patterns from section to section. The ROM stores
the program required for the microcontroller to function.
The ROM controls how the chip components operate and
how data and instructions flow through the chip.
ALU
Port A
Port B
Port C
Figure 26-3. A simplified block diagram of a typical microcontroller. All the major components are connected
together by a bus system. Data and instructions are moved
around the chip via the bus.
Ports and Registers
There are many different types of registers and ports
found inside a microcontroller chip. Ports and registers
are special memory locations dedicated to a specific
function such as a hardware location or a place to manipulate data. Some ports correspond to the input and output
pin assignments of the chip. By placing a one or zero into
a specific port address, you can change the pin assignment of the microcontroller from an input pin to an output pin. Ports can also contain information sent to the
pins on the microcontroller chip from sensors or
switches. The contents can then be stored in memory or
remain in the port.
A register can be used to hold the contents of data
being manipulated. For example, when performing a
mathematical operation, the data being manipulated must
be stored in two different registers and the result placed
in a third register. See Figure 26-4.
In the illustration, you see three registers labeled as
A, B, and C. The content of register A and the content of
register B are added together and then stored in register
C. Another typical use of a register is to store a value to
be compared to another register value. For example, compare the content of register A to the content of register B.
If the values are identical, an action is initiated such as
turning on a valve, activating a door switch, or turning on
an alarm. The contents of the two registers, A and B,
could compare a temperature rise to a preset value, or the
distance moved by a robotic arm. As you work with programs and memory, the use and applications for registers
will become more apparent.
Chapter 26
Register A
00001010
Register A
00001010
Register B
00110000
Register B
00110000
Register C
00111010
When the content of register A is
equal to the content of register B, set
off the alarm.
Figure 26-4. The contents of the two registers can be compared to determine if an action should begin.
Microcontrollers
459
ALU
The arithmetic/logic unit (ALU) performs common
mathematical and logical operations on data. The logical
operations are similar to the digital devices you have
studied earlier in this textbook. Some typical logic functions are similar to the AND, OR, NOT, NOR, and
NAND digital gates.
Oscillator
Stack Pointer and Program Counter
The area in memory that is used to store data and
program information is called the stack. The most common use of the stack is to store the program address of the
next instruction to be executed. Since the processor
processes commands sequentially, one after the other
until complete, it relies on the stack to temporarily store
the address of the next instruction in case the main program is interrupted. A special device called the stack
pointer keeps track of the last stack location used while
the processor is busy manipulating data values, checking
ports, or checking interrupts. Look at Figure 26-5.
Stack
Stack
pointer
value
00 A2
10101001
11110000
00110011
01010101
00011100
11001000
10100010
00110001
Instruction
addresses
and data
Figure 26-5. The stack pointer is responsible for keeping
track of the sequence of memory locations, referred to as
the stack, while the computer is manipulating data and
checking interrupts.
Interrupts are, as the name implies, a way to interrupt the processor while it is processing instructions and
data to perform a different task. The interrupts come in
two varieties: hardware interrupts and software interrupts. An example of a hardware interrupt is when input
pin number 3 receives an electrical signal from a push
button, and the microprocessor stops what it is doing to
respond to the signal. While it is responding to the push
button signal (a hardware interrupt), the contents of the
program counter are pushed onto the stack. The stack
pointer bookmarks the location of the last item pushed
onto the stack. When the processor finishes servicing the
interrupt, the counter address is popped from the stack
and returned to the program counter, and the microprocessor continues processing the instructions in order.
The oscillator is a complex digital device that
requires a steady digital pulse for timing. All of the separate functions are controlled by one central timing system. The timing pulse provides the basis for proper
sequence of all the separate sections of the microcontroller chip. The source of the steady pulse rate is the
oscillator circuit.
460
Electronic Communication and Data Systems
memory (RAM) and read only memory (ROM). These
two types of memory are classified according to their
function as memory units.
You have already learned about RAM in detail in
Chapter 25—Introduction to the Personal Computer
(PC). Random access memory is used in the microcontroller just as it is in the computer, to temporarily store
programs and data. It is volatile memory, which means it
loses its contents when electrical power is no longer
applied to the memory chip. Power can be lost due to
power failures or by simply turning off the power switch.
Read only memory is used to permanently store a
program or data and retain the information even when the
power is disconnected from the unit. It is described as nonvolatile memory. In Chapter 25 you learned about ROM
only as it applied to the computer’s BIOS. In this chapter,
we will take a closer look at the many types of ROM.
Programmable Memory
Watchdog Timer
A specialized program often found as part of the
microcontroller is called a watchdog timer. The watchdog
timer is designed to prevent the microcontroller from
halting or “locking up” because of a user-written program. Remember that a processor processes instructions
step-by-step. It will wait until one instruction is completed before processing the next. A problem can arise if
a command is issued to a processor that cannot be accomplished. For example, a program is written by a user and
contains an instruction calling for checking the input
value of I/O pin 36. If there is no I/O pin 36, the microcontroller will wait forever for input. It will not process
the next command until it receives input on pin 36. Without the watchdog timer, the microcontroller would halt
and thus fail to process further instructions.
The watchdog timer uses a routine that is based on
timing. If a program has not been completed or repeated
as a loop within a certain amount of time, the watchdog
timer issues a reset command. A system reset sets all the
register values to zero. The reset feature allows the controller to recover from the crash. It releases the program
and sets the controller to start over again.
Memory
As stated earlier in this chapter, microcontrollers and
computers use various types of memory. These types of
memory are usually referred to by acronyms. The two
broad classifications of memory are random access
ROM is classified as to how it is constructed and
how the program code inside the chip can be altered.
Masked ROM (usually referred to as just ROM) is a special type of memory that is permanently programmed
during the manufacturing process. Masked ROM memory cannot be reprogrammed. To change the program in a
system using masked ROM, you must use an entirely different masked ROM chip.
Programmable read only memory (PROM) is memory that is programmed after it is manufactured. It is manufactured with thousands of empty memory cells. A program writer "burns" a pattern into the empty cells,
forming a program in the blank PROM chip. Like masked
ROM, a PROM chip program is permanent for the life of
the chip.
Erasable programmable read only memory
(EPROM) is a special type of PROM that can be reprogrammed many times. When manufactured, a small window is left on the chip. Any program entered into the chip
can be erased at a later time by shining an ultraviolet light
through the window on the chip. The chip can then be
reprogrammed using a programming device.
Electrically erasable programmable read only
memory (EEPROM) is a type of memory chip that can be
erased electrically and then reprogrammed. It is commonly used for the computer BIOS chip.
A microcontroller is usually constructed with a
PROM on the chip itself containing the basic set of
instructions for running the microcontroller and its internal parts. This program is permanent and not alterable. In
addition to the basic ROM, another section of memory is
used to store programs written by the user. It typically
contains a small EEPROM memory area. If larger program storage is required, additional EEPROM chips can
be connected to the microcontroller.
Review Questions for Section 26.2
1. The special function areas of a microcontroller chip
are connected using an _____ system.
2. The _____ keeps track of the data held in the stack
area.
3. Math and logic functions are provided by the
_____.
4. The _____ circuit provides a steady pulse for timing.
5. A(n) _____ prevents the microcontroller from
crashes caused by bad programming.
26.3 PROGRAMMING
LANGUAGES AND
TERMINOLOGY
A variety of languages are used to program controllers and computers. These languages range from the
most basic level of machine language to a wide selection
of high-level languages. Some languages used to program
microcontrollers are C, C++, BASIC, Visual BASIC,
Quick BASIC, and numerous proprietary languages such
as PBASIC for the Parallax microcontroller. Software
programs especially written for programming microcontrollers are usually referred to as editors. Look at the
illustration in Figure 26-6. Use this drawing as a reference while we discuss the various languages used to program microcontrollers.
Machine Language, Assembly Language,
and High-Level Languages
One language that can be used to program a microcontroller is machine language. Machine language, or
machine code, is a computer language constructed of
ones and zeros that represent binary codes and digital
voltage pulse levels. The computer understands machine
code without further conversion. There is no form of code
that is closer to the machine itself, hence the name
machine code. Machine code is also referred to as object
code or executable code. Machine code matches the type
of processor with which it is communicating. For example, an Intel processor cannot use a machine code written
for a Motorola (Apple) processor, or vice versa.
Chapter 26
High level
programming
languages
Source
code
C, C++, Basic,
Visual BASIC
Mnemonics
opcode and
operand
Compiler
Assembler
Machine
code
Digital pulses
Microcontrollers
461
The main differences between using a high-level
language and using assembly language is the amount of
code stored after being converted into machine language,
and the speed of its execution. Assembly code is difficult
and time-consuming to program in, but the code uses less
memory and executes faster than high-level code.
The assembly code is much more exacting than highlevel programming languages. For example, when using
an assembly, you must indicate the exact memory location
in which to place the data, and each command must be in
sequence followed by the proper amount of data bits. This
is more work for the programmer. A high-level language
is not as restrictive. The programmer must simply indicate
that data needs to be stored in a file. However, when highlevel language is compiled into the low-level machine
language, allowances must be made for the fact that no
specific memory location was specified. The program
must use an additional set of commands to locate a free
block of memory that does not conflict with another data
block. Higher-level languages are also not as restrictive
about putting commands in exact sequence for execution.
Assembly code must be in exact sequence, or it will lock
up (crash) the system when implemented.
Figure 26-6. Compiled from high-level languages, programming language or source code is turned into machine code
by a compiler. Machine language is closest to the digital
signals found moving through the system circuitry. Binary
ones and zeros can be used to illustrate the digital pulse
patterns. Compilers and assemblers convert high-level
programming languages and assembly language into
machine language.
Programming Terminology
Machine code is difficult to learn and inefficient for
writing programs. The main advantage of using machine
language programs is that they run more quickly than
other types of programming.
Machine language programming was the original
way to program a microprocessor. Programming in this
language was very tedious, and it took many hours to
write even the simplest program. Soon, a faster programming language was invented called an assembly.
Assembly language uses a series of words, called
mnemonics, combined with hexadecimal data values to
create a program. After the program is written, it is
assembled. An assembler program converts the series of
instructions into a machine code that can be read by the
processor. Programs written in assembly language are very
compact, efficient, and fast. However, simple programs
still can take hours to write using assembly language.
Most programmers write programs using high-level
languages such as C, C++, BASIC, Visual BASIC, and
Quick BASIC because it is much easier and faster. Very
large programs that would be almost impossible to write
in assembly language can be written using a high-level
language. The high-level language can then be assembled into machine language code by using a program
called a compiler.
One of the most difficult aspects of programming is
learning all of the new terminology. Many new words
have been created, and many common words, such as bit,
word, and library, have been given new meanings.
Knowledge of these terms will help you understand the
manuals that come with microcontrollers.
The size of a normal group of bits processed at one
time through a computer system is referred to as its word
size. The word size typically matches the bus width of the
chip. Some common word sizes are 8, 12, 14, 16, 24, 32,
and 64 bits in length. A word usually contains an instruction and some data to be acted on.
The opcode is the part of a word that contains the
instruction to be carried out. Instructions are typically followed by data referred to as the operand. This is a repeating sequential pattern used in programming. Together, the
opcode and the operand combine to form the source
code. Source code is simply another name for the program written by the programmer. The source code is then
converted into machine code by an assembler or a compiler. When source code is assembled or compiled, it
becomes what is known as object code and is saved as a
file. Object code is also called executable code or
machine code. Source code can also be run directly by an
interpreter. The interpreter runs the source code without
compiling the code into machine code, Figure 26-7.
462
Electronic Communication and Data Systems
Many times programmers will save the programs
they have written so that they can use the code again on
another control system. As you work with programmable
devices, you will notice how much of the code can be
reused for another project. A library is a collection of
code modules. For example, the robot project that is covered later in this chapter can be programmed to perform
in a number of ways. It can be programmed to run while
connected to a PC. The keyboard arrow keys can be used
to control the robot’s movement. The robot can also be
programmed to follow a line drawn on a piece of cardboard without being connected to a PC. In addition, the
robot can be programmed to use infrared sensors to detect
objects in front of it such as a wall. When the object is
detected, the robot makes a 90-degree right turn. In every
case, you can reuse some of your code blocks rather than
rewriting the complete program from scratch. The parts
that are reusable can be saved to a code library. Libraries
are sometimes referred to as code modules. Some software systems allow you to call the code modules out of a
storage memory from the main program.
Opcode Operand
Source code
Interpreter
Object code
(Stored as a file)
Assembler
or compiler
Figure 26-7. Source code can be run straight from an
interpreter or from object code.
A macro is a collection of assembler instructions
represented by a single word. A macro assembler converts macros into machine language. The term macro and
mnemonic are often used interchangeably when talking
about programming. Mnemonics, discussed earlier with
assembly language, are instructions that can be converted
directly into machine language. The mnemonic itself
looks like a short abbreviation of the actual command
being issued. For example, MOV might be used to represent the instructions to move data.
Review Questions for Section 26.3
1. The most basic language, which is composed of
ones and zeros, is called _____.
2. Name three high-level languages.
3. Which portion of the source code contains the data,
the opcode or the operand?
26.4 THE MICROCONTROLLER
MODULE
A microcontroller can consist of a single chip, or it
can be composed of several components collected
together on a circuit board to function as a single unit.
When a microcontroller is mounted on a circuit board
with other components, it is sometimes referred to as a
module or a microcontroller board.
A microcontroller module typically consists of a
microcontroller, a power source, an interface for connecting to a programming device, I/O ports, and additional
memory. A power source is required to power the
microcontroller and any accompanying components
located on the printed circuit board. An interface is
needed to communicate with the controller. The
microcontroller interface is usually an older serial connection or a universal serial bus (USB) connection matching the ones found on most PCs. A set of input/output (I/O)
ports is used to send and receive signals from the devices
the microcontroller is designed to control. The microcontroller’s I/O pins can be programmed as output or input
pins. When programmed as an output pin, each pin can
output digital signals. When programmed as an input pin,
each pin can receive digital signals. Devices commonly
controlled through the I/O ports are relays, power transistors, servos, stepper motors, LEDs, and solenoids. Devices
that commonly send electrical signals to the I/O ports are
photocells, piezocells, thermistors, and thermocouples.
LEDs are often used to indicate the presence of power or
that communication is taking place, Figure 26-8.
Depending on the microcontroller’s intended use,
you might find timing oscillators. The oscillator circuit
can be a separate component or incorporated into the
microcontroller chip. When constructed separately from
the chip, a crystal or ceramic resonator is used as well as
capacitors and resistors. See Figure 26-9. One advantage
of using a separate oscillator circuit is that it can produce
a number of frequencies rather than a fixed frequency. By
having the ability to produce a wide range of frequencies,
the microcontroller can match the frequency required of
a large control circuit in which it may be embedded.
Additional components such as digital-to-analog and
analog-to-digital converters can be used to change the
digital pulses into analog signals. If you need to drive a
device requiring a higher output current, you can simply
Chapter 26
Input and output
devices
Relays
Power transistors
LEDs
Speakers
Stepper motors
Photocells
Piezocell
Thermocouple
Thermistor
Switches
Power
supply
Microcontroller
Interface
I/O
ports
Mem
Additional
memory
Figure 26-8. The basic components found on a microcontroller module and the typical devices to which the microcontroller interfaces.
PIC16C5X
OSC1/CLKIN
Crystal
OSC2/CLKOUT
Capacitors
Figure 26-9. The PIC exterior oscillator circuit is a simple
crystal with two capacitors to form a tank circuit. The exact
values of the crystal and capacitors will vary depending
on the desired frequency and the exact model of PIC
being used.
incorporate a small mechanical or solid-state relay
between the output pin and the load device.
A microcontroller chip usually contains a relatively
small amount of programmable memory. If a microcontroller is going to be used to run a large or complicated
program, or if much data is to be stored, additional RAM
and ROM can be mounted next to the microcontroller.
A reset button can be added to clear the processor if a
program fails.
Microcontroller Relay Circuit
Many times, the device or devices to be controlled
by a microcontroller far exceed the electrical power limits of the controller. A method must be used to increase
the power output capabilities of the microcontroller. This
can be accomplished many different ways. One common
Microcontrollers
463
way is using mechanical or solid-state relay devices.
Figure 26-10 shows an application for controlling a
device when the electrical requirements far exceed the
output power of the microcontroller I/O pins.
To drive a load of higher current, a special circuit
must be built and inserted between the microcontroller
module and the load device. The circuit in the schematic
is simple in design. The output pin of the microcontroller
connects directly to an optic coupler. The optic coupler
consists of an LED and a photosensitive transistor. The
optic coupler is designed to prevent higher voltage levels
and short circuits in the load device circuit from damaging
the sensitive microcontroller chip through a backfeed situation. This could happen if a typical transistor was used.
When the LED inside the optic coupler is energized
by the output of the microcontroller, the light forces the
photosensitive transistor into conduction, thus completing the circuit to the relay. When the relay energizes, the
relay contact closes and completes the circuit to a heavyduty device such as an appliance, some lighting, or a
welder. Note the fact that the heavy appliance load can
use any type of power supply, such as 120 Vac, 240 Vac,
12 Vdc, and 48 Vdc.
The components, such as the transistor, are sized
according to the relay coil requirements. The diode
placed in parallel with the relay coil is used to prevent an
inductive kick from damaging circuit components. As
you recall from the study of inductance reactance, when
a coil is energized or de-energized, the induction voltage
created can be quite high. This can damage circuit components such as transistors. The diode prevents the high
level by short-circuiting the induction created by the
relay coil. When ac is used as the applied voltage in the
load circuit, capacitors can also be incorporated into the
circuit to prevent damage induced by the ac circuit. In
place of a mechanical or solid-state relay, high power
transistors, SCRs, or even a TRIAC can be used to
increase the output capabilities of a microcontroller.
Microcontroller Interface
Most microcontrollers interface with an IBM compatible computer through the serial or parallel port. See
Figure 26-11. This figure shows the connections for a
standard 9-pin serial port PC connection and the
PIC16C5X microcontroller, from Microchip Technology.
The 9-pin serial port connection is also called a DB-9.
The Pins 1, 2, 3, and 4 on the microcontroller are tied
directly to pins 2, 3, 4, and 5 on the 9-pin serial port
connection. Note that pins 6 and 7 are connected together
on the serial port of the PC. This allows the PC to
464
Electronic Communication and Data Systems
120 AC voltage
source
AC
Connect to I/O
pin
9 VDC
High current load
for example:
appliance motor,
welder, lighting, etc.
Optic
coupler
Relay
Relay
contact
The optic coupler
is used to
electrically isolate
the microcontroller
from the relay
circuit.
Figure 26-10. A microcontroller chip can be connected to heavy loads using a typical circuit as shown.
BS2-IC
PIC16C5X
DSR
6
7
RTS
8
9
1
2 Rx
3 Tx
4 DTR
5 GND
1
SOUT
2
SIN
3
ATN
4
VSS
5
Serial port on PC
DB9
Pin connections on
microcontroller chip
Figure 26-11. This is an illustration of the serial port connections using a BS2-IC. The connection for the BS1-IC is
different. Always check the technical specifications of the
microcontroller chip.
automatically detect communications on the port. Also,
be aware that this illustration only refers to a connection
to a PIC16C5X microcontroller. It is important to always
check the technical specifications sheet of the particular
microcontroller you are using. Microcontroller pins for
different models have different pin functions. In addition,
the parallel port, as well as the USB port, can be used for
some microcontroller communications.
Review Questions for Section 26.4
1. A microcontroller that consists of several components is called an _____.
2. Name four major parts of a microcontroller module.
3. What is one advantage of using a separate oscillator
circuit?
4. Which ports found on an IBM compatible PC can be
used to download a program to a microcontroller?
5. Do all microcontrollers use the exact same pin
location to connect to a serial port?
26.5 GETTING STARTED WITH
MICROCONTROLLERS
One of the most used microcontrollers by students
and hobbyists are the PICmicro® microcontrollers by
Microchip Technology. There are a variety of educational kits that incorporate the PICmicro chips. Two of
the most popular kits are the BASIC Stamp 1 & 2 starter
kits from Parallax and the PIC BASIC Compiler Bundle
from microEngineering Labs.
What makes the BASIC Stamp microcontroller
unique is that it comes with a built-in interpreter. As you
recall, an interpreter reads one line of code at a time and
Chapter 26
processes the command. Therefore, you do not need to
compile the source code to run the program. You can program the BASIC Stamp with Parallax’s own BASIC language called PBASIC. The BASIC Stamp is sold as a
complete package with the BASIC Stamp controller, carrier board, Stamp Software, and cable or as separate components. The cable for the BASIC Stamp 1 connects to
the parallel port of your PC, and the cable for the BASIC
Stamp 2 connects to the serial port of your PC; although,
the BASIC Stamp software communicates serially
through both these ports as it downloads the program to
the BASIC Stamp.
The PicBASIC Compiler Bundle from microEngineering Labs comes with the PicBASIC Compiler, a software package that compiles your BASIC program and
downloads it to the PICmicro microcontroller. Included
in the Compiler Pro Bundle is the PicBASIC Compiler
Pro software that is compatible with BASIC Stamp 2
commands. Both kits come with the EPIC Plus Programmer and a 25-pin cable that connects the programmer
board to the parallel port of your PC.
Review Questions for Section 26.5
1. What is one the most popular microcontrollers used
by students and hobbyists?
2. The language used to program the BASIC Stamp is
______.
Summary
1. A microcontroller is an integrated circuit that can
be programmed to perform a specific function.
2. When a microcontroller is part of a larger control
assembly, it is often referred to as an embedded
controller.
3. A microcontroller is perfect for prototyping because
it can be easily reprogrammed.
4. A watchdog circuit is designed to prevent a microcontroller from locking up.
5. Machine language is the lowest-level programming
language and the language closest to the hardware.
6. Most programmers use high-level languages to
write programs for computers as well as
microcontrollers.
7. Computers are used to program microcontrollers,
and the program is transferred to the microcontroller using serial, parallel, or USB ports.
Microcontrollers
465
466
Electronic Communication and Data Systems
Test Your Knowledge
Please do not write in the text. Place your answers on
a separate sheet of paper.
1. Explain the difference between a microcontroller
and a personal computer.
2. What is the purpose of the watchdog timer?
3. What does the acronym PROM represent?
4. What does the acronym EPROM represent?
5. What does the acronym EEPROM represent?
6. The programming language constructed of ones and
zeros that represent binary codes and digital voltage
pulse levels is called _____ language.
7. Name three or more high-level programming
languages.
8. A(n) _____ is used to convert a high-level language
into machine language.
9. A word is a combination of _____ and _____.
10. Word size is typically equal to the _____ width of
the chip.
11. _____ and _____ are special memory locations
dedicated to a specific function such as an I/O port.
12. A microcontroller that consists of several components is called an _____.
For Discussion
1. Discuss the differences between a computer and a
microcontroller and describe some applications for
each.
2. Discuss the different kinds of read only memory.
3. Describe some of the functions of a
microcontroller.
4. Describe some applications that you can think of
for a microcontroller.
5. Discuss your experience with the exploration of
technical web sites and your future plans for
accomplishing this important task.
Closeup of an eraseable programmable read only memory (EPROM) module. Shining an ultraviolet light through the
EPROM’s tiny window can erase a program that has been written to the EPROM.