---
title: "Display 99 to 00 on seven segment"
subject: "Embedded System"
chapter: 9
key: "semester_6/embedded_system:8051-seven-segment-99-00"
source: https://www.bctengineer.com/q/semester_6/embedded_system/8051-seven-segment-99-00
site: BCT Engineering
---

# Display 99 to 00 on seven segment

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2070 Magh, 8 marks
- Video: [Counting from 0 to 99 using 8051 microcontroller with 7 segment display](https://www.youtube.com/watch?v=7drkBVTWSkM) — Embedotronics Technologies
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/8051-seven-segment-99-00
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Write 8051 program and draw circuit diagram to display number from 99 to 00 in seven segment display. The program should write in both assembly and C.

## Answer

#### Circuit

Two common-cathode seven-segment displays: units digit on Port 1, tens digit on Port 2, each segment through a 330 ohm resistor.

```text
 8051                    Tens display   Units display
 P2.0-P2.6 --[330R x7]--> a b c d e f g
 P1.0-P1.6 --[330R x7]------------------> a b c d e f g
                          common cathode  common cathode
                               |                |
                              GND              GND
```

The segment code for each digit (gfedcba, common cathode) is: 0=3F, 1=06, 2=5B, 3=4F, 4=66, 5=6D, 6=7D, 7=07, 8=7F, 9=6F. The count is kept as packed BCD in R0 and decremented using `ADD A,#99H` followed by `DA A` (adds 99H, which equals subtracting 1 in BCD).

#### Assembly program

```asm
        ORG 0000H
MAIN:   MOV R0,#99H          ; BCD count, start at 99
SHOW:   MOV DPTR,#SEGTAB
        MOV A,R0
        ANL A,#0FH           ; units digit
        MOVC A,@A+DPTR
        MOV P1,A             ; units display on Port 1
        MOV A,R0
        SWAP A
        ANL A,#0FH           ; tens digit
        MOVC A,@A+DPTR
        MOV P2,A             ; tens display on Port 2
        ACALL DELAY          ; about 0.5 s
        MOV A,R0
        JZ MAIN              ; after 00 start again from 99
        ADD A,#99H           ; A = A + 99H, then DA A gives BCD A - 1
        DA A
        MOV R0,A
        SJMP SHOW

DELAY:  MOV R5,#4
D1:     MOV R6,#250
D2:     MOV R7,#250
D3:     DJNZ R7,D3
        DJNZ R6,D2
        DJNZ R5,D1
        RET

SEGTAB: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH   ; common cathode codes
        END
```

Checked in a simulator: the sequence 99, 98, ... 90, 89, ... 00 is displayed, then restarts at 99.

#### C program

```c
#include <reg51.h>

unsigned char code seg[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for (i = 0; i < ms; i++)
        for (j = 0; j < 120; j++);          /* about 1 ms at 12 MHz */
}

void main(void)
{
    signed char n;                          /* must be signed: loop ends at -1 */
    while (1) {
        for (n = 99; n >= 0; n--) {
            P2 = seg[n / 10];               /* tens digit on Port 2  */
            P1 = seg[n % 10];               /* units digit on Port 1 */
            delay_ms(500);
        }
    }
}
```

## Check yourself

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

1. For a common-cathode display, how is a segment lit?
   A. Drive its pin low
   B. Drive its pin high (1)
   C. Connect to +5 V directly
   D. Remove the resistor
   Answer: B. The cathodes are grounded, so a 1 on a segment pin lights it. (Key point: Common cathode)

2. How does the assembly program get the segment pattern for a digit?
   A. MOVC A,@A+DPTR from the SEGTAB table
   B. Calculates it with MUL
   C. Reads it from P1
   D. Uses DA A
   Answer: A. The digit is the index into SEGTAB read by MOVC A,@A+DPTR. (Key point: Segment code lookup)

3. How is the packed BCD count decremented?
   A. ADD A,#99H followed by DA A
   B. SUBB A,#1
   C. DEC A only
   D. INC A then DA A
   Answer: A. Adding 99H and applying DA A is equivalent to subtracting 1 in BCD. (Key point: BCD decrement)

4. Why is n declared as signed char in the C program?
   A. To save memory
   B. Because the loop ends when n becomes -1, so n >= 0 fails
   C. For P2
   D. For delay
   Answer: B. With unsigned n, n >= 0 is always true and n-- would wrap, so the loop would never end. (Key point: C loop variable)

## Related in Embedded System

- [Display 0 to 9 on seven segment](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-seven-segment-0-9) — Embedded System, chapter 9, asked 2×
- [Seven segment display and clock counter](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aseven-segment-clock-design) — Embedded System, chapter 9
- [BCD counter 0-9999 on seven segment VHDL](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Avhdl-bcd-counter-seven-segment) — 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
- [8051 pin diagram; ports 1 and 2](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-pin-diagram-ports-1-2) — Embedded System, chapter 9
- [Precise 5 ms delay in 8051](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-5ms-delay) — Embedded System, chapter 9

## Related in other subjects

- [DEMUX vs decoder; seven-segment "b" segment](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Ademux-decoder-b-segment) — Digital Logic, chapter 4
- [Seven-segment "a" segment using NOR gates](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Aa-segment-nor-gates) — Digital Logic, chapter 4
- [Seven-segment "b" and "d" segments](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Ab-d-segments) — Digital Logic, chapter 4
- [Seven-segment "e" segment](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Ae-segment-seven-segment) — Digital Logic, chapter 4
