8051 Microcontroller
The 8051 is the classic 8-bit microcontroller. Learn its architecture (CPU, 4 KB ROM, 128 B RAM, register banks, SFRs, four I/O ports, two timers, UART, five interrupts), machine cycles, timer modes and reload values, serial baud-rate generation, and programming in assembly and Embedded C (Keil).
A microcontroller puts CPU, memory and peripherals on one chip. Intel's 8051 (1980) remains in production in countless variants (NXP, Silicon Labs, Nuvoton) and in many university labs — a perfect first MCU.
An analogy
If a microprocessor is an engine that needs a whole car built around it, a microcontroller is a scooter — engine, wheels and controls in one small package.
Architecture
8-bit CPU, 4 KB on-chip ROM (up to 64 KB external), 128 B RAM (4 register banks R0–R7, bit-addressable area, stack), 21 SFRs, 32 I/O lines on ports P0–P3, two 16-bit timers, full-duplex UART, 5 interrupt sources (INT0, T0, INT1, T1, serial).
Timers
Mode 0 (13-bit), mode 1 (16-bit), mode 2 (8-bit auto-reload — used for baud rates), mode 3 (split). Timers count machine cycles (timer) or external pulses (counter).
Serial port
Mode 1 = 8-bit UART with variable baud from Timer 1. With 11.0592 MHz and TH1 = FDH → 9600 baud. SMOD = 1 doubles the rate.
Modern 8051s
Enhanced cores execute most instructions in 1–2 clocks instead of 12 (Silicon Labs EFM8 up to 72 MHz), with ADCs, PWM, I²C, SPI and USB — still popular in low-cost consumer devices.
Formulas
- Machine cycle:
T_MC = 12 / f_osc (1 µs at 12 MHz) - Timer reload (mode 1):
TH:TL = 65536 − (delay / T_MC) - Baud rate (mode 1, Timer 1 mode 2):
TH1 = 256 − f_osc / (384 × baud) (SMOD = 0)
Worked example: Timer reload for 1 ms
Given: 12 MHz → 1 µs per count; Mode 1
Find: TH0:TL0.
- Counts = 1000
- 65536 − 1000 = 64536 = FC18H
Answer: TH0 = FCH, TL0 = 18H
Worked example: Baud rate
Given: 11.0592 MHz, 9600 baud, SMOD 0
Find: TH1.
- TH1 = 256 − 11 059 200/(384 × 9600) = 256 − 3 = 253
Answer: TH1 = FDH
1 ms delay with Timer 0 and UART at 9600 baud (Keil C51)
#include <reg51.h>
void delay_1ms(void) { /* f_osc = 12 MHz -> 1 µs per count */
TMOD = (TMOD & 0xF0) | 0x01; /* Timer 0, mode 1 (16-bit) */
TH0 = 0xFC; TL0 = 0x18; /* 65536 - 1000 = 64536 = FC18H */
TR0 = 1; /* start */
while (!TF0); /* wait for overflow */
TR0 = 0; TF0 = 0;
}
void uart_init(void) { /* f_osc = 11.0592 MHz */
TMOD = (TMOD & 0x0F) | 0x20; /* Timer 1, mode 2 (8-bit auto-reload) */
TH1 = 0xFD; /* 9600 baud */
SCON = 0x50; /* mode 1, receiver enabled */
TR1 = 1;
}
void uart_tx(char c) { SBUF = c; while (!TI); TI = 0; }
void main(void) {
uart_init();
while (1) { P1 ^= 0x01; uart_tx('A'); delay_1ms(); }
}SFRs are set directly: TMOD selects timer modes, TH/TL hold the start count, TF flags overflow. 11.0592 MHz crystals give exact standard baud rates.
Common mistakes
- Wrong: Using 12 MHz crystals for 9600 baud. — Right: Use 11.0592 MHz for exact rates.
- Wrong: Delays longer than 65.5 ms in one timer run. — Right: Loop multiple overflows.
Applications
- Consumer devices: Remote controls, toys, appliances.
- Education: Labs in timers, serial and interrupts.
- Smart cards & USB: 8051 cores inside many chips.
Key takeaways
- CPU + ROM + RAM + timers + UART on one chip.
- 12 clocks per machine cycle.
- Reload = 65536 − counts.
- TH1 = 256 − f/(384 × baud).
Going further
- Enhanced 8051 cores: 1-clock instruction execution.
- Bit addressing: SETB/CLR on bit-addressable RAM.
- Power modes: IDLE and power-down via PCON.