Catto / Topic Map · Digital Electronics IV Year 7
Digital Electronics IV · 120 h · Topic 1 of 6

Other microcontroller families

Learning one microcontroller is useful; understanding what all of them have in common is far more useful. This topic moves beyond the family you started with and compares architectures, resources and tools, so that you can choose the right chip for each problem and migrate without starting from scratch.

Architectures ARM Cortex-M Selection Migration

Where we start from

The basic architecture, the control registers, memory and I/O ports are covered in the microcontroller family, and the internal peripherals in microcontroller peripherals. Here we compare across families.

01Architecture decisions

DecisionWhat it implies
Harvard or von NeumannHarvard has separate buses for program and data: it can fetch an instruction and a data item in the same cycle. Von Neumann uses a single bus, simpler and more flexible. Almost all microcontrollers use variants of Harvard.
RISC or CISCRISC: few instructions, almost all of them single-cycle, and a compiler that combines them. It is what AVR, PIC and ARM use. CISC survives mostly in the PC world.
8, 16 or 32 bitsThe width of the data path. With 8 bits, adding two 32-bit variables takes several instructions; with 32 bits, one. It is not just speed: it also changes how much memory can be addressed.
Registers or accumulatorAVRs and ARMs have banks of general-purpose registers; classic PICs work almost everything against a single accumulator, which forces more data moves.
PipelineOverlaps the fetch of one instruction with the execution of the previous one. It raises performance and complicates exact cycle counting, which sometimes matters in fine-grained control.
32 bits no longer means expensive

For years, choosing 8 bits was the economical decision. Today an ARM Cortex-M0+ costs the same or less than an equivalent 8-bit part, with more memory and better peripherals. The reason to keep using 8 bits is usually something else: simplicity, very low sleep current, availability, and the fact that the team already knows it.

02Comparative overview

1101001000AVRATmega328PPIC16F877ASTM32F103ESP32RP2040AVR ATmega328P32 kB flash2 kB RAM16 MHz8-bit. Classic Arduino boards: huge documentation and predictable behavior.PIC 16F877A14 kB flash0.36 kB RAM20 MHz8-bit. Decades of industry behind it and very good analog peripherals.STM32 F103128 kB flash20 kB RAM72 MHz32-bit ARM Cortex-M3: the current industrial standard.ESP324096 kB flash520 kB RAM240 MHzDual core with built-in WiFi and Bluetooth. The choice for any connected project.RP20402048 kB flash264 kB RAM133 MHzDual Cortex-M0+ with PIO blocks that generate protocols in hardware.flashRAMclocklogarithmic scale: each line is ten times more
Figure 1. Resources of five common families, animated. The bars are on a logarithmic scale: between the classic 8-bit part and an ESP32 there are two orders of magnitude of difference in memory and speed.
FamilyCoreClockFlash / RAMDistinguishing feature
AVR (ATmega328P)8-bit RISC16 MHz32 kB / 2 kBThe one on classic Arduino boards. Huge documentation and very predictable behavior.
PIC (16F877A)8-bit RISC20 MHz14 kB / 368 BDecades of industry behind it. Very robust, with good analog peripherals.
ARM Cortex-M (STM32F103)32-bit72 MHz128 kB / 20 kBToday's industrial standard. Many manufacturers, one common core and shared tools.
ESP3232-bit, dual core240 MHz4 MB / 520 kBHas WiFi and Bluetooth built in. It is the natural choice for any connected project.
RP204032-bit, dual Cortex-M0+133 MHzexternal / 264 kBIts PIO blocks generate protocols in hardware without occupying the CPU.
What does not change between families

The concepts are the same in all of them: input and output ports with their direction and data registers, timers, interrupts with their vector and their service routine, A/D converters, PWM, serial communication. What changes are the register names and the tools. That is why someone who has understood one family well learns the next in days, not months.

03How to choose

CriterionWhat to ask
PeripheralsDoes it have the number of A/D channels, PWM outputs, serial ports and timers the project needs? This is what rules out candidates first.
MemoryFlash for the program and RAM for the data, with some margin. Running out of RAM midway through development forces a redesign.
Power consumptionIf it runs on a battery, what matters are the low-power modes and the sleep current, not the running current.
ConnectivityDo you need WiFi, Bluetooth, Ethernet, CAN? Integrating it is cheaper than adding it externally.
PackageA chip available only in a fine-pitch surface-mount package may be impossible to solder in the workshop.
Availability and priceThe best chip in the world is no use if you cannot get it in the country or if it has a year-long lead time.
Tools and communityCompiler, debugger, libraries, examples, and people who have already solved the same problem.
Example · Choosing for a temperature logger

Requirements: 4 analog sensors, logging to a memory card, a real-time clock, a display, sending data over WiFi once an hour, and battery power with a small solar panel.

  • A classic 8-bit part falls short: 2 kB of RAM is not comfortable for handling the card, the display and the buffers, and an external WiFi module would have to be added.
  • An ESP32 solves WiFi, memory and processing power all at once, and its deep sleep brings consumption down to tens of microamps between transmissions.
  • Decision: ESP32, with the caveat that its A/D converter is mediocre. If temperature accuracy matters, an external converter is added over a serial bus.

That last point is the kind of detail that only shows up by reading the datasheet: the decision is not made by brand but by the numbers.

04Migrating from one family to another

Applicationlogic, state machines, calculationsunchangedAbstraction layer (HAL)hal_led(), hal_adc(), hal_espera_ms()same interfaceLayer implementationaccess to the chip registersrewrittenHardwarethe microcontrollerswappedhal_avr.cPORTB |= (1<<PB5);AVRMigrating to AVRonly the two lower layers changehal_stm32.cHAL_GPIO_WritePin(...);STM32Migrating to STM32only the two lower layers changeThe more code stays above the abstraction layer, the less has to be rewritten when changing chip.
Figure 2. Layers of a portable program, animated. The more code stays above the abstraction layer, the less has to be rewritten when changing microcontrollers.
What ports by itself
  • The program logic: state machines, calculations, filtering, decisions.
  • The protocols and data formats.
  • Everything written in standard C that does not touch registers.
What has to be rewritten
  • The configuration of clocks, ports and peripherals.
  • The interrupt service routines and their vectors.
  • Anything that depends on exact cycle counting.
  • Startup code, the vector table and the memory map.
/* Abstraction layer: the application never touches registers. */
/* hal.h — the same interface for any family */
void  hal_init(void);
void  hal_led(uint8_t encendido);
uint16_t hal_adc(uint8_t canal);
void  hal_espera_ms(uint16_t ms);

/* hal_avr.c — implementation for AVR */
void hal_led(uint8_t e) { if (e) PORTB |= (1<<PB5); else PORTB &= ~(1<<PB5); }

/* hal_stm32.c — implementation for STM32, same signature */
void hal_led(uint8_t e) { HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, e ? GPIO_PIN_SET : GPIO_PIN_RESET); }

/* app.c — not a single line changes when migrating */
while (1) { hal_led(1); hal_espera_ms(500); hal_led(0); hal_espera_ms(500); }
The abstraction layer is written from the start

Adding it afterward, to a program where registers are sprinkled everywhere, costs almost as much as rewriting it. Separating what is logic from what is hardware from day one costs nothing and is worth a great deal, even if you never migrate: it makes it possible to test the logic on the computer, without the chip.

05Development tools

ToolWhat it does
Cross compilerRuns on the PC and generates code for the microcontroller. In practice, GCC for AVR, ARM and RISC-V.
Linker and linker scriptDecides where everything goes: code in flash, variables in RAM, stack. It is what changes the most between families.
ProgrammerWrites the binary into the chip's flash: ISP on AVR, ICSP on PIC, SWD on ARM, serial port on ESP32.
DebuggerHalts execution, sets breakpoints and shows variables and registers on the real chip. With SWD, two pins are enough.
Logic analyzerShows what happens on the pins. For debugging serial communication it is more useful than the debugger.
SimulatorLets you test without hardware. It is good for the logic; it never replaces testing on the real circuit.
Most people's “debugger” is an LED

And it works, until it stops working. Learning to use a real debugger —stopping at a line, looking at variables, stepping through the code— completely changes how fast you find problems. An SWD adapter is inexpensive and is the best investment in the workshop after the oscilloscope.

06In the lab

Lab 1 · The same program on two families

Write a program that reads a potentiometer and controls the brightness of an LED by PWM, with an abstraction layer. Implement it on two different families by changing only the layer's file. Measure how many lines had to be touched: it is the concrete demonstration of why layers are separated.

Lab 2 · Comparative datasheet

Choose three microcontrollers and build your own table with: A/D channels and their resolution, number of timers, memory, sleep current, operating voltage, packages and real price in the local market. Justify which one you would choose for a specific project.

Lab 3 · Real debugging

With an SWD adapter or similar, set a breakpoint inside an interrupt service routine, observe the value of the variables and step through the code. Compare the experience with debugging the same problem by turning an LED on and off.

Lab 4 · Power consumption

With a multimeter in series, measure the current of a board in normal operation, at idle and in deep sleep. Calculate the battery life with a given battery in each case. The difference between the three states is usually three orders of magnitude.

07Common mistakes

MistakeConsequence
Choosing the chip out of habitYou end up adding externally what another chip had built in, at higher cost and with less reliability.
Not checking real availabilityThe project gets stuck waiting for a component that cannot be found.
Mixing 5 V and 3.3 VARMs and the ESP32 run at 3.3 V and do not tolerate 5 V on their pins: they get damaged.
Registers scattered throughout the programMigrating or testing becomes impossible without rewriting.
Assuming the internal A/D is goodIt varies enormously between families. If the measurement matters, you have to read the real specification.
Running out of RAMThe program behaves erratically because of stack overflows, and the symptom does not point to the cause.
Debugging only with an LEDHours are lost on problems that a breakpoint solves in minutes.

08Self-assessment

What advantage does the Harvard architecture give in a microcontroller?

Separate buses for program and data: it can fetch the next instruction and access a data item in the same cycle, which raises performance without raising the clock.

Why is a 32-bit core not just “faster”?

Because it also changes how much memory it can address and how many instructions are needed to operate on large variables: adding two 32-bit integers takes one instruction instead of several.

What feature distinguishes the ESP32?

It has built-in WiFi and Bluetooth, in addition to a dual core and plenty of memory. That is what makes it the natural choice for a connected project.

What is the first criterion that rules out candidates when choosing a chip?

The peripherals: if it does not have the number of A/D channels, PWM outputs or serial ports that the project needs, no amount of speed or memory makes up for it.

Which part of the program ports without changes between families?

The logic: state machines, calculations, filtering and protocols, as long as they are written in standard C and do not touch registers directly.

What is a hardware abstraction layer for?

So that the application does not touch registers: only the implementation of the layer is changed when migrating. It also makes it possible to test the logic on the computer, without the chip.

What is a cross compiler?

One that runs on the PC and generates code for a different architecture, that of the microcontroller. In practice, GCC in its versions for AVR, ARM or RISC-V.

What precaution is needed when connecting an ARM or an ESP32 to 5 V logic?

They run at 3.3 V and their pins do not tolerate 5 V: a level shifter or a divider is needed. Connecting them directly damages them.

What is the difference between a programmer and a debugger?

The programmer only writes the program into the flash; the debugger also halts execution, sets breakpoints and shows variables and registers while the chip is running.

Why measure the sleep current and not only the running current?

Because in battery-powered equipment the device spends most of its time asleep: battery life is set by the sleep current, which can be a thousand times lower than the running current.

Development of the topic “The microcontroller family” of Digital Electronics IV (Year 7), based on the “Curriculum Proposal – Second Cycle of the Technical-Vocational Track, Secondary Education – Electronics,” Ministry of Education of the Province of Córdoba, DGETyFP. Back to the Topic Map · catto.ar