KEMBAR78
Es Lab 2 | PDF | Microcontroller | Electrical Engineering
0% found this document useful (0 votes)
11 views11 pages

Es Lab 2

Uploaded by

thebikashpokhrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Es Lab 2

Uploaded by

thebikashpokhrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

TR IB H U V A N U N IV ER SITY

IN STITU TE OF EN G IN EER IN G
P U LC H OW K C A M P U S

A Lab R eport On
Em bedded System s: Interfacing 7-Segm ent LED D isplay w ith
8051/8052 M icro-controller

SU B M ITTED B Y SU B M ITTED TO
N am e: Bikash Pokhrel
Department Of Electronics and
R oll N o: 078BEI010
Computer Engineering
Lab D ate: 2081-09-02

Subm ission D ate: 2081-10-02 Signature:


Lab 2: Interfacing 7-Segment LED Display with
8051/8052 Micro-controller

Objectives
To enable us to write assembly and embedded C code for the 8051/8052 micro-controller capable of:
• Displaying non-multiplexed and multiplexed output on 7-segment LED units

• Displaying static and scrolling output on 7-segment LED units

Equipment Required
• Hardware: 8051 or 8052 micro-controller development board, Jumper cables

• Simulation Software: KEIL, Vision-Embedded development tool, Proteus Design Suite – Professional
PCB layout, circuit design and simulation tool
• Inn-System Programming (ISP) Software: ProgISP – An in-system-programmable tool to load HEX files
in to micro-controller

• Device Drivers: LibUSB – Application controlling data transfer to/from USB devices

Theory
The 8051/8052 microcontroller is a widely used 8-bit microcontroller developed by Intel. It is a versatile
microcontroller with a rich instruction set, making it suitable for various embedded applications. The 8051
architecture includes features such as 4 KB of on-chip ROM, 128 bytes of RAM, 32 I/O pins, timers/counters,
serial communication, and interrupt handling.

Architecture of 8051 Microcontroller


The 8051 microcontroller consists of the following key components:
• CPU (Central Processing Unit): The 8051 CPU is an 8-bit processor that executes instructions
fetched from program memory.
• Memory:

– Program Memory (ROM): Stores the program code. In the 8051, it is 4 KB, while in the 8052,
it is 8 KB.
– Data Memory (RAM): Used for temporary data storage. The 8051 has 128 bytes of RAM, and
the 8052 has 256 bytes.

• I/O Ports: The 8051 has four 8-bit I/O ports (P0, P1, P2, P3) for interfacing with external devices.
• Timers/Counters: Two 16-bit timers/counters (Timer 0 and Timer 1) are available for timing and
counting operations.
• Serial Port: A full-duplex UART (Universal Asynchronous Receiver/Transmitter) for serial communi-
cation.

• Interrupts: The 8051 supports five interrupt sources, including two external interrupts, two timer inter-
rupts, and one serial port interrupt.

1
Figure 1: 8051 Microcontroller

Assembly Language Programming


Assembly language is a low-level programming language that directly corresponds to the machine code instruc-
tions of the microcontroller. It provides precise control over the hardware and is commonly used for time-critical
applications. The 8051 assembly language includes instructions for data manipulation, arithmetic and logic
operations, branching, and subroutine calls.

C Language Programming
C language is a high-level programming language that is often used for microcontroller programming due to
its portability and ease of use. The Keil C compiler is commonly used for writing and compiling C programs
for the 8051 microcontroller. C programs are compiled into machine code and loaded into the microcontroller’s
program memory.

Development Tools
• Keil Vision: An integrated development environment (IDE) for writing, debugging, and simulating
assembly and C programs for the 8051 microcontroller.

• Proteus Design Suite: A simulation tool used to design and test microcontroller-based circuits. It
allows for the simulation of the microcontroller along with other electronic components.
• ProgISP: A tool for in-system programming (ISP) to load the compiled HEX file into the microcontroller.

Circuit Diagram
The circuit diagram, consisting of micro-controller AT89C52 and four common cathode 7 segment display, used
for simulation for this lab is shown below:
Figure shows that the common data lines, from the array of four seven segment display, are connected to
the PORT 0 of microcontroller with array of 8 pull-up resistor. Here data line A is connected to P0.0 (LSB)
whereas DP is connected to P0.7 (MSB). The control pins 1, 2, 3 and 4 are indirectly connected to P2.0, P2.1,
P2.2 and P2.3 respectively. Since logic low should be applied to control pin to trigger corresponding segment,
here transistor is used to invert the logic. It means in order to trigger a certain segment, let’s say 1, logic high

2
Figure 2: Proteus Circuit Diagram

is applied to the connected pin, here P2.0, so that there will be logic low across the transistor where the control
pin 1 is connected.
Now in order to display digits on more than one segment then illusion technique most be used. It means
we have to give an illusion that multiple values are displayed at once on multiple 7-segment LED units using
shared data lines. This illusion is created due to the persistence of vision as we know that human brain cannot
differentiate between the two events occurring at a time difference of less than 40 milliseconds. Hence the data
must be passed to the common data lines at a rate of about 60 to 100 times per second in order to avoid
flickering. At the same time corresponding 7-segment units need to be turned ON or OFF.

Problems
Q1. Write a code to design a single digit decimal counter that counts up from 0
to 9 and back to 0. This process should repeat indefinitely.
Assembly Code

ORG 0000 H
MOV DPTR , # SEGMENTS ; Point to segment lookup table
MOV R0 , #00 H ; Counter register
START :
MOV A , R0 ; Get the current count
MOVC A , @A + DPTR ; Load the segment pattern
MOV P0 , A ; Output the segment pattern
ACALL DELAY ; Delay for visibility
INC R0 ; Increment the counter
CJNE R0 , #0 AH , START ; Repeat if not 10
DEC R0 ; Start counting down
COUNT_DOWN :
MOV A , R0
MOVC A , @A + DPTR
MOV P0 , A

3
ACALL DELAY
DJNZ R0 , COUNT_DOWN ; Repeat until 0
SJMP START ; Repeat the cycle

DELAY :
MOV R2 , #255
MOV R3 , #255
DLOOP :
DJNZ R3 , DLOOP
DJNZ R2 , DLOOP
RET

SEGMENTS : ; Lookup table for 7 - segment patterns


DB 3 FH , 06 H , 5 BH , 4 FH , 66 H , 6 DH , 7 DH , 07 H , 7 FH , 6 FH
END

C Code

# include < reg51 .h >

unsigned char seg_patterns [] = {0 x3F , 0 x06 , 0 x5B , 0 x4F , 0 x66 , 0


x6D , 0 x7D , 0 x07 , 0 x7F , 0 x6F };

void delay () {
unsigned int i , j ;
for ( i = 0; i < 500; i ++) {
for ( j = 0; j < 120; j ++) ;
}
}

void main () {
unsigned char count = 0;
unsigned char direction = 1;

while (1) {
P0 = seg_patterns [ count ];
delay () ;

if ( direction == 1) {
count ++;
if ( count > 9) {
count = 9;
direction = 0;
}
} else {
count - -;
if ( count == 0) {
direction = 1;
}
}
}
}

4
Output
The program counts from 0 to 9 and back to 0 which is seen in the 7 Segment Display.

Q2. Write a code to design a double digit decimal counter that counts up from 00
to 20 and back to 00 indefinitely.
C Code

# include < reg51 .h >

# define SEGMENT_PORT P0
# define CONTROL_PORT P2

unsigned char segment_code [] = {0 x3F , 0 x06 , 0 x5B , 0 x4F , 0 x66 , 0


x6D , 0 x7D , 0 x07 , 0 x7F , 0 x6F };

void delay ( unsigned int time ) {


unsigned int i , j;
for ( i = 0; i < time ; i ++) {
for ( j = 0; j < 150; j ++) ;
}
}

void main () {
unsigned char tens = 0;
unsigned char units = 0;
unsigned char direction = 1;

while (1) {
CONTROL_PORT = 0 x01 ;
SEGMENT_PORT = segment_code [ tens ];
delay (5) ;

CONTROL_PORT = 0 x02 ;
SEGMENT_PORT = segment_code [ units ];
delay (5) ;

if ( direction == 1) {
units ++;
if ( units > 9) {
units = 0;
tens ++;
if ( tens >= 2) {
tens = 2;
units = 0;
direction = 0;
}
}
} else {
units - -;
if ( units == 0 xFF ) {
units = 9;

5
tens - -;
if ( tens == 0 xFF ) {
tens = 0;
units = 0;
direction = 1;
}
}
}

delay (50) ;
}
}

Q3. Write a code to display the first (N) numbers of the Fibonacci sequence, where
the number (N) must be stored in a memory location and can be any integer from
1 to 10. The sequence should repeat indefinitely.
C Code

# include < reg51 .h >

# define SEGMENT_PORT P0
# define CONTROL_PORT P2

unsigned char segment_code [] = {0 x3F , 0 x06 , 0 x5B , 0 x4F , 0 x66 , 0


x6D , 0 x7D , 0 x07 , 0 x7F , 0 x6F };

void delay ( unsigned int time ) {


unsigned int i , j;
for ( i = 0; i < time ; i ++) {
for ( j = 0; j < 50; j ++) ;
}
}

void display_number ( unsigned char number ) {


SEGMENT_PORT = segment_code [ number ];
}

void main () {
unsigned char N = 10;
unsigned char fib [10];
unsigned char i ;

fib [0] = 0;
fib [1] = 1;

for ( i = 2; i < N ; i ++) {


fib [ i ] = fib [ i - 1] + fib [ i - 2];
}

while (1) {
for ( i = 0; i < N ; i ++) {

6
CONTROL_PORT = 0 x01 ;
display_number ( fib [ i ] / 10) ;
delay (3) ;

CONTROL_PORT = 0 x02 ;
display_number ( fib [ i ] % 10) ;
delay (3) ;

delay (400) ;
}
}
}

Q4. Write a code to generate the multiplication table of a number (N) stored in
a memory location which can be any integer from 1 to 10. Repeat the sequence
indefinitely.
C Code

# include < reg51 .h >

# define SEGMENT_PORT P0
# define CONTROL_PORT P2

unsigned char segment_code [] = {0 x3F , 0 x06 , 0 x5B , 0 x4F , 0 x66 , 0


x6D , 0 x7D , 0 x07 , 0 x7F , 0 x6F };

void delay ( unsigned int time ) {


unsigned int i , j;
for ( i = 0; i < time ; i ++) {
for ( j = 0; j < 120; j ++) ;
}
}

void display_number ( unsigned char number ) {


SEGMENT_PORT = segment_code [ number ];
}

void main () {
unsigned char N = 5;
unsigned char i , result ;

while (1) {
for ( i = 1; i <= 10; i ++) {
result = N * i ;

CONTROL_PORT = 0 x01 ;
display_number ( result / 10) ;
delay (3) ;

CONTROL_PORT = 0 x02 ;
display_number ( result % 10) ;

7
delay (3) ;

delay (200) ;
}
}
}

Q5. Write a code to display the roll numbers of your lab group members one
by one in static format. Each student roll number should be of four characters.
Display of student roll numbers should repeat indefinitely.
C Code

# include < reg51 .h >

# define SEGMENT_PORT P0
# define CONTROL_PORT P2

unsigned char segment_code [] = {0 x3F , 0 x06 , 0 x5B , 0 x4F , 0 x66 , 0


x6D , 0 x7D , 0 x07 , 0 x7F , 0 x6F };

unsigned char roll_numbers [4][4] = {


{0 , 0 , 0 , 9} ,
{0 , 0 , 1 , 0} ,
{0 , 0 , 1 , 1} ,
{0 , 0 , 1 , 2}
};

void delay ( unsigned int time ) {


unsigned int i , j;
for ( i = 0; i < time ; i ++) {
for ( j = 0; j < 120; j ++) ;
}
}

void display_number ( unsigned char number ) {


SEGMENT_PORT = segment_code [ number ];
}

void main () {
unsigned char i , j ;

while (1) {
for ( i = 0; i < 4; i ++) {
for ( j = 0; j < 4; j ++) {
CONTROL_PORT = 0 x01 << j ;
display_number ( roll_numbers [ i ][ j ]) ;
delay (3) ;
}
delay (300) ;
}
}

8
}

Q6. Write a code to display the roll numbers of your lab group members in scrolling
format, separated by using decimal point. Roll numbers should be scrolled towards
the left and is repeated indefinitely.
C Code

# include < reg51 .h >

# define SEGMENT_PORT P0
# define CONTROL_PORT P2

unsigned char segment_code [] = {0 x3F , 0 x06 , 0 x5B , 0 x4F , 0 x66 , 0


x6D , 0 x7D , 0 x07 , 0 x7F , 0 x6F , 0 x80 };

unsigned char roll_numbers [] = {1 , 0 , 0 x80 , 1 , 1 , 0 x80 , 1 , 2 , 0


x80 };

void delay ( unsigned int time ) {


unsigned int i , j;
for ( i = 0; i < time ; i ++) {
for ( j = 0; j < 120; j ++) ;
}
}

void display_number ( unsigned char number ) {


SEGMENT_PORT = segment_code [ number ];
}

void main () {
unsigned char i , j , k ;

while (1) {
for ( i = 0; i < 9; i ++) {
for ( j = 0; j < 4; j ++) {
CONTROL_PORT = 0 x01 << j ;
display_number ( roll_numbers [( i + j ) % 9]) ;
delay (30) ;
}
delay (300) ;
}
}
}

Discussion
In this lab, we interfaced a 7-segment LED display with an 8051 microcontroller using both non-multiplexed and
multiplexed methods. The non-multiplexed approach, though simple, uses more pins, making it inefficient for
displaying multiple digits. In contrast, the multiplexed method uses fewer pins and achieves multi-digit displays
by rapidly switching between displays, though it requires precise timing to avoid flicker. We also explored static
and scrolling outputs, demonstrating the display’s versatility for applications like clocks and counters.The
comparison between using C programming and the hardware design process highlighted the advantages of C

9
for managing complex code. Timing synchronization, especially in multiplexing, was a challenge, but using C
allowed us to manage the logic more effectively, reducing the complexity of controlling multi-digit displays.

Conclusion
In conclusion, this lab provided insights into using a 7-segment LED display with a microcontroller, showing
that multiplexing is more efficient for multi-digit displays than non-multiplexing. C programming allowed us
to efficiently manage the timing and logic needed for the multiplexed approach. The experience highlighted the
importance of timing in embedded programming and demonstrated the versatility of microcontroller-controlled
displays for dynamic applications.

10

You might also like