7-Segment LEDs

advertisement
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
2
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
3
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
f
f
PB7
.
b
c
e
g
a
b
PB4
PB6
1/4/2010
a
c
e
d
g
.
4
7-Segment LEDs
• If 0b00111111 was output on Port B, the following
segments would be on
Atmega128
a
1
b
1
c
1
d
1
PB4
e
1
PB5
f
1
g
0
.
0
PB0
PB1
PB2
PB3
PB6
PB7
120 Ώ
a
a
b
f
b
c
g
d
e
f
c
e
d
g
.
What type of 7-Segment LED would this be?
1/4/2010
5
7-Segment LEDs
• If 0b00111111 was output on Port B, the following
segments would be on
Atmega128
a
1
b
1
c
1
d
1
PB4
e
1
PB5
f
1
g
0
.
0
PB0
PB1
PB2
PB3
PB6
PB7
120 Ώ
a
a
b
f
b
c
g
d
e
f
c
e
d
g
.
Common-Cathode
1/4/2010
6
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
7
Flowchart:
Start
Port Initializations
A
Get codes for high
& low nibbles
Read b0 Port E
b0 = 1
N
Y
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
8
Flowchart:
Start
Port Initializations
A
Get codes for high
& low nibbles
Read b0 Port E
b0 = 1
N
Y
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
9
Flowchart:
Start
Port Initializations
A
Get codes for high
& low nibbles
Read b0 Port E
b0 = 1
N
Y
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
N
Y
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
N
Y
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
c
e
d
a
f
b
g
3 = 0b01001111
c
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:
c
e
d
Determine the codes for the remaining
digits: 4 – F
a
f
b
g
2 = 0b01011011
c
e
d
a
f
b
g
3 = 0b01001111
c
e
d
1/4/2010
14
7-Segment Codes:
a
a
f
f
b
g
e
g
4 = 0b01100110
c
g
c
b
g
f
6 = 0b01111101
c
g
1/4/2010
a
A = 0b01110111
c
f
c
7 = 0b00000111
f
g
c
d
E = 0b01111001
c
e
d
b
e
b
g
a
b
D = 0b01011110
c
e
d
a
d
b
e
b
g
d
g
d
e
c
f
a
a
f
9 = 0b01101111
d
d
e
a
b
e
C = 0b00111001
c
d
g
5 = 0b01101101
b
g
a
f
b
f
f
e
d
a
e
8 = 0b01111111
c
e
d
f
a
b
a
B = 0b01111100
f
b
g
F = 0b01110001
c
e
d
15
Flowchart:
Start
Port Initializations
A
Get codes for high
& low nibbles
Read b0 Port E
b0 = 1
N
Y
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
A
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
A
17
Two LEDs
• What happens if you need to drive two 7-segment
LEDs, but don’t 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
a
1
b
1
c
1
d
1
PB4
e
1
PB5
f
1
g
0
PB0
PB1
PB2
PB3
PB6
PC0
1/4/2010
PB7
.
0
120 Ώ
a
a
a
b
a
b
f
b
c
f
g
g
d
d
e
f
c
e
d
e
f
c
e
d
g
g
.
b
c
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
Download