2.embedded Microcontrollers
2.embedded Microcontrollers
1
2. EMBEDDED MICROCONTROLLERS
2.1 Introduction
2.2 Structure of a basic computer system
2.3 CPU families used in microcontrollers
2.4 Basic I/O devices and technologies
2.5 Interrupts and Memories
2
2.1 INTRODUCTION TO MICROCONTROLLERS
• Often used to run dedicated code that controls one or more tasks in the
operation of a device or a system.
• Also called embedded controllers, because the microcontroller and support
circuits are often built into, or embedded in, the devices they control.
• Microcontrollers usually must have low-power requirements since many
devices they control are battery-operated.
INTRODUCTION
Microcontrollers have only been with us for a few decades
but their impact (direct or indirect) on our lives is profound.
Their presence is unnoticed at most of the places like
At supermarkets in Cash Registers, Weighing Scales, etc.
At home in Ovens, Washing Machines, Alarm Clocks, etc.
At play in Toys, VCRs, Stereo Equipment, etc.
At office in Typewriters, Photocopiers, Elevators, etc.
In industry in Industrial Automation, safety systems, etc.
On roads in Cars, Traffic Signals, etc.
4
INTRODUCTION
Simply an embedded controller is a controller that is
embedded in a greater system.
One can define an embedded controller as a controller (or
computer) that is embedded into some device for some
purpose other than to provide general purpose computing.
Is an embedded controller the same as a microcontroller? The
answer is definitely no.
One can state devices such as 68000, 32032, x86, Z80, and
so on that are used as embedded controllers but they aren’t
microcontrollers(example: 8051 Microcontroller).
5
2.2 STRUCTURE OF A BASIC COMPUTER SYSTEM
7
Figure 2.2 Simplified block diagram of a computer system
BASIC COMPUTER SYSTEM
The width of the address bus determines the memory
addressing capacity of the processor.
The width of the data bus indicates the size of the data
transferred between the processor and memory or I/O device.
For example, the 8086 processor has a 20-bit address bus and a
16-bit data bus.
The amount of physical memory that this processor can address
is 220 bytes, or 1MB, and each data transfer involves 16 bits.
The Pentium, on the other hand, has 32 address lines and 64
data lines.
Thus, the Pentium can address up to 232 bytes, or a 4-GB
memory.
BASIC COMPUTER SYSTEM
The maximum memory size is calculated as:
The number of memory locations (addresses)*the size of
each memory location
The size of each memory location is equal to data bus
width/memory word size.
Example 2: Calculate the maximum memory size of a
computer with a 24 bit address bus and a memory word size
of 16 bits.
A 24 bit address bus allows addresses =16 MB of addresses.
224
Each address contains 16 bits =2 bytes. Therefore, maximum
memory size =16MB*2bytes= 32 MB
9
BASIC COMPUTER SYSTEM
The data bus is used to transfer the actual data to and from
the locations (basic functional units). It is a two-way bus as
data may be going to the processor (Read) or coming from
the processor (Write).
The control bus consists of a set of control signals.
instruction).
This process is often referred to as the fetch-decode-execute
cycle, or simply the execution cycle.
13
BASIC COMPUTER SYSTEM
To execute an instruction, the processor contains hardware
consisting of control circuitry and an arithmetic and logic unit
(ALU).
The control circuitry is needed to provide timing controls as well as
to instruct the internal hardware components to perform a specific
operation.
The ALU is mainly responsible for performing arithmetic
operations (such as add, divide) and logical operations (such as
and, or) on data.
In practice, instructions and data are not fetched, most of the time,
from the main memory.
There is a high-speed cache memory that provides faster access to
instructions and data than the main memory.
For example, the Pentium provides a 16 KB on-chip cache. This is
14
divided equally into data cache and instruction cache.
The presence of the on-chip cache is transparent to application
BASIC COMPUTER SYSTEM
The system clock provides a timing signal to synchronize the
operations of the system.
A clock is a sequence of 1’s and 0’s. The clock frequency is
measured in the number of cycles per second.
This number is referred to as Hertz (Hz). We often use the
abbreviations MHz and GHz to represent 10^6 and 10^9
cycles per second, respectively.
operates.
BASIC COMPUTER SYSTEM
The clock period is defined as the length of time taken by one
clock cycle.
20
2.3 CPU FAMILIES USED IN MICROCONTROLLERS
21
2.3 CPU FAMILIES
supportive devices used in microcontrollers are
Timers
A/D converter
Serial I/O and
Common communication lines (System Bus).
22
Figure 2.5 Structure of a typical microcontroller
2.3 CPU FAMILIES
The Central Processing Unit (CPU) in a microcontroller is
typically a microprocessor unit (MPU) or core.
The CPU is where instructions become signals and hardware
actions that command the microcomputer operation.
The minimal list of components that define the architecture of
a CPU include the following:
Hardware Components:
An Arithmetic Logic Unit (ALU)
A Control Unit (CU)
A Set of Registers
Bus Interface Logic (BIL)
Software Components:
Instruction Set
Addressing Modes
23
The instructions and addressing modes will be defined by the
specifics of the hardware ALU and CU units.
2.3 CPU FAMILIES
The collection of hardware components within the CPU
performing data operations is called the processor’s datapath.
The hardware components performing system control
operations are designated Control Path.
The control unit is at the heart of the CPU control path.
24
27
ii. MSP430X (CPUX) FAMILIES
28
Figure 2.7 MSP430X (CPUX) Block Diagram (Courtesy of Texas Instruments, Inc.)
2.4 BASIC I/O DEVICES AND TECHNOLOGIES
Input/output (I/O) devices provide the means by which a computer
system can interact with the outside world.
An I/O device can be purely an input device (e.g., keyboard, mouse),
purely an output device (e.g., printer, display screen), or both an input
and output device (e.g., disks).
Computers use I/O devices (also called peripheral devices) for two
major purposes—to communicate with the outside world, and to store
data.
There is usually an I/O controller that acts as an interface between the
system and the I/O device.
There are two main reasons for using an I/O controller.
First, different I/O devices exhibit different characteristics and, if these
devices were connected directly, the processor would have to understand and
respond appropriately to each I/O device.
The second reason for using an I/O controller is that the amount of electrical
power used to send signals on the system bus is very low. This means that the 29
cable connecting the I/O device has to be very short (a few centimeters at
most).
2.4 BASIC I/O DEVICES AND TECHNOLOGIES
I/O controllers typically contain driver hardware to send
current over long cables that connect the I/O devices.
I/O controllers typically have three types of internal registers
—a data register, a command register, and a status register.
let us consider printing a character on the printer. Before the
processor sends a character to be printed, it has to first check
the status register of the associated I/O controller to see
whether the printer is online/offline, busy or idle, out of
paper, and so on.
In the status register, three bits can be used to provide this
information.
30
2.4 BASIC I/O DEVICES AND TECHNOLOGIES
For example, bit 4 can be used to indicate whether the printer
is online (1) or offline (0), bit 7 can be used for busy (1) or
not busy (0) status indication, and bit 5 can be used for out of
paper (1) or not (0).
The following summarizes the sequence of actions involved
in sending a character to the printer:
Wait for the controller to finish the last command;
Place a character to be printed in the data register;
Set the command register to initiate the transfer.
The processor accesses the internal registers of an I/O
controller through what are known as I/O ports.
31
2.4 BASIC I/O DEVICES AND TECHNOLOGIES
An I/O port is simply the address of a register associated with
an I/O controller.
There are two ways of mapping I/O ports. Some processors
such as the million instructions per second (MIPS) map I/O
ports to memory addresses. This is called memory-mapped
I/O.
Other processors like the Pentium have an I/O address space
that is separate from the memory address space. This
technique is called isolated I/O.
In these systems, to access the I/O address space, special I/O
instructions are needed. Pentium provides two instructions—
in and out—to access I/O ports. The instruction can be used
to read from an I/O port and the out for writing to an I/O
port. 32
2.5 INTERRUPTS AND MEMORIES
The topic of interrupts and resets involve both hardware and
software subjects, but it is also closely related to how a CPU
operates.
For this reason it becomes important to provide an
introductory discussion on the subject at this point
The CPU in a microcontroller or microprocessor is a
sequential machine.
The control unit sequentially fetches, decodes, and executes
instructions from the program memory according to the
stored program.
This implies that a single CPU can only be executing one
instruction at any given time.
33
2.5 INTERRUPTS AND MEMORIES
When it comes to a CPU servicing its peripherals, the
program execution needs to be taken to the specific set of
instructions that perform the task associated to servicing each
device.
When a CPU services a device, one of two different
approaches can be followed: service by polling or service by
interrupts. Let’s look at each approach with some detail.
In service by polling, the CPU continuously interrogates or
polls the status of the device to determine if service is
needed.
When the service conditions are identified, the device is
served. This action can be exemplified with a hypothetical
case of real life where you will act like a polling CPU:
34
2.5 INTERRUPTS AND MEMORIES
Assume you are given the task of answering a phone to take
messages. You don’t know when calls will arrive, and you are
given a phone that has no ringer, no vibrator, or any other
means of knowing that a call has arrived. Your only choice for
not missing a single call is by periodically picking-up the phone,
placing it to your ear and asking “Hello! Anybody there?”
hoping someone will be in the other side of the line. This would
be quite an annoying job, particularly if you had other things to
do. However, if you don’t want to miss a single call you’ll have
to put everything else aside and devote yourself to continuously
perform the polling sequence: pick-up the phone, bring it to your
ear, and hope someone is on the line. Since the line must be
available for calls to enter (sorry, no call-waiting service), you
have to hang-up and repeat the sequence over and over to catch
every incoming call and taking the messages. What a waste of 35
time! Well, that is polling.
2.5 INTERRUPTS AND MEMORIES
When a peripheral is served by interrupts, it sends a signal to
the CPU notifying of its need. This signal is called an interrupt
request or IRQ.
We can bring this interrupting capability to our phone example
above.
Let’s assume that in this case your phone has a ringer. While
expecting to receive incoming calls, now you can rely on the
ringer to let you know that a call has arrived. In the mean time,
while you wait for calls to arrive you are free to perform other
tasks. You could even get a nap if there were nothing else to do.
When a call arrives, the ringer sounds and you suspend
whatever task you are doing to pick-up the phone, now with the
certainty that a caller is in the other end of the line to take his
or her message. The ring sound acts like an interrupt request to
36
you. A much more efficient way to take the messages.
2.5 INTERRUPTS AND MEMORIES
Interrupts can be used to serve different tasks. The following
are just a few simple examples that illustrate the concept:
A system that toggles an LED when a push-button is depressed.
The push-button interface can be configured to trigger an interrupt
request to the CPU when the push-button is depressed, having an
associated ISR that executes the code that turns the LED on or off.
A message arriving at a communication port can have the port
interface configured to trigger an interrupt request to the CPU so
that the ISR executes the program that receives, stores, and/or
interprets the message.
A voltage monitor in a battery operated system might be
interfaced to trigger an interrupt when a low-voltage condition is
detected. The ISR might be programmed to save the system state
and shut it down or to warn the user about the situation.
37
2.5 INTERRUPTS AND MEMORIES
When compared to the alternative of polling, interrupt
servicing allows a much more efficient use of the CPU.
In addition to the CPU usage efficiency advantage, using
interrupt servicing also provides the following advantages:
Compact and modular code: Interrupt service routines (ISR)
induce code modularity and software reusability.
Reduced energy consumption: As ISRs lead to less CPU cycles,
this has a direct impact in the amount of energy consumed by the
application, particular when combined with low-power modes.
Faster response time: When many events and devices need to be
served, well designed ISRs provide a quick response to the
triggering event. Well coordinated priorities among sources will
allow for a quick response with minimal processing overhead.
38
2.5 INTERRUPTS AND MEMORIES
The two types of interrupt requests are maskable and non-
maskable interrupt requests.
A maskable interrupt request is one that can be masked by the
CPU through the global interrupt enable (GIE) flag to ignore
it.
This is done by clearing the GIE in the processor status
register (PSW). This action makes the CPU ignore all
interrupt request from all maskable sources.
The second type is a non-maskable interrupt (NMI) request.
This type of request cannot be masked by the CPU and
therefore always has to be served, even if the GIE flag is clear.
This type of request is reserved only for system critical events
whose service cannot be postponed under any circumstance.
39
One example of such an event could be a low-battery voltage
condition in a portable device.
2.5 INTERRUPTS AND MEMORIES
Memory technologies in embedded systems provide the fundamental
functions of storing programs and data, like in any other computing
system. In embedded applications, however, the requirements of storage
have traditionally being different in the sense of size and speed.
Embedded memory can be classified according to two main criteria:
storage permanence and write ability.
The first criterion refers to the ability of a programmed memory to hold its
contents, while write ability, refers to how easily the memory contents can be
modified.
Looking at all the memory technologies that have reached mainstream
(ROM, PROM, EPROM, EEPROM, FLASH, and RAM), masked, read-
only memory (ROM) earns the mark as the least writable and most
permanent of all types.
The zeros and ones defining a ROM contents are hardcoded in the chip
through the metallization layer in their fabrication process. This
characteristic also makes the memory non-volatile.
Recall that volatility in memory refers to the loss of contents when the 40
chip is de-energized.
2.5 INTERRUPTS AND MEMORIES
While non-volatile memory technologies evolved, volatile
memory has also seen improvement in different aspects.
Non-volatile technologies moved from magnetic core-based to
semiconductor-based technologies.
Two types of volatile memory are predominant now-a-day: static
and dynamic cells.
Because of historical reasons, volatile, read-write memory has
been called RAM.
Two important parameters associated to RAM technologies are
their reading and writing speeds, quantified as the memory
access time.
Static RAM(SRAM)is the fastest, but dynamic RAM (DRAM) is
preferred for large memory arrays because of its density.
Newer technologies like Ferroelectric RAM(FRAM) are 41
beginning to show up in the market.
2.5 INTERRUPTS AND MEMORIES
Flash is a type of EEPROM differentiated by its own name
because it may be written and read in blocks.
Other EEPROM’s need to be completely erased byte by byte, or
by very small blocks in some cases, making the process slow
When flash memory operates in read mode, it works like any
ROM.
Flash memory offers fast read access times, as fast as dynamic
RAM, although not as fast as static RAM.
The other modes of operation are erase and program. In the first
case, the cells are taken to an “erased state”, which may be a
logic 0 or logic 1, depending on the hardware configuration.
A flash memory controller, or flash controller, manages the data
stored on flash memory and communicates with the CPU,
computer or whatever electronic device the memory is serving.
42