Electrical Engineering › Section X · Processors and integrated circuit technologies › Topic X.3
Microcontrollers, embedded systems, and systems on chip
Most processors never sit in a computer case: they are hidden in washing machines, cars, chargers and phones. Microcontrollers and systems on chip put a whole computer, plus the circuits that touch the real world, onto one piece of silicon.
From microprocessor to microcontroller
A general-purpose microprocessor is only the CPU: to sense and control anything it needs separate chips for memory, timers, converters and communication. A microcontroller (MCU) puts a processor core, memory and those peripherals in one package. It is the heart of an embedded system, a product built to do one set of tasks with its own hardware and firmware, the software stored permanently inside it. Embedded systems far outnumber personal computers: a microwave oven, a thermostat and each of the dozens of control units in a modern car contain at least one microcontroller.
Inside a microcontroller
A typical microcontroller contains a processor core; nonvolatile flash memory for the program and settings that must survive power-off; SRAM for variables, the stack and buffers; peripherals such as timers, analog-to-digital and digital-to-analog converters, communication controllers and general-purpose input/output (GPIO) pins; internal buses joining core, memory and peripherals; and interface circuits to the outside. Larger parts add a phase-locked loop (PLL) that multiplies a slow crystal frequency up to the core clock, DMA controllers, low-power sleep modes and a debug port, such as JTAG or Arm's two-wire SWD. Pin counts run from six to several hundred.
Microcontroller families today
Several families dominate, and the market changes quickly, so treat these as examples. Arm Cortex-M cores, licensed to many makers, power families such as STMicroelectronics' STM32 and the Raspberry Pi RP2040, which has two Cortex-M0+ cores. Older 8-bit families remain popular for simple, low-cost jobs: Microchip's AVR, including the ATmega328P of the classic Arduino Uno, and PIC. Espressif's ESP32 family adds Wi-Fi and Bluetooth radios; its first parts used Xtensa cores, and many newer ones use RISC-V, the open instruction set now spreading through microcontrollers.
Configuring peripherals with registers
Firmware controls each peripheral through its registers, which are memory-mapped: they sit at fixed addresses, so ordinary reads and writes reach them. Control registers set how the peripheral works, such as a serial port's data rate, or whether it signals by interrupt or is polled. Status registers report events and errors, such as an ADC's end-of-conversion bit. Data registers hold values to send, received values or a timer's count. A GPIO port often needs just two: a direction register choosing input or output for each pin, and a data register holding the pin levels. Vendor tools can generate set-up code, but it should still be checked against the manual.
Worked example: setting bits in a port register
An LED is wired to pin 5 of a GPIO port. In C, GPIO_DIR and GPIO_OUT name the port's direction and output registers (real names vary by chip), and bit n of each controls pin n. 1. The mask for pin 5 is 1u << 5, which is \(0010\,0000_2 = 20_{16}\). 2. GPIO_DIR |= (1u << 5); makes pin 5 an output without disturbing the others: if GPIO_DIR held \(0\text{F}_{16}\), it now holds \(2\text{F}_{16}\). 3. GPIO_OUT |= (1u << 5); drives the pin HIGH. 4. GPIO_OUT &= ~(1u << 5); drives it LOW, because the inverted mask has a 0 only in bit 5. 5. GPIO_OUT ^= (1u << 5); toggles it. Check: ORing \(2\text{F}_{16}\) with \(20_{16}\) again changes nothing.
Pins with many jobs
To keep packages small and cheap, most pins are multifunctional: one pin might serve as a GPIO line, a UART transmit line, an SPI clock or an address line for external memory, but only one of these at a time. A data sheet listing four UARTs, two SPI ports and 80 GPIO lines does not mean all can be used together. The designer fixes each pin's function, which is why makers repeat popular peripherals on different pins; when two needed functions clash, the answer is the alternative pins, an external chip or another part.
Timers, PWM and encoders
A timer is a counter clocked from the system clock, often through a prescaler that divides it first. It can mark intervals, time the gap between events, or generate pulse-width modulation (PWM): a pin is HIGH while the count is below a compare value and LOW for the rest of each period, so the compare value sets the duty cycle. A slow load, such as a motor or an LED seen by the eye, responds to the average: with LOW at 0 V, the duty cycle times the HIGH voltage. A quadrature encoder peripheral reads two signals from a shaft sensor that step through a Gray code, 00, 01, 11, 10 in one direction and the reverse in the other, counting up or down to track position and speed.
Worked example: a 1 kHz PWM signal
A microcontroller runs at 16 MHz and its 3.3 V output is to drive an LED with 1 kHz PWM at 30% duty cycle. Assume the timer counts from 0 up to a top value and repeats. 1. A prescaler of 64 gives timer ticks at \(16\,\text{MHz} \div 64 = 250\,\text{kHz}\), 4 µs each. 2. One 1 ms period needs \(250\,\text{kHz} \div 1\,\text{kHz} = 250\) ticks, so the top value is 249, because counting starts at 0. 3. A 30% duty cycle needs \(0.30 \times 250 = 75\) HIGH ticks, so the compare value is 75. 4. The average output is \(0.30 \times 3.3\,\text{V} \approx 0.99\,\text{V}\). Check: 75 ticks of 4 µs is 0.3 ms, 30% of 1 ms.
Embedded systems at work
In a programmable calculator, firmware in flash runs the functions, SRAM holds the stack and user data, GPIO lines scan the keypad, an LCD-driver peripheral runs the display and a timer switches it off when unused. A single-axis robot arm uses PWM to drive its motor, a quadrature encoder to count position, GPIO inputs from end-stop sensors and, at power-up, a homing move that sets the count to zero. Car controllers must also survive heat, vibration and electrical noise. Many systems use a watchdog timer, which resets the chip if the firmware stops checking in.
Systems on chip
A system on chip (SoC) integrates nearly a whole electronic system on one die: one or more CPU cores, usually Arm-based in phones and tablets, often with a graphics processor (GPU), a digital signal processor (DSP) and, in recent phones, a neural processing unit for machine learning; caches and a memory controller for external DRAM; USB, display and camera interfaces; radios such as Wi-Fi, Bluetooth and cellular; data converters; clock generators; and power management. Older PCs split such tasks between northbridge and southbridge chips. A microcontroller is often called a small SoC. Integration gives smaller size, lower power and, in high volume, lower cost.
Common mistakes to avoid
First, treating microprocessor and microcontroller as the same thing: a microcontroller adds memory and peripherals around the core. Second, writing a whole register when changing one pin: GPIO_DIR = (1u << 5); also clears every other bit, while |= changes only bit 5. Third, assuming every peripheral on the data sheet can be used together, when pins are shared. Fourth, an off-by-one top value: 250 ticks per period means a top value of 249 when counting starts at 0. Fifth, forgetting that PWM gives a correct average only for loads slow enough to smooth it.
Key takeaways
A microcontroller combines a processor core, flash, SRAM, timers, converters, communication controllers and GPIO on one chip, and runs an embedded system's firmware. Firmware configures peripherals through memory-mapped control, status and data registers, using masks with |=, &= ~ and ^= to change single bits. Pins are multifunctional. Timers with prescalers and compare values produce PWM, whose average is the duty cycle times the HIGH level. A system on chip adds GPUs, DSPs, radios, memory controllers and power management to integrate a whole system.
Flashcards
Flashcard
Processor core, memory and peripherals in one package
Test your understanding
Question 1 of 5
What distinguishes a microcontroller from a general-purpose microprocessor?
Question 2 of 5
GPIO_OUT holds 6C₁₆. What does it hold after GPIO_OUT &= ~(1u << 2); runs?
Question 3 of 5
A PWM output switches between 0 V and 5 V with a 25% duty cycle and drives a slow motor. What average voltage does the motor respond to?
Question 4 of 5
A timer is clocked at 2 MHz and counts from 0 up to a top value, then repeats. Which top value gives a 1 kHz PWM period?
Question 5 of 5
A data sheet lists four UARTs, two SPI ports and 80 GPIO lines for a microcontroller. What should a designer conclude?
