You are on page 1of 18

MCU Basic Structure/Operation

Back to top

Introduction to Microcontrollers: 1 of 6

Our purpose here is to present basic concepts for up-and-coming systems engineers. Now that we have
completed our introductory look at electronic circuits and digital circuitry, we are finally ready to begin
looking at the microcontroller unit (MCU) that sits at the core of each system. We start with an introduction to
the MCU’s basic structure and operation. In the next session we will look at the MCU’s peripheral circuitry.
And finally we will try using an MCU in an actual system.

MCU: The Brain That Controls the Hardware


Most modern electronic devices include one or more MCUs. Indeed, MCUs are ubiquitous: they’re essential to
the operation of cell phones; they’re in refrigerators and washers and most other household appliances; they
control flashing lights in children’s toys; and much more. So what is it, exactly, that the MCU is doing in all of
these devices? The answer is simple: It’s controlling the hardware that implements the device’s operation. The
MCU receives inputs from buttons, switches, sensors, and similar components; and controls the peripheral
circuitry—such as motors and displays—in accordance with a preset program that tells it what to do and how
to respond.

Figure 1 shows the structure of a typical MCU. The MCU incorporates a CPU (central processing unit), some
memory, and some circuitry that implements peripheral functionalities. If we wish to anthropomorphize, we
can say that the CPU does the "thinking," the memory stores the relevant information, and the peripheral
functions implement the nervous system―the inputs (seeing, hearing, feeling) and the responses (hand and
foot movements, etc.).

Figure 1: MCU Structure


But when we say that the CPU "thinks," we certainly do not mean to suggest that it has consciousness, or that
it is capable of pursuing an independent line of thought. Indeed, its operation is entirely determined by a
program—an ordered sequence of instructions—stored in memory. The CPU simply reads and executes these
instructions in the predetermined order.

And these instructions themselves are by no means sophisticated—there is no instruction that can tell the
machine to "walk" or to "talk," for example. Instead, a typical instruction might tell the CPU to "read data from
address XXX in memory," or "write data to memory address YYY," or "add" or "multiply" two values, and so
on. But while each instruction is simple, they can be organized into long sequences that can drive many
complicated functionalities.

CPU: The "Thinker"


Figure 2 shows the role of the CPU in an embedded system.

Program Counter (PC)


The program counter (PC) is an internal register that stores the memory address of the next instruction for the
CPU to execute. By default, the PC value is automatically incremented each time an instruction executes. The
PC starts at 0000, so the CPU starts program execution with the instruction at address 0000. As the instruction
executes, the PC automatically advances to 0001. The CPU then executes the instruction at 0001, the PC
advances again, and the process continues, moving sequentially through the program.

Instruction Decoder
The decoder circuitry decodes each instruction read from the memory, and uses the results to drive the MCU’s
arithmetic and operational circuitry. An actual decoder is a somewhat more complicated version of the decoder
circuitry we studied in the session titled "Introduction to Digital Circuits-Part 2". It restores the encoded
instructions to their original, unencoded form.
Figure 2: What the CPU Does

Arithmetic and Logic Unit (ALU)


This circuitry carries out arithmetic and logical operations. Arithmetic operations include addition and
multiplication; logic operations include AND, OR, and bit shifts. The ALU is controlled by the instruction
decoder. In general, the ALU consists of a complex combination of circuits.

CPU Internal Registers


These registers store transient information. General-purpose registers hold results of arithmetic and logical
operations, whereas specialized registers store specific types of information—such as a flag register, which
stores flag values (carry flag, etc.). When the ALU performs an operation, it does not operate directly on
values in memory; instead, the data at the specified memory address is first copied into a general-purpose
register, and the ALU uses the register content for the calculation.

Operation of the CPU


As an illustration of how the CPU works, let’s see how it would execute a simple addition: 3+4. First, the
following program code and data must be saved to memory.

Address Instruction (a binary code value identifying the action to be taken)


0000 Read the value at memory address 0100, and store it in Register 1.
Address Instruction (a binary code value identifying the action to be taken)
0001 Read the value at memory address 0101, and store it in Register 2.
Add the value in Register 2 to the value in Register 1, and save the result in
0002
Register 1.

Address Data
0100 3
0101 4

Step 1: When the CPU starts, it fetches the instruction stored at the address given in the program counter: in
this case, at address 0000. It decodes and then executes this instruction. In this example, the instruction tells
the CPU to get the value in memory address 0100 and write it into Register 1.

 Change of value in Register 1: 0→3


 First instruction executed, so program counter automatically advances to 0001.

Step 2: The CPU fetches the instruction stored at address 0001 (the new value in the program counter), then
decodes and executes it. The program counter is incremented again.

 Register 2: 0→4
 PC: 0001→0002

Step 3: The CPU fetches the instruction stored at address 0002 (the new value in the program counter), then
decodes and executes it. The instruction tells the CPU to add the contents of Registers 1 and 2, and write the
result into Register 1.

 Register 1: 3→7
 PC: 0002→0003

Register 1 now holds the sum of 3 and 4, which is 7. This completes the addition. As you can see, the CPU
executes a program by carrying out an ordered sequence of very simple operations.

Memory: The "Store"


The MCU’s memory is used to store program code and data. There are two main types of memory: ROM and
RAM.

ROM (Read-only Memory)


This memory retains its content even while power is off. This memory is for reading only; it cannot be erased
or overwritten. ROM is typically used to store the start-up program (executed immediately after power-on or
reset), and to store constant values that may be freely accessed by running programs.

Many Renesas MCUs use flash memory in place of ROM. Like ROM, flash memory retains its content even
while power is off. Unlike ROM, this content can be overwritten.
RAM (Random-access Memory)
This memory can be freely rewritten. Its disadvantage is that it loses its content when the power goes off. This
memory is mainly used to store program variables.

Many single-chip MCUs1 use static RAM (SRAM) for their internal RAM. SRAM offers two advantages it
supports faster access, and it does not require periodic refreshment. The disadvantage is that the internal
circuitry is complex, making it difficult to pack large quantities on the chip’s limited space. SRAM is not
suitable for implementing large memory sizes.

The alternative to SRAM is DRAM (dynamic RAM). The simple structure of DRAM allows large quantities to
be mounted in small spaces; typical DRAM sizes are much bigger than typical SRAM sizes. But it is difficult
to form DRAM together with high-speed logic on a single wafer. For this reason, DRAM is generally not used
within single-chip MCUs. Instead, it is typically connected to the chip and treated as peripheral circuitry.

1. An MCU implemented on a single LSI (large scale integration) chip. The chip holds the CPU, some ROM, some RAM,
oscillator circuitry, timer circuitry, serial interfacing, and other components. If the chip also includes the system’s main
peripheral circuitry, it is called a "system LSI."

Why Do We Use MCUs?


Let’s take a quick look at why MCUs are currently used in so many devices. For our example, consider a
circuit that causes a LED lamp to light up when a switch is pressed. Figure 3 shows what the circuit looks like
when no MCU is used. This simple circuit has only three components: the LED, the switch, and a resistor.

Figure 3: A LED Lamp Circuit with No MCU

Figure 4, in contrast, shows the circuit design when an MCU is included.

Clearly, the design is now more complicated. Why spend the extra time and money to develop such a design,
when the other version is so much simpler?

But let’s consider this for a moment! Suppose that we later decide to modify the operation of the circuits
shown above, so that the LED lamp will begin flashing at a certain time after the switch is pressed. For the
circuit with the MCU, all we need to do is to change the program—there is no need to touch the design itself.
For the circuit without the MCU, however, we need to redesign the circuit—adding a timer IC to count the
time, a logic IC and FPGA to implement the logic, and so on.

So the presence of the MCU makes it much easier to change the operation and to add new functionality. That’s
why so many devices now include MCUs—because they make things so much easier.

Figure 4: A LED Lamp Circuit with an MCU

In part 2 of this MCU introduction, we will talk about the MCU’s peripheral circuitry. We look forward to
your continued participation.

Introduction to Microcontrollers Update:


Peripheral Circuitry
Back to top

Introduction to Microcontrollers: 2 of 6

We continue with our presentation of basic technical concepts that must be mastered by all students of
embedded systems engineering. In our previous session, we looked at some basic microcontroller concepts. In
this session, we look at some of the hardware (peripheral circuitry) required to run a microprocessor. In our
next session, we will see how to put a real microcontroller to work.

"Generator" Power Circuitry


In our last session we looked at the basic structure and operation of a microcontroller (MCU). Now let's look at
some of the hardware (peripheral circuitry) required to support the microprocessor. In particular, we will look
at some hardware used in the Renesas RL78 Family (RL78/G14), one of the new-generation general-purpose
MCUs.

An MCU, like any of its various components introduced in Digital Circuits, needs a power supply to drive it.
So it must be connected to an outside battery or other suitable power source. Figure 1 shows the pin
arrangement on a 64-pin RL78 Family (RL78/G14) chip. Pins 13/14 (VSS/EVSS0) and 15/16 (VDD/EVDD0)
are the power pins, which connect as follows:

 Pins 13 (VSS) and 14 (EVSS0) to GND.


 Pins 15 (VDD) and 16 (EVDD0) to the positive terminal of the power supply.

The datasheet (hardware manual) for the RL78 Family (RL78/G14) indicates that the power voltage (VDD)
must be between 1.6 and 5.5 V. This means that the MCU is guaranteed to run when supplied with any voltage
within this range. This voltage range is referred to as the operating voltage, or, in some hardware manuals, as
the recommended operating voltage.
Figure 1: Pin Diagram of a 64-pin RL78/G14 MCU (in the RL78 Family)

Figure 2 shows an example of an actual power-connection configuration of a 64-pin RL78 Family (RL78/G14)
MCU.
 Pin 15 connects to bypass capacitor C1. This bypass prevents malfunctions that might otherwise occur
when a current spike causes the voltage to drop. A typical bypass capacitor is a ceramic capacitor with
capacitance between 0.01 and 0.1 µF.
 The power-supply voltage is stepped down by an internal regulator to the voltage used to drive the
MCU's internal circuitry: that is, to either 1.8 V or 2.1 V. The regulator itself is stabilized by another
capacitor, C2, at pin 12.
Figure 2: Power Circuitry of a 64-Pin RL78 Family (RL78/G14) MCU

"Conductor" Oscillators
As we saw in our third session on digital circuitry basics, sequential circuits operate in sync with the rising
or falling edge of a clock (CK) signal. MCUs consist of sequential circuits, and so they require a CK signal.
This external clock signal is provided by an external oscillator connected to the MCU.

Figure 3: Role of Oscillation Circuitry

Figure 3 shows an example of an external oscillator connected to an RL78 Family (RL78/G14) MCU.
Specifically, a crystal oscillator is connected to pins X1 and X2. The MCU includes two internal clock
oscillators that work in conjunction with the external clock signal.

 The main clock drives the CPU.


 The sub-clock is typically used with peripheral circuits or as a real-time clock.

Because the RL78 Family (RL78/G14) uses a highly precise on-chip oscillator (accurate to within 1%) to drive
its robust set of peripheral circuitry, it can operate without need of an external clock. MCUs driven by internal
clocks are less expensive to design.

Even where an on-chip oscillator is present, however, an external crystal oscillator may be used in cases where
it is necessary to achieve better precision and lower temperature-induced variation; for example, in MCUs used
to control watches and so on.

"Alarm Clock" Reset Circuit


It takes a short time after MCU power-on for the internal circuitry to stabilize. During this interval, the CPU
cannot be expected to perform normally. This problem is resolved by applying a reset signal to the reset pin on
the MCU. Setting the signal to active (LOW) causes the MCU to reset.

The signal into the reset input pin must remain LOW until the power-supply and clock signals stabilize; this
pin must connect internally to that part of the circuitry that needs time to stabilize. Figure 4 shows how this can
be accomplished using a power-on reset circuit (an RC circuit).
The incoming power voltage moves through this circuit's resistor, causing some of the initial current to flow
into the capacitor. As a result, the voltage rise into the reset pin is gradual. The reset condition is cleared when
the rising voltage reaches a predetermined level.

Figure 4: Simple Reset Circuit and Waveform

As you can see in the above figure, there is also a manual reset circuit located next to the power-on reset
circuit. The user can reset the MCU at any time by throwing the manual circuit's reset switch.

On a general-purpose MCU, the reset signal must stay LOW for a predetermined interval. This interval is
specified in the MCU's hardware manual or datasheet. The values for the power-on reset circuit's resistor (R)
and capacitor (C) must be selected accordingly.

Conveniently, the Renesas RL78 Family (RL78/G14) uses its own internal power-on reset circuitry to handle
its resets. The MCU automatically clears the reset when the power input rises to the MCU's operating voltage.

CPU Reset
A reset operation causes the CPU to reinitialize the value of its program counter. (The program counter is the
register that stores the address of the next instruction that the CPU will execute.) Upon reinitializing, the
program counter (PC) will hold the start address of the first program to be run. Either of two methods may be
used for getting this first address: either static start addressing or vector reset addressing.
 In static start addressing mode, the MCU always starts program execution at the same fixed address.
The address itself is different for each MCU model. If the PC value is 0, for example, then program
execution will start with the instruction at address 0.
 In vector reset mode, the CPU starts by reading a pointer value stored at a fixed address (called the
reset vector) in ROM. Specifically, the CPU gets and decodes the pointer value, then places the
resulting value into the program counter. This may seem more complicated than the static method
described above; an important advantage, however, is that it allows the initial program address to be
freely changed.

* This article was revised on March 30, 2015.

In our third installment of this MCU introduction, we will look at the MCU development environment and try
out some real MCU operations. As always, we look forward to your continued participation.

Introduction to Microcontrollers Update:


Peripheral Circuitry Control
Back to top

We continue our review of MCU basics for aspiring embedded systems engineers. In earlier segments we
covered MCU hardware, programming languages, and the development environment. This time we look at the
basics of peripheral circuitry control.

Special Function Registers (SFRs)


MCUs use a variety of internal registers to store values related to status and operations. Typical registers
include the Program Counter, general-purpose registers, and SFRs. An MCU uses some of its SFRs special for
the purpose of controlling peripheral circuitry. For example, it reads SFR values to obtain peripheral data such
as count values, serial-port input, and general input. And it writes to SFRs as necessary to output data to
peripherals and to control peripherals' settings and status.

Control of External Peripheral Circuits


As an example, let’s look at how the MCU uses an SFR to handle output to and input from a specific
peripheral.

1. The MCU writes 0 or 1 into a SFR bit to set output to the peripheral to LOW or HIGH level, which is
connected to the SFR bit.
2. The MCU reads the value of a SFR bit to get the status from the connected peripheral.

In the Figures below, pin A is a general-purpose I/O line that connects to a specific bit (call it bit “k”) in one of
the SFRs (call it SFR “j”).

Let’s first look at how the MCU uses the SFR bit to set the peripheral to either HIGH or LOW level.
 To set to LOW (0V), write 0 into bit k.
 To set to HIGH (5V), write 1 into bit k.

Figure 1: General Purpose I/O Pin; Output Control

Assume, for example, that Pin A is connected to an LED, as shown in Figure 2. To turn the LED on, the MCU
writes a 0 into SFR-j bit-k. To turn the LED off, it writes a 1 into the bit. This very simple design is actually
used by many different types of peripherals. For example, the MCU can use the bit as a switch for turning a
motor ON and OFF. (Since most MCUs cannot output enough current to drive a motor, the pin would typically
connect to a drive circuit incorporating FETs or other transistors). More complex controls can be implemented
by utilizing multiple I/O ports.

Figure 2: LED On/Off Circuit Controlled by General I/O


Next, let’s see how the MCU uses the SFR bit to input the current status of the peripheral. All that is needed is
to read the bit value.

 If the MCU reads 0 from SFR-j bit-k, it knows that the peripheral is inputting a LOW signal (0V) into
Pin A.
 If the MCU reads 1 from SFR-j bit-k, it knows that the peripheral is inputting a HIGH signal (5V) into
Pin A.

Figure 3: General Purpose I/O Pin; Input Control

Figure 4 shows how an external switch can be set up so that MCU can read the switch setting through its SFR.

 When Switch S is OFF, voltage is pulled up by Resistor R, resulting into HIGH input into Pin A. This
sets the value of the SFR bit (SFR-j bit-k) to 1.
 When Switch S is ON, the voltage into Pin A is LOW, and the SFR bit value resets to 0.

MCU can easily determine whether the switch is ON or OFF by reading the SFR bit.
Figure 4: Implementing a Switch through General I/O

Each MCU incorporates numerous SFRs capable of implementing a wide range of functionalities. Programs
can read these SFRs to get information about external conditions, and can write these SFRs to control external
behavior. If an MCU were a human being, its SFRs would be its hands, its feet, and its five senses.

This concludes our introduction to peripheral control basics. In our next and final segment on MCU basics, we
will be looking at interrupts. See you then.

In the concluding session of this MCU Introduction series, we will be looking at interrupts. Interrupt
processing is one of the most important features of MCU programming.

Module List
Introduction to Microcontrollers: Interrupt
Processing
Back to top

Introduction to Microcontrollers: 5 of 6

In this final session of our series covering MCU basics, we look at interrupt processing—one of the core
concepts of MCU programming. We also look at the alternative process of polling.

Interrupts and Polling


This is the fifth and last topic to be covered in this “Introduction to Microcontrollers” series. Part 1 of the
series explained about the MCU basic structure and operation, part 2 covered peripheral circuitry, part 3
covered programming languages and the software development environment, and part 4 looked at the basics of
peripheral circuitry control. Today we look at interrupt processing, a key feature of MCU control.
This is the fifth and last topic to be covered in this “Introduction to Microcontrollers” series. Part 1 of the
series explained about the MCU basic structure and operation, part 2 covered peripheral circuitry, part 3
covered programming languages and the software development environment, and part 4 looked at the basics of
peripheral circuitry control. Today we look at interrupt processing, a key feature of MCU control.

Now assume that you are reading the book and waiting for the delivery, but you don’t have a doorbell and the
delivery person has agreed to quietly drop the package off at your door. (In other words, you won’t be
interrupted.) In this case, you would stop reading from time to time and go to the door to see if the package has
been reached. In the MCU world, this type of periodic checking—the alternative to interrupts—is called
polling.

Interrupt Processing by the MCU


In actuality, interrupt processing in the MCU is just slightly more complicated than the description above. But
it’s still closely analogous to the book-reading example, as evident from the following.
Processing an Interrupt at Home Processing an Interrupt in the MCU

1) You’re reading a book. The main program is running.

2) The delivery person rings bell. An interrupt signal lets the MCU know that an event has oc

3) Stop reading. The MCU receives the interrupt signal, and suspends exec

4) Bookmark your current page. The MCU saves the current program execution state into its

5) Get the delivery. The MCU executes the interrupt routine corresponding to th

6) Go back to the marked page. The MCU restores the saved program execution state.

7) Resume reading from where you left off. Resume program execution.

The above analogy should clarify the general idea. Now let’s look a little more closely at the actual process
within an MCU.

When an event occurs, an interrupt signal is sent to notify the MCU. If the event occurs at an external device,
the signal is sent into the MCU’s INT pin. If the event occurs in the MCU’s on-chip peripheral circuitry such
as a timer increment or a serial I/F event—then the interrupt signal is issued internally.

These interrupt signals are received and processed by the MCU’s Interrupt Controller (IC). If multiple interrupt
signals are received, the IC’s logic decides the order in which they are to be handled (based on each device’s
priority level), and then sends the corresponding interrupt request signals to the CPU in the appropriate order.
(The IC can also be set to ignore, or “mask,” particular interrupts, to block out unwanted interruptions.) When
the CPU receives the request, it suspends current program execution and then loads and runs the interrupt
processing code corresponding to the interrupt.

Real-Time Processing
While interrupts and polling carry out similar processing, there is a notable difference. Where interrupts are
used, the MCU is immediately alerted when an event occurs, and can quickly switch to the requested
processing. This rapid responsiveness is often referred to as real-time processing.

In theory, polling can also be rapid, provided that the polling frequency is very high. In practice, however,
operation is slowed by the need to poll for multiple events, and by the difficulties of the main processing with
a sufficiently short polling loop.

For example, consider the case where an MCU is expecting a user to eventually press a switch. Since the MCU
has no way to predict when this will happen, it must continue looping and polling indefinitely. This idle
looping can consume considerable amounts of CPU processing time. And if it is necessary to poll for a variety
of events, then it becomes increasingly difficult to keep the polling interval short enough for rapid response.

Interrupt processing may be slightly more difficult for programmers to write, since it requires a reasonable
understanding of the MCU’s hardware. But interrupts are an essential feature of MCU programming, and
cannot be sidestepped. Programmers are encouraged to deepen their knowledge of MCU architecture and learn
how to write effective interrupt handlers.

This completes our series on MCU basics. We hope you have found these sessions interesting, and we
encourage you to proceed to some hands-on practice with MCUs.

Module List
 

You might also like