Microcontroller architecture and programming model
Microcontroller architecture and the 8051 programming model: memory map, register banks, PSW flags, addressing modes, machine-cycle timing and delay loops.
Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Almost every modern instrument, from a smart pressure transmitter to a pulse oximeter, has a microcontroller at its heart. A microcontroller puts the CPU, program memory, data memory, timers, serial ports, ADC and I/O pins on one chip, so the programming model (which registers exist, where memory lives and how long instructions take) decides how you write the firmware and how fast it can respond.
Key ideas
Microcontroller versus microprocessor. A microprocessor is a CPU that needs external memory and peripherals; it is optimised for computing power. A microcontroller integrates CPU, flash/ROM, RAM, EEPROM, timers, UART/SPI/I²C, ADC, PWM and GPIO on one chip; it is optimised for low cost, low power and direct control of hardware. Most use a Harvard architecture, with separate program and data memories.
The 8051 programming model (classic reference).
- 8-bit CPU, 16-bit PC and DPTR (data pointer); 64 KB program address space and 64 KB external data space.
- On-chip: 4 KB ROM, 128 bytes RAM, four 8-bit ports P0–P3, two 16-bit timers/counters, one full-duplex UART, five interrupt sources (INT0, Timer 0, INT1, Timer 1, serial).
- Internal RAM map: 00H–1FH four register banks of R0–R7 (bank chosen by RS1 RS0 in the PSW); 20H–2FH bit-addressable area (128 bits); 30H–7FH general scratch-pad. SFRs (A, B, PSW, SP, DPTR, port latches, timer and serial registers) sit at 80H–FFH.
- SP resets to 07H, so the stack starts at 08H (inside bank 1); programs that use bank 1 move SP higher.
- PSW bits: CY (carry), AC (auxiliary carry), F0 (user flag), RS1, RS0 (bank select), OV (signed overflow), P (parity of A: 1 when A has an odd number of 1s).
- Timing: one machine cycle = 12 oscillator periods. At 12 MHz a machine cycle is 1 µs; at 11.0592 MHz (chosen for standard baud rates) it is 1.085 µs. Most instructions take 1 or 2 machine cycles; MUL and DIV take 4.
- Port 0 is open-drain and needs pull-ups when used as general I/O; it also carries the multiplexed low address/data bus for external memory, with ALE latching the address.
Addressing modes. Immediate (MOV A, #25H), register (MOV A, R3), direct (MOV A, 40H), register-indirect (MOV A, @R0), indexed (MOVC A, @A+DPTR for look-up tables in code memory), and bit addressing (SETB P1.0).
Modern families. AVR and PIC are 8-bit RISC Harvard machines executing most instructions in one or a few clocks; ARM Cortex-M parts are 32-bit with nested vectored interrupt controllers, DMA and much richer peripherals. The concepts (register file, memory map, peripheral registers, interrupts, timers) carry over directly.
Programming. Firmware is written in C or assembly, compiled to the program memory (flash), and talks to peripherals by reading and writing their special-function registers. Typical structure: initialise clocks, ports and peripherals; then a main loop plus interrupt service routines. A watchdog timer resets the chip if the firmware stops servicing it.
Formulas
T_MC = 12 / f_osc (classic 8051)
- T_MC: machine-cycle time (s); f_osc: crystal frequency (Hz).
t_instr = (machine cycles) × T_MC
t_instr = (clock cycles) / f_clk (single-clock RISC parts)
Register bank address of R0 = 8 × (2·RS1 + RS0) (in hex: 00H, 08H, 10H, 18H)
ADC step = V_ref / 2ⁿ (on-chip n-bit ADC)
Worked examples
Example 1 (standard). An 8051 runs from a 12 MHz crystal. Find the machine-cycle time and the time taken by DJNZ (2 machine cycles). Repeat for 11.0592 MHz.
- T_MC = 12 / 12 MHz = 1 µs; DJNZ = 2 × 1 µs = 2 µs.
- At 11.0592 MHz: T_MC = 12 / 11.0592 MHz = 1.085 µs; DJNZ = 2.170 µs.
Answer: 1 µs and 2 µs; 1.085 µs and 2.17 µs
Example 2 (GATE level: nested delay). At 12 MHz, find the delay of: MOV R2, #200 (1 MC); L1: MOV R3, #250 (1 MC); L2: DJNZ R3, L2 (2 MC); DJNZ R2, L1 (2 MC).
- Inner loop: 250 × 2 = 500 MC.
- One outer pass: 1 (MOV R3) + 500 + 2 (DJNZ R2) = 503 MC.
- Total: 1 + 200 × 503 = 100 601 MC.
- Delay = 100 601 × 1 µs = 100.6 ms.
Answer: about 100.6 ms
Example 3 (flags). Execute MOV A, #9AH then ADD A, #7BH. Find A, CY, AC, OV and P.
- 9AH + 7BH = 115H, so A = 15H and CY = 1.
- Low nibbles: AH + BH = 15H > FH, so AC = 1.
- Signed: 9AH = −102, 7BH = +123; the sum +21 fits in −128 to +127, so OV = 0 (operands of opposite sign can never overflow).
- A = 15H = 0001 0101 has three 1s (odd), so P = 1.
Answer: A = 15H, CY = 1, AC = 1, OV = 0, P = 1
Common mistakes
- Using the crystal period as the 8051 instruction time; one machine cycle is 12 oscillator periods.
- Forgetting that the stack starts at 08H and overwrites register bank 1.
- Confusing CY (unsigned carry) with OV (signed overflow).
- Leaving Port 0 pins without pull-up resistors when used as outputs.
- Writing delays in software without counting the loop-setup and the final non-taken branch.
For GATE IN
Questions give a short assembly program and ask for register, memory or flag contents afterwards, the execution time or delay of a loop for a given crystal, the memory map and register-bank addresses, or the purpose of a peripheral. Practise tracing programs instruction by instruction and counting machine cycles for nested loops.
Quick check
- Machine-cycle time of an 8051 with a 24 MHz crystal?
- With RS1 RS0 = 10, at which internal RAM address is R0?
- What is SP after reset?
- An instruction needs 4 clock cycles on a 16 MHz single-clock MCU. How long does it take?
Answers: 1. 0.5 µs 2. 10H 3. 07H 4. 0.25 µs
Interview questions
All Digital Electronics and Microcontrollers interview questionsTry answering each one aloud before you open it.
1.What is a microcontroller and how does it differ from a microprocessor?Concept
A microcontroller is an integrated circuit designed to perform a specific task in an embedded system. It includes a CPU, memory (RAM and ROM), and peripherals on a single chip. In contrast, a microprocessor is a general-purpose processor that requires external components like memory and I/O ports to function. Microcontrollers are used in applications where control tasks are needed, while microprocessors are used in computers and devices requiring high processing power.
2.Explain the architecture of a typical microcontroller.Concept
A typical microcontroller architecture includes a central processing unit (CPU), memory units (RAM, ROM, EEPROM), input/output ports, timers, and communication interfaces like UART, SPI, or I2C. The CPU executes instructions stored in memory, while the I/O ports allow the microcontroller to interact with external devices. Timers are used for time-based operations, and communication interfaces enable data exchange with other devices.
3.What is the role of the program counter in a microcontroller?Concept
The program counter (PC) in a microcontroller is a register that holds the address of the next instruction to be executed. It ensures the sequential execution of instructions by incrementing after each instruction fetch. If a jump or branch instruction is executed, the PC is updated with the new address, allowing the program to continue from a different location.
4.Why are interrupts used in microcontrollers, and how do they work?Application
Interrupts are used in microcontrollers to handle asynchronous events, allowing the CPU to respond immediately to external or internal events without polling. When an interrupt occurs, the CPU pauses its current execution, saves its state, and executes an interrupt service routine (ISR) to address the event. After the ISR completes, the CPU resumes its previous task. This mechanism improves efficiency and responsiveness in real-time applications.
5.What happens if a microcontroller's watchdog timer is not reset?Application
If a microcontroller's watchdog timer is not reset within a predefined time interval, it assumes the system is malfunctioning and automatically resets the microcontroller. This feature is used to recover from software errors or system hangs, ensuring the system returns to a known state and continues operation.
6.How does the Harvard architecture benefit microcontroller performance?Application
The Harvard architecture separates the memory for instructions and data, allowing simultaneous access to both. This separation increases the throughput of the microcontroller by enabling parallel fetching of instructions and data, reducing bottlenecks and improving overall performance compared to the von Neumann architecture, where a single memory bus is used for both instructions and data.
7.Why is EEPROM used in microcontrollers, and what are its limitations?Application
EEPROM (Electrically Erasable Programmable Read-Only Memory) is used in microcontrollers to store non-volatile data that must be retained even when the power is off, such as configuration settings or calibration data. Its limitations include a finite number of write/erase cycles, typically around 100,000 to 1,000,000, and slower write speeds compared to RAM.
8.Calculate the time taken for a microcontroller with a 16 MHz clock to execute an instruction that requires 4 clock cycles.Numerical
The time taken to execute an instruction is calculated by dividing the number of clock cycles by the clock frequency. For a 16 MHz clock and an instruction requiring 4 clock cycles: Time = 4 cycles / 16,000,000 Hz = 0.25 microseconds.
9.A microcontroller has a 10-bit ADC with a reference voltage of 5 V. What is the smallest voltage change it can detect?Numerical
The resolution (1 LSB) is V_ref/2ⁿ = 5 V/1024 = 4.88 mV, so the converter's output changes by one count for about every 4.9 mV of input. (Some datasheets use V_ref/(2ⁿ − 1) = 4.89 mV; the difference is negligible.) In practice noise and the ADC's own errors of 1–2 LSB mean the useful resolution is a little coarser.
10.Explain the purpose of using timers in microcontrollers.Concept
Timers in microcontrollers are used to perform time-based operations such as generating precise delays, measuring time intervals, and creating pulse-width modulation (PWM) signals. They can operate in various modes, including counting external events or generating interrupts at specific intervals, making them essential for tasks like motor control, signal generation, and event counting.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?