ระบบดิจิทัลและสถาปัตยกรรมคอมพิวเตอร์
Number systems, logic gates, combinational and sequential circuits, memory and CPUs
Overview
Every flight controller is a small computer that works only with 0s and 1s. Accelerometer readings, motor speed commands and telemetry messages are all groups of bits that digital circuits create, store and pass on. If you understand digital systems you can read a chip datasheet, decode raw sensor values yourself, and see why some counters wrap back to zero. This unit climbs from number systems to the CPU in five steps, each built on the one before, like assembling a large model from small bricks.
Number systems and data representation
A bit is a binary digit, 0 or 1. Eight bits make a byte, which holds 256 different values. One hexadecimal digit stands for exactly four bits, so register values are usually written in hex: 0xC8 is 1100 1000, or 200 in decimal.
The same bits can mean different things depending on the agreed format:
- Unsigned integers of n bits hold 0 to 2n − 1.
- Signed integers in two's complement hold −2n−1 to 2n−1 − 1. A negative value −x is stored as 2n − x, and the top bit is 1 for negative numbers.
- Floating-point numbers under IEEE 754 split the bits into sign, exponent and fraction. Some values, such as 0.1, cannot be represented exactly.
Byte order must also be agreed. Little-endian puts the least significant byte first, as MAVLink and most Arm microcontrollers do. Big-endian puts the most significant byte first, and many sensors send register pairs that way, so always check the datasheet.
Logic gates and Boolean algebra
A logic gate takes input bits and produces an output bit by a fixed rule. The basic gates are AND, OR, NOT, XOR and NAND. Their behaviour is written in Boolean algebra: Y = A · B for AND and Y = A + B for OR. A frequently used rule is De Morgan's theorem, NOT(A · B) = NOT A + NOT B, which lets a circuit be rebuilt from one gate type; NAND alone can make every other gate.
In drone terms, "allow arming only if the safety switch is on AND GNSS is ready AND the battery is not low" is a three-input AND. In software the same logic is written with bitwise operators on a bitmask of status flags.
Combinational and sequential circuits
A combinational circuit has no memory: its output depends only on the present inputs. Key examples:
- Adders: a half adder gives sum S = A XOR B and carry C = A AND B. Chaining full adders adds multi-bit numbers and forms the core of the ALU.
- Multiplexers route one of several inputs to a single output according to select bits, like a channel selector switch.
- Decoders turn an n-bit code into one active line out of 2n, used to select a memory chip or peripheral by address.
A sequential circuit has memory: its output depends on the inputs and on its previous state. The basic element is the flip-flop, which stores one bit and changes only on an edge of the clock. Several flip-flops form a register; a register that adds one on every clock tick is a counter. Counters are the heart of the timers inside a microcontroller that generate PWM, measure pulse widths and keep time.
Example 1: a 16-bit timer generating ESC PWM
Assume a 16-bit timer counts a 1 MHz clock after the prescaler, so each tick lasts 1 µs.
- A 16-bit counter has 65,536 states, so it wraps every 65,536 µs, or 65.536 ms.
- For 400 Hz PWM the period is 1/400 = 2.5 ms, so the reload value is set to 2,500 ticks, well inside 65,536.
- A 1,500 µs pulse (mid throttle) uses a compare value of 1,500 ticks: the output is high while the count is below 1,500 and low for the rest of the period.
A 10 MHz timer clock gives ten times finer resolution, and the 2.5 ms period then needs 25,000 ticks, still within 16 bits. Choosing the timer clock is a trade between resolution and the longest period you can count.
Memory
Memory types differ mainly in whether data survives power-off.
| Type | After power-off | Character | Drone use |
|---|---|---|---|
| Registers and cache | Lost | Fastest, smallest, inside the CPU | Values being computed |
| SRAM / DRAM | Lost | Fast read and write; main memory | Variables, stack, data buffers |
| Flash | Kept | Fast reads, block-wise writes, limited write cycles | Firmware and constants |
| EEPROM / FRAM | Kept | Byte-wise writes | Parameters, calibration values |
| SD card / SSD | Kept | Large, slower | Flight logs, video |
Each memory location has an address. An n-bit address bus reaches 2n locations, so 2 MB of flash needs 21 address bits, because 221 = 2,097,152.
The CPU and computer architecture
A CPU contains a control unit, an ALU and registers, and repeats a fetch, decode, execute cycle paced by the clock. The set of instructions it understands is its instruction set architecture (ISA), such as Arm or RISC-V; C or Python programs are translated into these instructions before they run.
A microcontroller (MCU) puts a CPU together with memory, timers, an analogue-to-digital converter (ADC) and communication buses on one chip. A flight controller such as the Pixhawk 6X uses an Arm Cortex-M7 MCU, while a companion computer uses a faster multi-core processor running a general-purpose operating system. When a device needs immediate attention, for example the IMU has a new sample, it raises an interrupt so the CPU handles the urgent job first.
Example 2: decoding a raw accelerometer value
Assume an accelerometer set to a ±16 g range reports 16-bit two's complement values. The resolution is 32 g ÷ 65,536 = 1/2,048 g per count (assumed from the chosen range; check the datasheet of the actual part).
- The raw bytes combine to
0xF830, which is 63,536 if read as unsigned. - The top bit is 1, so the value is negative: 63,536 − 65,536 = −2,000.
- In physical units: −2,000 ÷ 2,048 ≈ −0.977 g.
About −0.977 g on the z axis of a drone at rest is consistent with gravity. Read as unsigned, the same bytes give about 31 g, which is impossible. Odd values like that are a cue to check data type and byte order before blaming the sensor.
Common pitfalls
- Reading signed data as unsigned, turning small negative values into huge ones.
- Swapping byte order between little-endian and big-endian.
- Forgetting that fixed-size counters wrap; compute differences modulo the counter size.
- Testing floating-point values for exact equality.
- Writing flash too often, for example saving parameters every second, and wearing it out.
- Comparing CPUs of different families by clock frequency alone.
Continue in the lessons
- UAT 105 Module 1: Number systems and digital logic: two's complement, IEEE 754, adders and the CRC of a MAVLink frame.
- UAT 105 Module 2: Computer architecture: von Neumann structure, memory hierarchy, interrupts and on-board buses.
Key references
- Harris, S. L., & Harris, D. (2021). Digital design and computer architecture: RISC-V edition. Morgan Kaufmann.
- IEEE. (2019). IEEE standard for floating-point arithmetic (IEEE Std 754-2019). link
- MAVLink Development Team. Packet serialization. MAVLink developer guide. link
- PX4 Autopilot. Holybro Pixhawk 6X. PX4 user guide. link
- 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