Module 4 · 12 min read · Free chapter
8051 microcontroller, timers, serial port and peripheral interfacing
The 8051 puts an 8-bit CPU, 4 KB ROM, 128 bytes RAM, four ports, two 16-bit timers, a UART and five interrupts on one chip. With a 12 MHz crystal a machine cycle is 1 μs, so a 1 ms delay in timer mode 1 loads 65 536 − 1000 = FC18H. TH1 = FDH at 11.0592 MHz gives 9600 baud.
Start from zero: why a microcontroller?
A microprocessor such as the 8085 needs separate chips for memory, timers, ports and serial communication before it can do anything useful. That suits a computer, but not a washing machine or a traffic light, where cost and board space matter more than raw speed.
A microcontroller puts the processor, program memory, data memory, timers, I/O ports and a serial port on a single chip. Intel's 8051 of 1980 did exactly this, and its design is still made by many companies and used in meters, remotes and appliances.
This chapter covers the 8051's architecture and memory map, its instructions and addressing modes, timers and the serial port, interrupts, and interfacing to displays, converters, motors and the 8255 programmable peripheral chip.
It builds on the microprocessor ideas of 8085 and 8086 microprocessors and the converters of data converters and memories.
Why it matters. 8051 memory map, timer reload values, baud rates and 8255 control words are standard ESE, RRB JE and state AE questions.
8051 architecture and ports
The 8051 has an 8-bit CPU with accumulator A and a second register B used in multiply and divide. On the chip are 4 KB of program ROM, 128 bytes of RAM, two 16-bit timers, a full-duplex serial port, five interrupt sources and four 8-bit ports, P0 to P3, giving 32 I/O lines.
The 8051 keeps program and data in separate address spaces, a Harvard architecture. It can address 64 KB of external code memory using the 16-bit program counter, and 64 KB of external data memory using the 16-bit DPTR (data pointer). The pin chooses whether code is fetched from the internal ROM or from outside.
The oscillator runs from an external crystal, commonly 12 MHz or 11.0592 MHz. One machine cycle is 12 oscillator periods, so a 12 MHz crystal gives 1 μs machine cycles. Most instructions take one or two machine cycles; MUL and DIV take four.
The ports have different jobs:
- P0 doubles as the multiplexed low address and data bus – when external memory is used, latched with ALE. Its pins are open-drain and need external pull-up resistors as an output port.
- P1 is a plain I/O port.
- P2 carries the high address byte – for external memory.
- P3 pins have alternate functions: RXD, TXD, , , T0, T1, and .
Why it matters. Counts of on-chip ROM, RAM, timers and ports, the machine-cycle time and port functions are frequent one-mark questions.
Memory organisation and special function registers
The 128 bytes of internal RAM, addresses 00H to 7FH, are divided into three parts:
- Register banks, 00H to 1FH: four banks of eight registers R0 to R7. Bits RS1 and RS0 of the PSW select the active bank; bank 0 is used after reset.
- Bit-addressable area, 20H to 2FH: 16 bytes whose 128 individual bits have bit addresses 00H to 7FH, so single flags can be set and tested.
- Scratch-pad RAM, 30H to 7FH: general storage, often holding the stack.
Addresses 80H to FFH hold the special function registers (SFRs) that control the chip. Important ones are A (E0H), B (F0H), PSW (D0H), SP (81H), DPTR (DPH:DPL), the four ports P0 (80H) to P3 (B0H), TMOD and TCON for the timers, SCON and SBUF for the serial port, and IE and IP for interrupts. SFRs whose address ends in 0H or 8H are also bit-addressable.
The stack pointer is 8 bits and resets to 07H, so the first push goes to 08H, inside register bank 1. Programs that use bank 1 or the bit area usually move SP higher, for example with MOV SP, #5FH. Unlike the 8085, the 8051 stack grows upwards.
The PSW holds the flags CY (carry), AC (auxiliary carry), F0 (user flag), RS1 and RS0 (bank select), OV (overflow, for signed arithmetic) and P (parity of A).
Worked example. To use register bank 2, set RS1 = 1 and RS0 = 0, for example with SETB PSW.4 and CLR PSW.3. R0 of bank 2 is then RAM address 10H.
Why it matters. "What is the address of R3 of bank 1?", the reset value of SP and bit-addressable ranges are favourite questions.
Instructions and addressing modes
The 8051 has 111 instructions, grouped into data transfer, arithmetic, logical, Boolean (single-bit) and branch instructions. Its addressing modes are:
- Immediate: MOV A, #25H loads the constant 25H; the # marks immediate data.
- Register: MOV A, R0 copies a register of the active bank.
- Direct: MOV A, 40H copies the byte at RAM address 40H, or an SFR such as P1.
- Register indirect: MOV A, @R0 uses R0 or R1 as a pointer into internal RAM; MOVX A, @DPTR reaches external data memory.
- Indexed: MOVC A, @A+DPTR reads code memory at DPTR + A, which is how look-up tables are stored in ROM.
The Boolean processor works on single bits: SETB P1.0, CLR C, CPL P1.7, MOV C, P3.2, and the jumps JB, JNB and JBC. This makes switch reading and relay control very compact.
Arithmetic includes ADD, ADDC, SUBB (subtract with borrow; there is no plain subtract), INC, DEC, DA A for BCD, and MUL AB and DIV AB, which put 16-bit products and quotient–remainder pairs in A and B.
Loops use DJNZ, decrement and jump if not zero, which does in one instruction what the 8085 needs DCR and JNZ for. Other branches are SJMP, AJMP, LJMP, JZ, JNZ, CJNE (compare and jump if not equal), ACALL, LCALL and RET.
Worked example. A 200-cycle delay at 12 MHz: MOV R2, #100; HERE: DJNZ R2, HERE. DJNZ takes 2 machine cycles, so the loop takes about μs, plus 1 μs for the MOV.
Why it matters. Identifying the addressing mode, MUL/DIV results and DJNZ delay counts are routine 8051 questions.
Timers and the serial port
Each of the two timers, T0 and T1, is a 16-bit register pair (TH0:TL0 and TH1:TL1). As a timer it counts machine cycles, at ; as a counter it counts pulses on the T0 or T1 pin. When it rolls over from its maximum to zero it sets an overflow flag, TF0 or TF1, in TCON.
The TMOD register sets each timer's mode with bits M1 and M0, chooses timer or counter with C/, and can gate counting by an external pin with GATE:
- Mode 0: 13-bit timer, kept for compatibility with the older 8048.
- Mode 1: 16-bit timer, used for most delays.
- Mode 2: 8-bit auto-reload; TL counts and reloads from TH on each overflow, ideal for baud rates.
- Mode 3: timer 0 splits into two 8-bit timers.
For a delay of machine cycles in mode 1, load the timer with and wait for TF to set. The run bits TR0 and TR1 in TCON start and stop the timers.
Worked example. For a 1 ms delay at 12 MHz, , so load : TH0 = FCH, TL0 = 18H. Toggling a pin after every overflow gives a 500 Hz square wave.
The serial port sends and receives through SBUF, controlled by SCON. In the common mode 1 (8-bit UART: start bit, 8 data bits, stop bit), the baud rate comes from timer 1 in mode 2:
The crystal 11.0592 MHz is chosen because it divides evenly into standard rates: with SMOD = 0, TH1 = FDH () gives exactly 9600 baud. Setting SMOD = 1 in PCON doubles the rate.
Why it matters. Timer reload values and baud-rate settings are the most common 8051 numericals.
Interrupts and peripheral interfacing
The 8051 has five interrupt sources, each with a fixed vector address eight bytes apart. In default priority order:
| Source | Flag | Vector |
|---|---|---|
| External | IE0 | 0003H |
| Timer 0 overflow | TF0 | 000BH |
| External | IE1 | 0013H |
| Timer 1 overflow | TF1 | 001BH |
| Serial port | RI or TI | 0023H |
The IE register enables each source, and its bit EA enables or disables them all. The IP register can raise any source to high priority, so it can interrupt a low-priority routine. External interrupts can be level- or edge-triggered, set by IT0 and IT1 in TCON.
Common interfacing tasks are:
- LEDs and switches: an LED through a resistor on a port pin, a switch read with a pull-up and debounced in software.
- Seven-segment displays: segment codes from a look-up table, several digits multiplexed by enabling one at a time fast enough that the eye sees all lit.
- LCD modules: data on one port, RS, R/ and E on control pins.
- ADC0804 and DAC0808: an 8-bit ADC started and read through port pins, and a DAC driven from a port to generate waveforms.
- Relays and stepper motors: through transistor or ULN2003 drivers, since port pins supply only a few milliamperes; a four-phase stepper steps through a fixed pattern such as 09H, 0CH, 06H, 03H.
For processors without built-in ports, the 8255 programmable peripheral interface adds three 8-bit ports, A, B and C. Port C can split into two 4-bit halves. Mode 0 is simple I/O, mode 1 is strobed I/O with handshake lines on port C, and mode 2 (port A only) is bidirectional.
The 8255 is set up by writing a control word. With bit 7 = 1 it defines port modes and directions; with bit 7 = 0 it sets or resets a single bit of port C (BSR mode).
Worked example. To make all three ports outputs in mode 0, write . To make port A an input and ports B and C outputs, set bit 4 too: .
Why it matters. 8051 vector addresses, IE settings, 8255 control words and stepper-motor sequences all appear in ESE and AE papers.
Descriptive lens
Written papers ask about this topic in four ways:
- Compare a microprocessor with a microcontroller, and draw and explain the 8051 architecture and pin functions.
- Explain the 8051 internal memory map, register banks, bit-addressable area and SFRs.
- Explain the timer modes and serial port, and compute reload values and baud rates.
- Explain the 8051 interrupt structure and interfacing to displays, converters and motors, or the modes and control word of the 8255.
Practice questions (our own, not previous-year questions).
- Compare the 8085 microprocessor with the 8051 microcontroller in architecture, memory and on-chip peripherals. Why are microcontrollers preferred for embedded control? (12 marks)
- Write an 8051 program using timer 0 in mode 1 to generate a 1 kHz square wave on P1.0 with a 12 MHz crystal. Show how the reload value is found. (15 marks)
- Explain the modes of the 8255 PPI. Write the control word to set port A as input in mode 0 and ports B and C as outputs, and explain the BSR mode. (10 marks)
Answer plan for question 2.
- A 1 kHz wave has a 1 ms period, so the pin must toggle every 500 μs.
- At 12 MHz one machine cycle is 1 μs, so and the reload is .
- Set TMOD = 01H, load TH0 = FEH and TL0 = 0CH, and set TR0.
- Wait for TF0, then clear TF0 and TR0, complement P1.0 and reload the timer.
- Note that reload overhead adds a few microseconds, which can be trimmed from .
Quick revision
- 8051: 8-bit CPU, 4 KB ROM, 128 B RAM, 4 ports (32 lines), 2 timers, UART, 5 interrupts.
- Harvard: 64 KB code (PC) and 64 KB data (DPTR); machine cycle = 12 clocks.
- P0 = AD bus (needs pull-ups), P2 = high address, P3 = alternate functions.
- RAM: banks 00H–1FH, bit area 20H–2FH, scratch 30H–7FH; SFRs 80H–FFH.
- SP resets to 07H; stack grows upwards; bank , R$k$ at .
- Modes: immediate (#), register, direct, indirect (@R0, @R1), indexed (MOVC A, @A+DPTR).
- Timer modes 0 (13-bit), 1 (16-bit), 2 (auto-reload), 3 (split); reload in mode 1.
- Baud (SMOD = 0) ; TH1 = FDH gives 9600 at 11.0592 MHz.
- Vectors: INT0 0003H, T0 000BH, INT1 0013H, T1 001BH, serial 0023H; EA is the master enable.
- 8255: ports A, B, C; modes 0, 1, 2; control word 80H = all outputs, bit 7 = 0 is BSR.