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

# Display 0 to 9 on seven segment

Embedded System · Chapter 9: Microcontrollers in Embedded Systems

- Asked in: 2071 Bhadra, 8 marks; 2073 Bhadra, 5 marks (asked 2 times)
- Video: [Embedded C program to display  0 to 9 on 7 segment display with Keil and Proteus combination](https://www.youtube.com/watch?v=k9rmuSiCEeg) — a learning room
- 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-0-9
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-9

## Question

Write an assembly (and C) program to display 0 to 9 in seven segment display.

## Short answer

A common-cathode display on Port 1 uses segment codes 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH for digits 0–9. A loop fetches each code from a lookup table in ROM, outputs to P1, and a delay maintains display for about 0.5 s per digit.

## Answer

#### Circuit assumption

A **common-cathode** seven-segment display is connected to Port 1: P1.0 = a, P1.1 = b, ... P1.6 = g (P1.7 = dp unused). The common pin goes to ground, and each segment has a 330 ohm resistor. A segment lights when its pin is 1.

```text
  P1.0-P1.6 --[330R]--> a b c d e f g
  common cathode ------> GND
```

| Digit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Code (gfedcba) | 3F | 06 | 5B | 4F | 66 | 6D | 7D | 07 | 7F | 6F |

For a common-anode display use the complements (C0H, F9H, A4H, B0H, 99H, 92H, 82H, F8H, 80H, 90H).

#### Assembly program

```asm
        ORG 0000H
MAIN:   MOV R0,#0          ; digit counter 0..9
NEXT:   MOV A,R0
        MOV DPTR,#SEGTAB   ; table of segment codes
        MOVC A,@A+DPTR     ; A = code for digit
        MOV P1,A           ; send to 7-segment (P1.0=a ... P1.6=g)
        ACALL DELAY        ; show for about 0.5 s
        INC R0
        CJNE R0,#10,NEXT   ; until 10 digits shown
        SJMP MAIN          ; repeat 0..9

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
        END
```

The digit is used as an index into SEGTAB with `MOVC A,@A+DPTR`. It was assembled and tested in a simulator: successive iterations output P1 = 3FH, 06H, 5BH, ...

#### 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)
{
    unsigned char n;
    while (1) {
        for (n = 0; n < 10; n++) {
            P1 = seg[n];                /* show digit n */
            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, when does a segment light?
   A. When its pin is 0
   B. When its pin is 1
   C. Always
   D. Never
   Answer: B. The common pin is at ground, so a high segment pin lights the segment. (Key point: Common cathode)

2. What is the common-cathode code for digit 1?
   A. 3FH
   B. 5BH
   C. 06H
   D. 7FH
   Answer: C. Digit 1 lights b and c: 0000110 = 06H. (Key point: Segment codes)

3. How does the program get the code of digit n?
   A. Computes it with ADD
   B. Reads a port
   C. Uses a timer
   D. Uses MOVC A,@A+DPTR with the digit as index into SEGTAB
   Answer: D. The table of segment codes is indexed by the digit using indexed addressing. (Key point: Look-up table)

4. How are the codes changed for a common-anode display?
   A. Complement them (e.g. C0H for 0)
   B. Use the same codes
   C. Double them
   D. Add 1
   Answer: A. A common-anode segment lights on 0, so the codes are the complements. (Key point: Common anode)

5. How does CJNE R0,#10,NEXT stop after digit 9?
   A. It loops forever
   B. It clears R0 at 9
   C. When R0 reaches 10 the loop falls through and restarts at MAIN
   D. It halts the CPU
   Answer: C. After ten digits R0 = 10 so CJNE doesn't jump, and SJMP MAIN restarts. (Key point: Loop end)

## Related in Embedded System

- [Display 99 to 00 on seven segment](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-seven-segment-99-00) — Embedded System, chapter 9
- [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

## 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
