- Asked 2/19
- 2071 Bhadra · 8 marks
- 2073 Bhadra · 5 marks
Display 0 to 9 on seven segment
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.
Write an assembly (and C) program to display 0 to 9 in seven segment display.
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.
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
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
#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);
}
}
}
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Display 99 to 00 on seven segment · Embedded System, chapter 9
- Seven segment display and clock counter · Embedded System, chapter 9
- BCD counter 0-9999 on seven segment VHDL · Embedded System, chapter 9
- DEMUX vs decoder; seven-segment "b" segment · Digital Logic, chapter 4
- Seven-segment "a" segment using NOR gates · Digital Logic, chapter 4
- Seven-segment "b" and "d" segments · Digital Logic, chapter 4
- Seven-segment "e" segment · Digital Logic, chapter 4