สถาปัตยกรรมไมโครคอนโทรลเลอร์

GPIO, timers, interrupts, ADC, PWM and the memory map

Last updated:

Why it matters

A microcontroller (MCU) is a complete small computer on one chip: a processor, memory and the circuits that connect it to the outside world. A drone carries several. The flight controller has a fast MCU, every ESC has its own, and GNSS receivers and some smart batteries have one too. Understanding what is inside an MCU lets you read its datasheet, pick a board that fits the job, and write code that meets its deadlines.

This page describes the structure common to most MCUs, with examples from the RP2350 on the Raspberry Pi Pico 2 used in the lab, and the STM32H753 in the Pixhawk 6X flight controller.

What is inside

  • CPU core. Executes instructions one after another, paced by the clock. Most drone MCUs use Arm Cortex-M cores: the Pixhawk 6X uses an STM32H753 with a 480 MHz Cortex-M7, while the RP2350 has two Cortex-M33 cores.
  • Flash holds the program and constants and keeps them without power. SRAM holds variables at run time: fast, but lost at power-off.
  • Bus. The data paths that connect the core to memory and peripherals.
  • Peripherals. Dedicated circuits on the chip, such as GPIO, timers, ADCs, communication blocks and DMA (direct memory access), which moves data between peripherals and memory without the CPU. Peripherals run in parallel with the CPU, which is why an MCU of a few hundred megahertz handles real-time work so well.

The memory map

The CPU sees everything as memory locations: program, variables and the control registers of every peripheral. The allocation of address ranges to each part is the memory map, and controlling hardware by reading and writing those addresses is called memory-mapped I/O. The RP2350 datasheet divides the main address ranges as follows.

SegmentBase addressUsed for
ROM0x00000000On-chip boot code; where the cores start after reset
XIP0x10000000Execute-in-place from external flash
SRAM0x20000000Variables and stack
APB peripherals0x40000000UART, ADC, PWM, timer and other registers
AHB peripherals0x50000000Higher-speed peripherals
SIO0xd0000000Fast core-local GPIO access
Cortex-M33 private0xe0000000Core registers such as the interrupt controller

Each chip family has its own map, but the principle is the same: a register's address is a base address plus an offset listed in the datasheet. The SDK gives these values names such as SIO_BASE.

Example 1: driving a GPIO by writing a register

The RP2350 datasheet places SIO at 0xd0000000 and the GPIO_OUT_SET register at offset 0x018, so its address is 0xd0000000 + 0x018 = 0xd0000018. To drive GPIO 15 high, write 1 shifted left by 15 bits, 0x00008000, to that address.

SET and CLR registers change only the bits written as 1 in a single write. There is no read, modify, write-back sequence that an interrupt could split, corrupting another pin's bit.

GPIO, timers and PWM

GPIO (general-purpose input/output) pins can be configured as digital inputs or outputs. An unconnected input floats and reads random values, so enable a pull-up or pull-down resistor. Most modern MCU pins run at 3.3 V, and overdriving a pin can destroy the chip.

A timer is a counter clocked without CPU involvement. It generates PWM (pulse-width modulation), a pulse train of adjustable width. Following ST's timer cookbook (AN4776), the PWM frequency is fPWM = fTIM / ((PSC + 1)(ARR + 1)), where PSC is the clock prescaler and ARR the auto-reload value at which the count restarts. A compare value, CCR, sets the pulse width.

A trade-off worth knowing: higher PWM frequency costs resolution. With a 150 MHz clock, no prescaling (PSC = 0) and 20 kHz PWM, the counter has only 150,000,000 / 20,000 = 7,500 steps per period, about 12.9 bits. A 20 ms servo period, by contrast, needs prescaling, because 3,000,000 steps overflow a 16-bit counter.

The ADC

An ADC (analogue-to-digital converter) turns a voltage into an integer. An N-bit ADC divides 0 to the reference voltage Vref into 2N steps, so one step is LSB = Vref / 2N. A 12-bit ADC at 3.3 V resolves about 0.806 mV per step. Resolution is not accuracy: readings still carry errors from the reference, noise and the external circuit.

Speed is a limit too. The RP2350 datasheet states that the ADC takes 96 cycles of a 48 MHz clock per sample, 2 µs, or 500,000 samples per second. Scanning four channels in turn gives each 125,000 samples per second.

Interrupts and DMA

An interrupt lets hardware pause the main program to run an interrupt service routine (ISR) when an event occurs: a pin changes, a timer wraps, or a UART receives data. Cortex-M cores have an interrupt controller with configurable priority, so a more urgent interrupt can pre-empt a less urgent ISR. Keep ISRs short: set a flag and let the main program do the heavy work. Variables shared between an ISR and the main program must be declared volatile, as Arm's compiler guide explains, so the compiler does not optimise away the real reads.

Example 2: reading the ADC by interrupt or by DMA

An assumed MCU runs at 150 MHz and must read the ADC at 500,000 samples per second. Suppose one interrupt per sample, with the ISR including entry and exit taking 200 clock cycles (assumed).

  • Time per ISR = 200 / 150,000,000 ≈ 1.33 µs
  • CPU share = 1.33 µs × 500,000 ≈ 67%, leaving only a third for everything else

If DMA fills a 1,024-sample buffer and interrupts only when it is full, there are about 488 interrupts per second, using about 0.07% of the CPU. Continuous data streams, such as high-rate sensor reads or bulk UART reception, belong on DMA.

Common pitfalls

  • Overdriving a pin, such as a 5 V signal into a 3.3 V input, or battery voltage into an ADC pin with no divider.
  • Read-modify-write on registers that an interrupt also changes, instead of using SET and CLR registers or briefly masking interrupts.
  • Doing heavy work in an ISR, such as printing or waiting for data.
  • Forgetting volatile on variables an ISR modifies.
  • Setting ARR beyond the counter size, or not checking how the timer clock is already divided.
  • Choosing an MCU on core speed alone without checking the number and type of peripherals the job needs.

Continue in the lessons

Key references

  1. White, E. (2024). Making embedded systems: Design patterns for great software (2nd ed.). O'Reilly Media.
  2. Raspberry Pi Ltd. RP2350 datasheet. link
  3. STMicroelectronics. (2026). How to use general-purpose timer peripheral on STM32 MCUs (Application note AN4776, Rev 4; formerly the General-purpose timer cookbook for STM32 microcontrollers). link
  4. Arm. Effect of the volatile keyword on compiler optimization. Arm Compiler for Embedded User Guide (version 6.24). link
  5. PX4 Autopilot. Holybro Pixhawk 6X. PX4 user guide. link

Used in courses

How to cite this page

Drone Institute, Rangsit University. (2026). สถาปัตยกรรมไมโครคอนโทรลเลอร์. In UAS Technology Knowledge Hub (B.Tech.). https://rsu-uat.pk-research.work/en/kb/new/mcu-arch/