---
title: "Precise 5 ms delay in 8051"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-5ms-delay"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-5ms-delay
site: BCT Engineering
---

# Precise 5 ms delay in 8051

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2071 Magh, 4 marks
- Video: [8051 Timers Explanation- Delay Calculation- Embedded systems - Full course- Session 6](https://www.youtube.com/watch?v=vyuuS-fw_4M) — EROTECH SOLUTIONS
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/8051-5ms-delay
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Write a short program in assembly language that computes a precise 5-millisecond delay using the 8051 microcontroller.

## Short answer

At 12 MHz, 1 machine cycle = 1 µs, so 5 ms needs 5000 cycles. Load R7=15, R6=165; inner loop DJNZ takes 2 cycles. Total cycles = 2+1+15×(1+165×2+2)+2 =5000, yielding exactly 5 ms delay.

## Answer

Assume a 12 MHz crystal, so one machine cycle = 1 us and 5 ms = 5000 machine cycles. The delay is made with two nested DJNZ loops, with the count chosen so that the total, including the call and return, is exactly 5000 cycles.

#### Calculation

- ACALL = 2 cycles, RET = 2 cycles, `MOV R7,#15` = 1 cycle.
- Each outer pass: `MOV R6,#165` (1) + inner loop 165 x `DJNZ` (2 each = 330) + outer `DJNZ R7` (2) = 333 cycles.
- 15 passes = 4995 cycles. Total = 2 + 1 + 4995 + 2 = 5000 cycles = 5.000 ms.

```asm
        ORG 0000H
MAIN:   ACALL DELAY5MS
        SJMP MAIN

; 5 ms delay at XTAL = 12 MHz (machine cycle = 1 us)
; cycles = ACALL 2 + MOV 1 + 15 x (MOV 1 + DJNZ 165 x 2 + DJNZ 2) + RET 2 = 5000
DELAY5MS: MOV R7,#15
OUTER:    MOV R6,#165
INNER:    DJNZ R6,INNER
          DJNZ R7,OUTER
          RET
          END
```

In a simulator at 12 MHz the call measured exactly 5.000 ms.

#### Alternative using Timer 0

```asm
DELAY5T: MOV TMOD,#01H     ; Timer 0, mode 1
         MOV TH0,#0ECH     ; 65536 - 5000 = 60536 = EC78H
         MOV TL0,#78H
         SETB TR0          ; start the timer
WAIT:    JNB TF0,WAIT      ; wait for overflow
         CLR TR0           ; stop timer
         CLR TF0           ; clear flag
         RET
```

This takes about 5.013 ms because of the few setup instructions; load 65536 - 5000 + 13 to compensate if it must be exact.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. With a 12 MHz crystal, how many machine cycles make a 5 ms delay?
   A. 500
   B. 50000
   C. 5000
   D. 12000
   Answer: C. One machine cycle is 1 us at 12 MHz, so 5 ms = 5000 cycles. (Key point: Cycles needed)

2. How many cycles does one outer pass take (MOV R6,#165; 165 x DJNZ; DJNZ R7)?
   A. 165
   B. 333
   C. 330
   D. 168
   Answer: B. 1 + 165 x 2 + 2 = 333 cycles. (Key point: Cycle count per pass)

3. Why does the program give exactly 5000 cycles?
   A. 15 x 333 = 4995 plus 5 cycles of ACALL, MOV and RET
   B. 165 x 15 x 2 = 4950
   C. 15 x 333 only
   D. DJNZ takes 1 cycle
   Answer: A. 4995 + ACALL 2 + MOV 1 + RET 2 = 5000. (Key point: Total delay)

4. In the Timer 0 mode 1 alternative, what is loaded into TH0:TL0 for 5000 counts?
   A. 5000 = 1388H
   B. 65536 - 5000 = EC78H
   C. 65536 + 5000
   D. FFFFH
   Answer: B. A 16-bit timer counts up to overflow, so load 65536 - 5000 = 60536 = EC78H. (Key point: Timer reload)

## Related in Embedded System

- [Microcontroller vs microprocessor; 8051 features](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Amicro-vs-microprocessor-8051-features) — Embedded System, chapter 9, asked 2×
- [Microprocessor vs microcontroller; 8051 addressing](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-addressing-modes-micro-vs-processor) — Embedded System, chapter 9
- [8051 minimum circuit and 75% duty pulse](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-min-config-pwm-75) — Embedded System, chapter 9
- [Addressing modes of 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-addressing-modes) — Embedded System, chapter 9, asked 4×
- [Why 8051 / its operational features](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Awhy-8051-features) — Embedded System, chapter 9, asked 2×
- [Purpose of port 3 and port 2 of 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-port3-port2-purpose) — Embedded System, chapter 9, asked 2×

## Related in other subjects

- [8086 assembly language operators](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3A8086-operators) — Microprocessor, chapter 3, asked 3×
- [Assembly line balancing with cycle time](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_engineering_and_management%3Aassembly-line-balancing-problem) — Industrial Engineering and Management, chapter 2
- [Program for RK-4 first order IVP](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-program-first-order) — Numerical Method, chapter 6
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
