You are on page 1of 21

Microprocessor Fundamentals

Topic 10
7-Segment LEDs

Objectives
Discuss 7-Segment LEDs
Turn on specific LED segments using code
Common-Anode, Common-Cathode

Develop the 7-Segment Code for the LEDs


Show connections from AVR to LEDs
Develop an example flowchart
Describe bit masking

1/4/2010

7-Segment LEDs
Two categories:
Common-cathode
Common-anode
Common-Anode:
Logic 0 turns LED on

+ 5v

To Port
To Port

Common-Cathode:
Logic 1 turns LED on

1/4/2010

7-Segment LEDs
A 7-Segment LED could be attached as shown
Atmega128

PB0
PB1
PB2
PB3

b
c
d

120
a

f
g
d
e

PB5

PB7

PB4

PB6

1/4/2010

e
d

7-Segment LEDs
If 0b00111111 was output on Port B, the following
segments would be on
Atmega128

PB4

PB5

PB0
PB1
PB2
PB3

PB6
PB7

120
a

g
d
e
f

e
d

What type of 7-Segment LED would this be?


1/4/2010

7-Segment LEDs
If 0b00111111 was output on Port B, the following
segments would be on
Atmega128

PB4

PB5

PB0
PB1
PB2
PB3

PB6
PB7

120
a

g
d
e
f

e
d

Common-Cathode
1/4/2010

Program Specifications
Write an assembly language program to:
Read two 8 bit binary numbers from Port A and Port B
Use pull-up resistors

Use Port E, pin 0 to let the AVR know the numbers are
ready to be read
A 0 on pin 0 means NOT READY, a 1 on pin 0 mean READY

AND the two numbers


Output the low nibble (digit) to Port C
Determine the 7-segment code for the digit
Connect a common-cathode 7-segment LED to display the digit

Output the high nibble (digit) to Port D


Determine the 7-segment code for the digit
Connect a common-cathode 7-segment LED to display the digit
1/4/2010

Flowchart:
Start
Port Initializations
A

Get codes for high


& low nibbles

Read b0 Port E
b0 = 1

Output data to Ports


C & D (LEDs)
Read b0 of Port E

Read ports A & B


A
AND Numbers

This is the simpler flowchart


Separate high
& low nibbles

1/4/2010

Flowchart:
Start
Port Initializations
A

Get codes for high


& low nibbles

Read b0 Port E
b0 = 1

Output data to Ports


C & D (LEDs)
Read b0 of Port E

Read ports A & B


A
AND Numbers

More detail could be added to these blocks


Separate high
& low nibbles

1/4/2010

Flowchart:
Start
Port Initializations
A

Get codes for high


& low nibbles

Read b0 Port E
b0 = 1

Output data to Ports


C & D (LEDs)
Read b0 of Port E

Read ports A & B


A
AND Numbers

Separate high
& low nibbles

1/4/2010

00
FF
00
FF
00
FF
FF
00
FF
00

DDRA
PortA: use pull-ups
DDRB
PortB: use pull-ups
DDRE:
PortE: use pull-ups
DDRC
PortC: set initial values LEDs off
DDRD
PortD: set initial values LEDs off
10

Flowchart:
Start
Port Initializations
A

Get codes for high


& low nibbles

Read b0 Port E
b0 = 1

Output data to Ports


C & D (LEDs)
Read b0 of Port E

Read ports A & B


A
AND Numbers

Port A
Port B

r17
r18

Separate high
& low nibbles
Copy r18 to r17 (to hold Low) and r18 (to hold high)
AND r17 with $0F (masks off high bits)
LSR r18 4 times (shift high bits to low bits)

1/4/2010

11

Flowchart:
Start
Port Initializations
A

Get codes for high


& low nibbles

Read b0 Port E
b0 = 1

Output data to Ports


C & D (LEDs)
Read b0 of Port E

We need to figure out these codes

Read ports A & B


A
AND Numbers

Separate high
& low nibbles

1/4/2010

12

7-Segment Codes:
a
f

b
g

0 = 0b001 11111
c

e
d

a
f

b
g

1 = 0b00000110
c

e
d

a
f

b
g

2 = 0b01011011

e
d

a
f

b
g

3 = 0b01001111

e
d

1/4/2010

13

7-Segment Codes:
a
f

b
g

0 = 0b001 11111
c

e
d

a
f

b
g

1 = 0b00000110

In-Class Exercise:

e
d

Determine the codes for the remaining


digits: 4 F

a
f

b
g

2 = 0b01011011

e
d

a
f

b
g

3 = 0b01001111

e
d

1/4/2010

14

7-Segment Codes:
a

a
f

b
g

4 = 0b01100110
c

g
c

b
g

6 = 0b01111101

1/4/2010

A = 0b01110111

7 = 0b00000111

f
g

c
d

E = 0b01111001

e
d

b
g

a
b

D = 0b01011110
c

b
g

9 = 0b01101111

C = 0b00111001
c

5 = 0b01101101

b
g

a
f

f
e

8 = 0b01111111
c

B = 0b01111100

b
g

F = 0b01110001

e
d

15

Flowchart:
Start
Port Initializations
A

Get codes for high


& low nibbles

Read b0 Port E
b0 = 1

Output data to Ports


C & D (LEDs)
Read b0 of Port E

Read ports A & B


A
AND Numbers

Look up the codes for each digit in


the look up table. This assumes a
look up table was developed as part
of the initialization or placed in
memory with the .db assembler
directive

Separate high
& low nibbles

1/4/2010

16

Flowchart:
Start
00
FF
00
FF
00
FF
FF
00
FF
00

Port Initializations
DDRA
PortA: use pull-ups
DDRB
PortB: use pull-ups
DDRE:
PortE: use pull-ups
DDRC
PortC: set initial values LEDs off
DDRD
PortD: set initial values LEDs off

AND Numbers
Copy r18 to r17 (to hold Low) and r18 (to hold high)
AND r17 with $0F (masks off high bits)
LSR r18 4 times (shift high bits to low bits)

Get codes for high


& low nibbles from
the lookup table

Read b0 Port E
N

b0 = 1

Output data to Ports


C & D (LEDs)
Read b0 of Port E

Y
Port A
Port B

1/4/2010

r17
r18

17

Two LEDs
What happens if you need to drive two 7-segment
LEDs, but dont have enough ports?
Assume you do have one more output pin available
Port C, bit 0
Assume it has been configured as an output
The following circuit would work ..

7-Segment LEDs
Your program must output the code for LED 1 on Port B and then select
LED 1 by placing a 0 on PC0
Then your program must output the code for LED 2 on Port B and then
select LED 2 by placing a 1 on PC0
You must then continue to alternately light the two LEDs
Atmega128

PB4

PB5

PB0
PB1
PB2
PB3

PB6

PC0

1/4/2010

PB7

120
a

f
g

g
d

d
e
f

e
d

e
f

e
d

LED 1

LED 2

19

The Program:
The program will be assigned as homework
The program and circuit connections will be assigned
as a lab

1/4/2010

20

Summary
In this topic we:
Discussed 7-Segment LEDs
Turned on specific LED segments using code
Common-Anode, Common-Cathode

Developed the 7-Segment Code for the LEDs


Showed the connections from AVR to LEDs
Developed an example flowchart
Described bit masking
The program will be written for homework
The program will be tested on the development board in the
lab
1/4/2010

21

You might also like