Electronics and Communication Engineering‹›⌂ 0:00 studied

Module 4 · 12 min read · Free chapter

8051 microcontroller, timers, serial port and peripheral interfacing

In short

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.

See it in 3DWatch an 8051 timer count to overflow. Turn it, cut it open, or just press Watch.
What are you preparing for? We show the practice and previous year questions of your exam.

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 EA‾\overline{\text{EA}} 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 AD0\text{AD}_0–AD7\text{AD}_7 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 A8A_8–A15A_{15} for external memory.
  • P3 pins have alternate functions: RXD, TXD, INT0‾\overline{\text{INT0}}, INT1‾\overline{\text{INT1}}, T0, T1, WR‾\overline{\text{WR}} and RD‾\overline{\text{RD}}.

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 100×2=200100 \times 2 = 200 μ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 fosc12\dfrac{f_{\text{osc}}}{12}; 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/T‾\overline{\text{T}}, 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 NN machine cycles in mode 1, load the timer with 65 536−N65\,536 - N 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, N=1000N = 1000, so load 65 536−1000=64 536=FC181665\,536 - 1000 = 64\,536 = \text{FC18}_{16}: 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:

baud=2SMOD32×fosc12 (256−TH1)\text{baud} = \frac{2^{\text{SMOD}}}{32} \times \frac{f_{\text{osc}}}{12\,(256 - \text{TH1})}

The crystal 11.0592 MHz is chosen because it divides evenly into standard rates: with SMOD = 0, TH1 = FDH (256−3256 - 3) 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 INT0‾\overline{\text{INT0}} IE0 0003H
Timer 0 overflow TF0 000BH
External INT1‾\overline{\text{INT1}} 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/W‾\overline{\text{W}} 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 1000 00002=80H1000\,0000_2 = 80\text{H}. To make port A an input and ports B and C outputs, set bit 4 too: 1001 00002=90H1001\,0000_2 = 90\text{H}.

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:

  1. Compare a microprocessor with a microcontroller, and draw and explain the 8051 architecture and pin functions.
  2. Explain the 8051 internal memory map, register banks, bit-addressable area and SFRs.
  3. Explain the timer modes and serial port, and compute reload values and baud rates.
  4. 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).

  1. Compare the 8085 microprocessor with the 8051 microcontroller in architecture, memory and on-chip peripherals. Why are microcontrollers preferred for embedded control? (12 marks)
  2. 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)
  3. 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 N=500N = 500 and the reload is 65 536−500=65 036=FE0C1665\,536 - 500 = 65\,036 = \text{FE0C}_{16}.
  • 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 NN.

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 nn, R$k$ at 8n+k8n + k.
  • 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 65 536−N65\,536 - N in mode 1.
  • Baud (SMOD = 0) =fosc384(256−TH1)= \dfrac{f_{\text{osc}}}{384(256 - \text{TH1})}; 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.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 12 answered

  1. How much on-chip RAM does the standard 8051 have?

  2. With a 12 MHz crystal, one machine cycle of the 8051 lasts:

  3. Which 8051 port needs external pull-up resistors when used as an output port?

  4. After reset, the 8051 stack pointer holds:

  5. Register R5 of register bank 2 is at internal RAM address:

  6. The bit-addressable area of 8051 internal RAM spans:

  7. The addressing mode used in MOVC A, @A+DPTR is:

  8. Which registers can be used as pointers in the 8051 instruction MOV A, @Ri?

  9. For a 2 ms delay using timer 0 in mode 1 with a 12 MHz crystal, the value loaded into TH0:TL0 is:

  10. With an 11.0592 MHz crystal, SMOD = 0 and timer 1 in mode 2, which TH1 value gives 9600 baud?

  11. The vector address of the timer 1 overflow interrupt in the 8051 is:

  12. The 8255 control word that sets port A as input and ports B and C as outputs, all in mode 0, is:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What is the difference between a microprocessor and a microcontroller?

A microprocessor is only the CPU and needs external memory and I/O chips; a microcontroller has CPU, memory, timers and ports on one chip for control tasks.

What is the reset value of the 8051 stack pointer?

07H, so the first push goes to 08H; the 8051 stack grows upwards.

Why is an 11.0592 MHz crystal used with the 8051?

It divides exactly into standard baud rates; with timer 1 in mode 2 and TH1 = FDH it gives exactly 9600 baud.

What does the 8255 control word 80H mean?

Bit 7 = 1 selects I/O mode, and all other bits 0 make ports A, B and C outputs in mode 0.

More in this module: Microprocessors and computer organisation

Official sources

Facts in this chapter were checked against these official sources.

  1. UPSC Civil Services Examination 2026 notice, Electrical Engineering optional, Paper II
  2. NPTEL Microprocessors and Microcontrollers (IIT Kharagpur, 108105102): 8051 architecture, instructions, timers, serial port and interfacing