MCU Structure
MCU Structure
An MCU is comprised of I/O, CPU, ROM, RAM, and peripheral circuits.
The integration of these components allows the MCU to operate by receiving inputs, processing them based on predefined instructions, and generating the corresponding output signals.
Here, we will explain the role of each component.
I/O: Receives inputs and sends outputs signals.
CPU: Executes predefined processes.
ROM: Stores software that defines the processing operations.
RAM: Temporarily holds data, such as processing results.
Peripheral Circuits: Additional circuits that enhance the functionality of the MCU.
Understanding MCU Operation using an Electric Kettle as an Example
Consider an electric kettle, where pressing a button triggers the heater to warm the water, of how an MCU functions.
What is I/O?
The I/O (Input/Output) receives input signals from and outputs signals to externally connected components.
Consider the operation of an electric kettle. The kettle receives a button input, triggering the heater to warm the water. In this context, the role of the I/O is to accept button inputs, receive signals from the internal temperature sensor, and output control signals to the heater.
Without I/O, the electric kettle would be unable to receive input from the button or temperature sensor, or output control signals to the heater.
Thus, I/O acts as the bridge between the MCU and external components, making it an essential part of the system.
What is a CPU?
The CPU (Central Processing Unit) serves as the command center of the MCU. In an electric kettle, the CPU receives button input, sends a heating command to activate the heater, and stops the heater when the water temperature reaches the desired level.
For example, to heat water to 90°C, the process can be predefined as follows:
‘If the button is pressed, activate the heater,’ and
‘If the internal temperature sensor detects 90°C, stop the heater.’
By following these instructions, the electric kettle can automatically heat the water to 90°C with a simple button press.
This predefined sequence of operations is called a program, which functions as a structured plan. As the command center, the CPU processes and executes operations according to this program.
Processing (Instructions) Executed by the CPU
A Closer Look at the CPU
The process of ‘If the internal temperature of the electric kettle reaches 90°C, stop the heater’ can be broadly divided into the following three steps.
1. Receive output from the electric kettle’s internal temperature sensor every few seconds.。
2. Compare the obtained temperature with the 90°C threshold.
3. Stop the heater if the temperature exceeds 90°C.
These steps can be further broken down into smaller tasks that the CPU executes at the most granular level. Specifically, steps 2 and 3 which involve comparing the temperature and stopping the heater correspond to comparison and branching instructions in the CPU. In MCU assembly language, these operations might be written as:
・ CMP Kettle Internal Temperature, 90°C → Compare the kettle's internal temperature to 90°C
・ BGE Heater Off → Branch to stop the heater if the temperature is 90°C or higher.
Although the specific assembly language instructions will vary depending on the MCU, many follow a standard naming convention, such as CMP (compare) to compare values, MOV (move) to transfer data, ST (store) to save data to memory, and B (branch) to control program flow.
In higher-level programming languages like C, the same logic could be expressed as:
if (kettle temperature >= 90°C) {heater off();}
The CPU executes these instructions sequentially.
Executing Instructions by the MCU
A Deeper Look into CPU Operation
The CPU consists of three main components: a program counter, instruction decoder, and arithmetic unit.
The program counter stores the location (address) of the next instruction to be executed.
The instruction decoder converts the instruction into signals that the CPU can process.
The arithmetic unit processes the instruction.
In the sample diagram, ① the program counter specifies the address where the comparison instruction (CMP) is stored.
② The instruction at the specified address is read and input into the decoder.
③ The instruction is converted into a signal that the CPU can process, then sent to the arithmetic unit.
At this point, the program counter automatically increments, moving to the next location (address).
If a branch instruction B is encountered, the program counter specifies the branch destination instead.
④ The arithmetic unit processes the instruction, performing addition, subtraction, or other operation according to the instruction type. For example, the CMP (compare) instruction executes subtraction to determine which value is greater.
⑤ If a conditional branch instruction is used, the CPU updates the program counter with the appropriate branch destination after processing the comparison result.
These steps are commonly referred to as ② fetch, ③ decode, and ④ execute.
By performing each step in order, the CPU can process sequences of instructions to efficiently achieve the desired operations.
Time Required for the CPU to Execute Instructions
One key parameter that defines CPU performance is the time required to process instructions. Here, we examine the execution time of the CMP (compare) instruction.
Although the time will vary depending on the MCU, instructions are processed in approx. 100us at the slowest. In contrast, high-speed MCUs can execute instructions in just tens of nanoseconds. The primary factor in determining the execution speed of instructions is the clock.
A clock is a signal that regulates the timing of instruction execution, measured in Hertz (Hz).
The higher the clock frequency, the faster instructions can be processed. The CPU executes instructions sequentially, synchronized with this clock signal.
What is ROM?
ROM (Read Only Memory) is a type of non-volatile memory that can only be read and cannot be rewritten.
Just like in an electric kettle,
‘If the button is pressed, activate the heater,’ and
‘If the internal temperature sensor detects 90°C, stop the heater.’
where programs are stored, which is why it is also called program memory.
The contents of ROM are read as soon as the power is turned ON and are retained when power is restarted.
This ensures that appliances like electric kettles operate the same way each time they are turned on.
Modern ROM utilizes flash memory that allows rewriting of its contents when necessary. This increases the flexibility of MCUs, leading to shorter development times and lower costs for electrical devices.
Relationship Between the CPU, Instructions, and ROM
The CPU processes each instruction, which is the smallest unit of operation, one by one. Common instructions include data transfer MOV, memory storage ST, and comparison CMP.
Each of these instructions is individually stored in the ROM.
Let's revisit the example of the electric kettle.
The process of ‘If the internal temperature of the electric kettle reaches 90°C, stop the heater’ can be broadly divided into the following three steps.
1. Receive output from the electric kettle’s internal temperature sensor every few seconds.。
2. Compare the obtained temperature with the 90°C threshold.
3. Stop the heater if the temperature exceeds 90°C.
The following assembly language instructions correspond to steps [2] and [3] in the process:
・ CMP Kettle Internal Temperature, 90°C → Compares the internal temperature of the kettle with 90°C
・ BGE Heater Off → Branches to turn OFF the heater if the temperature is 90°C or higher.
These instructions are stored in ROM and the CPU processes them sequentially.
What is RAM?
RAM (Random Access Memory) is a type of readable and writable memory used for temporary data storage, often referred to as data memory.
In an electric kettle, RAM stores essential operational data, such as the boiling temperature settings, measurement results from the internal temperature sensor, and the current status of the heater.
While RAM is not necessary for basic operations like MOV (data transfer) and ST (data storage) instructions, it becomes essential when using an MCU to develop real-world electrical devices due to the complex processing requirements.
RAM Processing
To understand RAM processing, let's examine the functionality of an electric kettle with adjustable temperature settings.
① Boiling Function
・ When the boil button is pressed while heating is stopped, the boiling process starts.
・ When the internal temperature of the kettle reaches the set (target) temperature, the boiling process stops.
・ If the boil button is pressed while heating, the boiling process stops.
② Temperature Setting Function
・ The temperature can be set to three levels: 75°C, 90°C, and 100°C.
・ Each time the temperature setting button is pressed, the temperature cycles through 100°C⇒90°C⇒75°C⇒100°C⇒90°C and so on.
Since these operations involve storing and updating data dynamically, RAM is required to implement these functions effectively.
● Boiling Function
The boiling function begins heating when the boil button is pressed while boiling is stopped, making it necessary to track whether the kettle is currently boiling or not. To achieve this, the boiling ON/OFF status must be stored as data.
In this case, the boiling status is stored in RAM under the variable HeaterStatus.
When the boil button is pressed, the CPU reads HeaterStatus from RAM, determines the current state, then updates HeaterStatus accordingly.
If HeaterStatus is OFF when the boil button is pressed, current flows to the heater and HeaterStatus is updated to ON. Conversely, if HeaterStatus is ON when the boil button is pressed, the current to the heater stops and HeaterStatus is set to OFF.
Without a writable memory area like RAM, the current state cannot be stored.
● Temperature Setting Function
Since the kettle offers three temperature settings, it is necessary to store the currently selected temperature. In addition, each time the temperature setting button is pressed, the setting cycles through three levels before returning to the original temperature. To implement this function, the number of button presses and the corresponding target temperature are stored as data in RAM.
As demonstrated, using a freely readable and writable area like RAM allows for more flexible processing.
Peripheral Circuits
Peripheral circuits are built-in components within the MCU that enhance functionality. To better understand their role, let's examine the operation of an electric kettle as an example.
‘If the internal temperature of the electric kettle reaches 90°C, stop the heater’
The process can be divided into three key steps.
1. Receive temperature data from the kettle’s internal temperature sensor every few seconds.
2. Compare the obtained temperature with the 90°C threshold.
3. Stop the heater if the temperature exceeds 90°C.
For example, in Step 1. a peripheral circuit that measures time called a timer is required to take periodic measurements.
An A-D (Analog-Digital) converter is also needed to process the analog temperature sensor output. Therefore, an MCU equipped with a timer and A-D converter can successfully execute Step 1, demonstrating how peripheral circuits can perform functions that the CPU, ROM, and RAM alone cannot achieve.