Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 57 ยท Toward a Processor

Microcontrollers and Embedded Digital Systems

Learn what a microcontroller contains, how Harvard and von Neumann designs differ, how microcontrollers are classified, and how timers, ADC and PWM work, with Python models.

A tiny CPU built from registers, an ALU and a state machine can run programs. Put that CPU on one chip together with its memory and a set of ready-made interface circuits, and you have a microcontroller (MCU): a complete small computer on a single piece of silicon. Microcontrollers run washing machines, car engines, thermostats, keyboards, toys, drones and the smart bulbs in your home. There are billions more of them than there are laptops.

This lesson explains what is inside one, how to tell the main kinds apart, and how the digital building blocks from this course (counters, comparators, registers, converters) show up as MCU peripherals.

Microprocessor versus microcontroller

A microprocessor is only the CPU. To make a working computer you add RAM, storage and interface chips on a board. A microcontroller puts everything on one chip:

Part Microprocessor system Microcontroller
CPU the chip itself on the chip
RAM and program storage external chips on the chip
I/O interfaces external chips on the chip
Typical clock GHz 1 MHz to a few hundred MHz
Typical RAM gigabytes bytes to a few megabytes
Cost and power higher cents to a few dollars, microwatts to milliwatts when asleep
Typical job a general-purpose computer one dedicated task, often real-time

What is inside

  • CPU core: registers, ALU and control unit, like the datapath you built in ALU Design.
  • Program memory: usually flash (electrically erasable, keeps its contents without power). Older and cheaper parts used mask ROM, OTP-PROM or EPROM.
  • Data memory: SRAM for variables and the stack, sometimes with a small EEPROM for settings that must survive power-off.
  • GPIO (general-purpose input/output) pins that can be read or driven as 0 or 1, often with selectable pull-up resistors.
  • Timers and counters: counters built from flip-flops that count clock ticks to measure time, generate delays or count external pulses.
  • ADC and DAC: converters that read analog voltages (temperature, light, battery) and sometimes produce them.
  • Serial interfaces: UART, SPI and I2C for talking to sensors, displays and other chips.
  • Interrupt controller: lets a hardware event (a button, a timer overflow, a received byte) pause the main program, run a short handler, and return.
  • Watchdog timer: restarts the chip if the software stops responding.
  • Clock and reset circuits: an on-chip oscillator, optional crystal, and brown-out detection.
flowchart LR
    CPU["CPU core<br/>(registers, ALU, control)"] <--> BUS["Internal bus"]
    BUS <--> FLASH["Flash<br/>(program)"]
    BUS <--> RAM["SRAM<br/>(data)"]
    BUS <--> PER["Peripherals:<br/>GPIO, timers,<br/>ADC, UART, SPI, I2C"]
    PER --> PINS["Pins to the outside world"]

Everything shares one chip and an internal bus. The pins connect the peripherals to the outside world.

Von Neumann and Harvard architectures

In a von Neumann machine, instructions and data share one memory and one bus. The CPU cannot fetch an instruction and read data at the same moment. In a Harvard machine, program memory and data memory are separate, each with its own bus, so the CPU can do both at once. Most microcontrollers are Harvard (or modified Harvard) designs: it is faster and it keeps the program in non-writable flash apart from the data in RAM.

Classifying microcontrollers

There are several ways to classify them, and exam questions use all of them.

  • By data width: 8-bit (classic, simple, very cheap), 16-bit (middle ground), 32-bit (the current mainstream; the ARM Cortex-M family is the best-known example).
  • By memory arrangement: an embedded-memory MCU has program and data memory on the chip; an external-memory MCU needs extra chips for memory it cannot hold itself.
  • By instruction set: CISC parts have many complex instructions; RISC parts have a small, regular set that executes quickly.
  • By memory architecture: Harvard or von Neumann, as above.
  • By programmability of storage: mask ROM (fixed at the factory), OTP (one time), EPROM or EEPROM, and flash (reprogrammable thousands of times).

Timers: counters with a purpose

A timer is a counter driven by the clock, usually through a prescaler (a divide-by-N counter) so that it counts more slowly. When the count reaches a programmed compare value, it raises a flag or an interrupt and restarts. The timing formulas are the counter formulas from Counter Design and Modulus:

Text
timer tick frequency = clock / prescaler
overflow frequency   = clock / (prescaler x 2^bits)
compare value for target frequency f:  OCR = clock / (prescaler x f) - 1
Python Timer arithmetic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a 1 Hz tick on a 16 MHz clock, prescalers 1 to 64 need a count above 65,535, which does not fit in a 16-bit timer; prescalers 256 and 1024 do, with 62,499 and 15,624. The designer picks the smallest prescaler that fits, to keep the resolution fine.

ADC and PWM: crossing between analog and digital

An N-bit ADC reports round(Vin / Vref x (2^N - 1)) (the exact rounding rule varies by part). With a 10-bit ADC and a 5 V reference each step is 5 / 1023, about 4.9 mV.

An MCU rarely has a true analog output. Instead it uses PWM (pulse-width modulation): a fast square wave whose duty cycle (fraction of time at 1) sets the average voltage: V_avg = duty x Vcc. A small RC filter turns the pulses into a steady voltage. PWM controls LED brightness, motor speed and servos. The PWM period and duty both come from the timer's counter and compare register: the output is 1 while the counter is below the compare value.

Python ADC reading and PWM duty

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common issues in microcontroller design

  • Timing: the program must meet deadlines. A late response to an interrupt can mean a missed sensor reading or a glitch in a motor.
  • Power: battery-powered devices live in sleep modes and wake only when needed. Clock speed, supply voltage and peripherals all cost current.
  • Heat: heavy use, or driving big loads through the pins, warms the chip and can limit speed or reliability.
  • Noise: switching loads inject noise into supply and signal lines; decoupling capacitors and good layout are part of the design.
  • Code size and bugs: memory is tiny, there is often no operating system, and a crash may mean the device is unreachable. The watchdog is the safety net.
  • Security: connected devices need protected firmware, secure boot and update signing.
  • Compatibility: different families have different instruction sets, peripherals and toolchains, so moving a design between them costs effort.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main difference between a microprocessor and a microcontroller?

    A microcontroller is a whole small computer on one chip: CPU, program and data memory, and peripherals.
  2. 2. In a Harvard architecture the CPU:

    Separate program and data memories allow an instruction fetch and a data access at the same time.
  3. 3. A 16 MHz clock goes through a prescaler of 256. What is the timer tick frequency?

    16,000,000 / 256 = 62,500 Hz.
  4. 4. A PWM signal at 5 V with 25% duty cycle has an average voltage of:

    Average voltage is duty cycle times supply: 0.25 x 5 V = 1.25 V.
  5. 5. Which peripheral restarts the chip if the software stops responding?

    The software must keep resetting the watchdog; if it fails to, the watchdog forces a reset.

Summary

  • A microcontroller is a complete small computer on one chip: CPU, flash, SRAM, GPIO, timers, ADC, serial ports, interrupts and a watchdog.
  • Most use a Harvard architecture; they are classified by bit width, memory arrangement, instruction set and memory technology.
  • Timers are prescaled counters with compare registers; ADCs and PWM bridge analog and digital worlds.
  • Real designs must balance timing, power, heat, noise, code size, security and compatibility.

Next: Practice: Combinational Circuits.