Parallel Interfacing: Interfacing Seven Segment Display

advertisement
Experiment 8
Parallel Interfacing: Interfacing Seven
Segment Display
Objective
This lab provides an opportunity to learn about the use of a microcontroller (MCU) and its interfacing to external devices. We will use ARM Cortex-M4F based Stellaris LM4F120 LaunchPad
to drive a 7-segment display.
7-Segment Display Construction
A 7-Segment display is a useful electronic component use to produce numeric, alphabetic and
some non-alphabetic symbols using a specific arrangement of LEDs as shown in Figure 8.1.
Figure 8.1: 7 Segment LED Display
A seven segment display consists of seven LEDs arranged in the form of a squarish 8 slightly
inclined to the right and a single LED as the dot character. Different characters can be displayed
by selectively glowing the required LED segments. Seven segment displays are of two types,
common cathode and common anode. In common cathode type, the cathode of all LEDs are
tied together to a single terminal which is usually labeled as com and the anode of all LEDs
are left alone as individual pins labeled as a, b, c, d, e, f, g & dot. In common anode type, the
anode of all LEDs are tied together as a single terminal and cathodes are left alone as individual
pins. Both the configurations are shown in Figure 8.2
In this experiment, a GPIO port on the LM4F120H5QR MCU transmits 8 bits of data. These
bits are applied to the seven-segment display to cause it to illuminate the appropriate segments
to display the proper number or character. The seven segment used in this experiment is of
66
67
Figure 8.2: Common Anode and Common Cathode Configurations
common-cathode type. Segment intensity is dependent on the current flow and should not
exceed the limit of the segment.
Digit Drive Pattern
Digit drive pattern of a seven
display is simply
the
different
logic combinations
®
How tosegment
InterfaceLED
a Seven-Segment
Display
with
the Z8 Encore!
MCU
of its terminals. For a certain character, a combination of LED ON and LED OFF is generated
to display the character for a short period of time. The pattern is loaded alternately to display
other characters. For example, to display the number 2, LEDs a, b, d, e, and g are illuminated.
Table 8.3 provides the display pattern for numbers(0-9) and characters A through F.
Table 1. Display Pattern for Characters 0-F
Characters
DP
G
F
E
D
C
B
A
Hexadecimal
0
0
0
1
1
1
1
1
1
3F
1
0
0
0
0
0
1
1
0
06
2
0
1
0
1
1
0
1
1
5B
3
0
1
0
0
1
1
1
1
4F
4
0
1
1
0
0
1
1
0
66
5
0
1
1
0
1
1
0
1
6D
6
0
1
1
1
1
1
0
1
7D
7
0
0
0
0
0
1
1
1
07
8
0
1
1
1
1
1
1
1
7F
9
0
1
1
0
1
1
1
1
6F
A
0
1
1
1
0
1
1
1
77
B
0
1
1
1
1
1
0
0
7C
C
0
0
1
1
1
0
0
1
39
D
0
1
0
1
1
1
1
0
5E
E
0
1
1
1
1
0
0
1
79
F
0
1
1
1
0
0
0
1
71
Figure
8.3: 7 Segment
Decoder
Table
To control four 7-segment displays,
multiplexing
can
duces
the illusion
that all four 7-segment displays are
reduce the number of GPIO pins required.
turned on.
In this setup, the four multiplexed seven-segment displays are turned on one at a time to output the appropriate display. Because of the visual phenomenon
known as persistence of vision, rapid switching of the
seven-segment display can appear as if all four dis-
EE/CME 392 Laboratory
4-2
Part 1a – Starter project (< 1 hour)
Obtain from
technicians INTERFACING:
one Stellaris® LM4F120
LaunchPad and
one four-digit
7-segment
68CHAPTER
8. the
PARALLEL
INTERFACING
SEVEN
SEGMENT
DISPLAY
display module. Use the USB cable to connect the lab computer’s USB port to the Power/ICDI
micro-USB port at the top of the LaunchPad. Make sure the slide switch in the top left corner is
Multiplexing
theposition.
Seven Segments
in the right hand
In order to verify the LaunchPad circuit function and to become familiar with loading a program
To control
7-segment
multiplexing
can reduce
the on
number
of GPIO
pins
required.
into thefour
LaunchPad,
youdisplays,
should run
the starter project
is provided
the course
website.
Using
lab computers
in 2C61 or 2C80,
download the
projectare
(starter.zip),
it and
runto
theoutput
In thisthesetup,
the four multiplexed
seven-segment
displays
turned onunzip
one at
a time
uVision4 project named “start_0.uvproj”.
Within uVision4, go to “Projects > Options for
the appropriate
display.
Becausetab,
of make
the visual
phenomenon
known
as persistence
vision,
Target”. Under
the “utilities”
sure that
Stellaris ICDI
is listed
as the target of
driver
for rapid
flash programming.
Close the window
the code
into the
flash
memory
switching
of the seven-segment
display and
can“Load”
appear(download)
as if all four
displays
are
turned
on.of
the MCU. To program the LaunchPad’s MCU, the Power Select Switch must remain in “Debug”
position and the USB cable must remain connected since the LaunchPad is powered from it. For
other power options, consult p.11 of the user manual [1]. Press the RESET button (must do after
Launchpad
Interface
every download)
then press SW1 (active low) and observe the LED flashing (active high).
Now study the starter code (starter_0.c) and the comments after every line – it will help you
code, syntax and configuration of LM4F120 MCU. The code is to simply
Beforebetter
you understand
build yourthecircuit,
you should carefully note the non-sequential pin-out diagram of
turn on the onboard red, blue and green LEDs in sequence when the switch (SW1) is pressed.
the LaunchPad
shown
Figure
8.4. Although
The switch and
LEDsinare
all connected
to Port F. the LM4F120H5QR MCU has 43 GPIO, only
35 of LaunchPad
them are available
through
are:and
6 pins
Port
A (PA2-PA7),
Interface:
Before the
youLaunchPad.
go to the nextThey
section
build ofyour
circuit,
you should8 pins
carefully
note the non-sequential
pin-out
diagram of the
LaunchPad
in Fig. 1. Although
of Port
B (PB0-PB7),
4 pins of Port
C (PC4-PC7),
6 pins
of Portshown
D (PD0-PD3,
PD6-PD7), 6
the LM4F120H5QR MCU has 43 GPIO, only 35 of them are available through the LaunchPad.
pins of
Port
and
5 pins of8Port
F Port
(PF0-PF4).
In addition,
there
are two ground,
They
are:E6(PE0-PE5)
pins of Port A
(PA2-PA7),
pins of
B (PB0-PB7),
4 pins of Port
C (PC4-PC7),
6 pinsone
of Port
(PD0-PD3,
pins of
Port E (PE0-PE5),
and 5 pins of Port F (PF0one 3.3V,
5V D
(VBUS),
andPD6-PD7),
one reset6pins
available
on the LaunchPad.
PF4). In addition, there are two ground, one 3.3V, one 5V (VBUS), and one reset pins available
on the LaunchPad.
Pins PC0-PC3
are left off as they are used for JTAG debugging. Pins PA0-PA1 are also left off
Pins
PC0-PC3
arecreate
left offaasvirtual
they areCOM
used for
JTAG
debugging.
PA0-PA1 are
off pins
as they are used to
port
to connect
thePins
LaunchPad
to also
PC.left
These
as they are used to create a virtual COM port to connect the LaunchPad to PC. These pins should
should
be used
for regular
I/O purpose.
notnot
be used
for regular
I/O purpose.
J1
J3
J4
J2
3.3V
VBUS
PF2
GND
PB5
GND
PF3
PB 2
PB0
PD0
PB3
PE 0
PB1
PD1
PC4
PF0
PE4
PD2
PC5
RST
PE5
PD3
PC6
PB 7
PB4
PE 1
PC7
PB 6
PA5
PE 2
PD6
PA 4
PA6
PE 3
PD7
PA 3
PA7
PF1
PF4
PA 2
Fig. 1 Header pins on the LaunchPad (EK-LM4F120XL)
Figure 8.4: Header Pins on the Launchpad (EK-LM4f120H5QR)
2
Display System Using Seven Segments
In this experiment, we will program the LaunchPad and four-digit 7-segment display module
to display ”UOFS”. In addition, when SW1 is pressed, the display is to flash the letters ”H”,
”E”, ”L”, ”O” then settle back to ”UOFS”.
Note carefully the diagram of the display module. Connect the circuit as shown in Figure 8.5.
The LaunchPad board is sufficient to supply +5V to Vdd of the 7-segment display module. The
display power pins are connected directly to VBUS and GND of J3 On the expansion board.
The experiment uses C language to program the MCU. A program for this task is included in
“L”, “L”,
off with a
urned
Note caref
2 and
connect t
ent
69
display
now,
you may
the
the lab
manual.
The display latches are connected to PA2-PA5 and the data pins areplish
connected
MCU.
Us
the task. Using the Latch Enable (LE) pin, your program can update the displays are
to PB0-PB7.
digits one
connected
splay
at a digits
time.one
3.3V
5V
VBUS
(5V)
4
Q7SD
2
PB7
3
LM4F120
LaunchPad
PA2
PA3
PA4
PA5
PB0
PB1
..
..
..
..
1
VDD
VDD
DIG1
DIG2
DIG3
DIG4
A
B
C
D
E
F
G
DP
GND
GND
SW1
PF4
GND
Figure 8.5: Connection to Seven Segment Display
Part 1c
In this expe
Fig. 2). T
Source
Code
For a full
LM4F
n in
d.
of
will
o the
( ∗ ( ( v o l a t i l e u n s i g n e d l o n g ∗ ) 0 x400FE608
))
The
Consult
the datasheet for the proper understanding of this code.
registers
use only
1 #d e corres
f i n e SYSCTL RCGCGPIO R
2
3 #d e f i n e GPIO PORTB DATA R
4 #d e f i n e GPIO PORTB DIR R
5 #d e f i n e GPIO PORTB AFSEL R
6 #d e f i n e GPIO PORTB DEN R
7 #d e f i n e GPIO PORTB PCTL R
(∗((
(∗((
(∗((
(∗((
(∗((
volatile
volatile
volatile
volatile
volatile
unsigned
unsigned
unsigned
unsigned
unsigned
long
long
long
long
long
3
∗ ) 0x400053FC
))
∗ ) 0 x40005400 ) )
∗ ) 0 x40005420 ) )
∗ ) 0 x4000551C ) )
∗ ) 0 x4000552C ) )
(∗((
(∗((
(∗((
(∗((
(∗((
volatile
volatile
volatile
volatile
volatile
unsigned
unsigned
unsigned
unsigned
unsigned
long
long
long
long
long
∗ ) 0x400043FC ) )
∗ ) 0 x40004400 ) )
∗ ) 0 x40004420 ) )
∗ ) 0 x4000451C ) )
∗ ) 0 x4000452C ) )
(∗((
(∗((
(∗((
(∗((
(∗((
volatile
volatile
volatile
volatile
volatile
unsigned
unsigned
unsigned
unsigned
unsigned
long
long
long
long
long
∗ ) 0x400253FC ) )
∗ ) 0 x40025400 ) )
∗ ) 0 x40025420 ) )
∗ ) 0 x4002551C ) )
∗ ) 0 x4002552C ) )
8
9 #d e f i n e GPIO PORTA DATA R
10 #d e f i n e GPIO PORTA DIR R
11 #d e f i n e GPIO PORTA AFSEL R
12 #d e f i n e GPIO PORTA DEN R
13 #d e f i n e GPIO PORTA PCTL R
14
15 #d e f i n e GPIO PORTF DATA R
16 #d e f i n e GPIO PORTF DIR R
17 #d e f i n e GPIO PORTF AFSEL R
18 #d e f i n e GPIO PORTF DEN R
19 #d e f i n e GPIO PORTF PCTL R
70CHAPTER 8. PARALLEL INTERFACING: INTERFACING SEVEN SEGMENT DISPLAY
20 #d e f i n e GPIO PORTF PUR R
( ∗ ( ( v o l a t i l e u n s i g n e d l o n g ∗ ) 0 x40025510 ) )
21
22 #d e f i n e SEG 1
23 #d e f i n e SEG 2
24 #d e f i n e SEG 3
25 #d e f i n e SEG 4
0xFB
0xF7
0xEF
0xDF
26
27 v o i d i n i t g p i o ( v o i d ) ;
28 v o i d w a i t f o r k e y ( v o i d ) ;
29 v o i d d i s p l a y u o f s ( v o i d ) ;
30 v o i d d i s p l a y h e l l o ( v o i d ) ;
31 v o i d d e l a y ( u n s i g n e d l o n g v a l u e ) ;
32
33 // PortB p i n s :
34 //
76543210
1 edcbafg
35
36 // Look up t a b l e f o r UOFS
37 c o n s t c h a r l u t u o f s
38
39
40
41
[ 4 ] = {0xC1 ,
0xC0 ,
0x8E ,
0 x92
};
//
//
//
//
U
O
F
S
11000001
11000000
10001110
10010010
//H
//E
//L
//L
//O
10001001
10000110
11000111
11000111
11000000
42
43
44 // Look up t a b l e f o r HELLO
45 c o n s t c h a r l u t h e l l o
46
47
48
49
50
[ 5 ] = {0 x89 ,
0 x86 ,
0xC7 ,
0xC7 ,
0xC0
};
51
52 // I n i t i a l i z a t i o n
53
54
55
56
function for ports
void i n i t g p i o ( void ) {
v o l a t i l e u n s i g n e d l o n g d e l a y c l k ; // d e l a y f o r c l o c k , must have 3 s y s
clock delay
SYSCTL RCGCGPIO R |= 0 x23 ;
d e l a y c l k = SYSCTL RCGCGPIO R ;
57
58
59
60
61
GPIO PORTB PCTL R &= 0 x00000000 ;
GPIO PORTB AFSEL R &= ˜0xFF ;
GPIO PORTB DIR R |= 0xFF ;
GPIO PORTB DEN R |= 0xFF ;
62
63
64
65
66
GPIO PORTA PCTL R &= 0 x00000000 ;
GPIO PORTA AFSEL R &= ˜0x3C ;
GPIO PORTA DIR R |= 0x3C ;
GPIO PORTA DEN R |= 0x3C ;
71
67
GPIO PORTF PCTL R &= 0xFFF0FF0F ;
GPIO PORTF AFSEL R &= ˜0 x12 ;
GPIO PORTF DEN R |= 0 x12 ;
GPIO PORTF DIR R |= 0 x02 ;
GPIO PORTF PUR R |= 0 x10 ;
68
69
70
71
72
73
74 }
75
76 // o t h e r f u n c t i o n s
77 v o i d w a i t f o r k e y ( v o i d ) {
78
79
80
81
82
83
w h i l e (GPIO PORTF DATA R & 0 x10 ) ; // w a i t f o r SW1 p r e s s , p o l l low
GPIO PORTF DATA R |= 0 x02 ; // RED on , make PF1 h i g h
delay (1000000) ;
GPIO PORTF DATA R &= ˜0 x02 ; // RED o f f , make PF1 low
delay (1000000) ;
84 }
85
86 // d i s p l a y s UOFS on d i s p l a y board
87
88 v o i d d i s p l a y u o f s ( v o i d ) {
89
90
w h i l e (GPIO PORTF DATA R & 0 x10 )
{
91
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t u o f s [ 0 ] ;
GPIO PORTA DATA R = SEG 1 ;
delay (10000) ;
92
93
94
95
96
97
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t u o f s [ 1 ] ;
GPIO PORTA DATA R = SEG 2 ;
delay (10000) ;
98
99
100
101
102
103
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t u o f s [ 2 ] ;
GPIO PORTA DATA R = SEG 3 ;
delay (10000) ;
104
105
106
107
108
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t u o f s [ 3 ] ;
GPIO PORTA DATA R = SEG 4 ;
delay (10000) ;
109
110
111
112
113
114 }
}
72CHAPTER 8. PARALLEL INTERFACING: INTERFACING SEVEN SEGMENT DISPLAY
115
116 // d i s p l a y s HELO on d i s p l a y board
117
118 v o i d d i s p l a y h e l o ( v o i d ) {
119
120
121
122
123
124
125
i n t i =0;
w h i l e ( i <100)
{
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t h e l l o [ 0 ] ;
GPIO PORTA DATA R = SEG 1 ;
delay (10000) ;
126
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t h e l l o [ 1 ] ;
GPIO PORTA DATA R = SEG 2 ;
delay (10000) ;
127
128
129
130
131
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t h e l l o [ 2 ] ;
GPIO PORTA DATA R = SEG 3 ;
delay (10000) ;
132
133
134
135
136
GPIO PORTA DATA R = 0xFF ;
GPIO PORTB DATA R = l u t h e l l o [ 4 ] ;
GPIO PORTA DATA R = SEG 4 ;
delay (10000) ;
i ++;
137
138
139
140
141
142
}
143 }
144
145 v o i d d e l a y ( u n s i g n e d l o n g v a l u e ) {
146
147
unsigned long i ;
f o r ( i = 0 ; i < v a l u e ; i ++) ;
148 }
149
150 i n t main ( v o i d ) {
151
152
init gpio () ;
153
154
155
156
157
158
159 }
while (1) {
display uofs () ;
w a i t f o r k e y ( ) ; // p o l l i n g SW1 (PF4)
display helo () ;
}
Download