Anda di halaman 1dari 7

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 inter-
facing 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


How tosegment
InterfaceLED display is simply
a Seven-Segment the
Display different
with logic combinations
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
To control four 7-segment displays, 8.3: 7 Segment
multiplexing can Decoder
duces Table
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 dis-
plays are turned on one at a time to output the appro-
priate 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
68CHAPTER 8. the technicians INTERFACING:
PARALLEL one Stellaris LM4F120 LaunchPad and
INTERFACING one four-digit
SEVEN SEGMENT 7-segment
DISPLAY
display module. Use the USB cable to connect the lab computers 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.
in the right hand Seven Segments
In order to verify the LaunchPad circuit function and to become familiar with loading a program
into thefour
To control LaunchPad, youdisplays,
7-segment should run the starter project
multiplexing is provided
can reduce the on the course
number website.
of GPIO pinsUsing
required.
In thisthesetup,
lab computers in 2C61 or 2C80,
the four multiplexed download the
seven-segment projectare
displays (starter.zip),
turned onunzip it and
one at runto
a time theoutput
uVision4 project named start_0.uvproj. Within uVision4, go to Projects > Options for
the appropriate display.
Target. Under Becausetab,
the utilities of make
the visual phenomenon
sure that known
Stellaris ICDI as persistence
is listed as the target of vision,
driver for rapid
flash programming.
switching Close the window
of the seven-segment display and
canLoad
appear(download) the code
as if all four into the
displays flash
are memory
turned on.of
the MCU. To program the LaunchPads 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
Beforebetter
you understand
build yourthecircuit,
code, syntax and configuration of LM4F120 MCU. The code is to simply
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 switch and
the LaunchPad LEDsinare
shown all connected
Figure to Port F. the LM4F120H5QR MCU has 43 GPIO, only
8.4. Although
35 of LaunchPad
them are available through
Interface: Before the
youLaunchPad.
go to the nextThey are:and
section 6 pins
build ofyour
Port A (PA2-PA7),
circuit, you should8 pins
carefully
of Port note the non-sequential
B (PB0-PB7), 4 pins of Portpin-out diagram of the
C (PC4-PC7), LaunchPad
6 pins of Portshown in Fig. 1. Although
D (PD0-PD3, PD6-PD7), 6
the LM4F120H5QR MCU has 43 GPIO, only 35 of them are available through the LaunchPad.
pins of Port
They are:E6(PE0-PE5) and
pins of Port A 5 pins of8Port
(PA2-PA7), F Port
pins of (PF0-PF4). In addition,
B (PB0-PB7), there
4 pins of Port are two ground,
C (PC4-PC7),
6 pinsone
one 3.3V, of Port
5V D (PD0-PD3,
(VBUS), andPD6-PD7),
one reset6pins
pins of Port E (PE0-PE5),
available and 5 pins of Port F (PF0-
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
as they are used to arecreate
left offaasvirtual
they areCOM
used for JTAG
port debugging.
to connect thePins PA0-PA1 are
LaunchPad to also
PC.left off pins
These
as they are used to create a virtual COM port to connect the LaunchPad to PC. These pins should
should
notnot be used
be used for regular
for regular I/O purpose.
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, urned
off with a
Note caref 2 and
connect t ent 69
display now,
you may the
the lab
MCU.manual.
Us The display latches are connected to PA2-PA5 and the data pins areplish
connected
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

5V
3.3V VBUS
(5V)

4
VDD
VDD
PA2 DIG1
PA3 DIG2

3
PA4 DIG3
PA5 DIG4
PB0 A
LM4F120 PB1 B
.. C Q7SD
LaunchPad .. D
.. E
F
..

2
G
PB7 DP
GND
GND

1
SW1
PF4

GND

Figure 8.5: Connection to Seven Segment Display

Part 1c
In this expe n in
Fig. 2). T
Source Code d.
For a full
LM4F of
Consult the datasheet for the proper understanding of this code.
registers will
use only o the
1 #d e corres
f i n e SYSCTL RCGCGPIO R ( ( ( v o l a t i l e u n s i g n e d l o n g ) 0 x400FE608
The ))
2
3 #d e f i n e GPIO PORTB DATA R ((( volatile unsigned long 3
) 0x400053FC ))
4 #d e f i n e GPIO PORTB DIR R ((( volatile unsigned long ) 0 x40005400 ) )
5 #d e f i n e GPIO PORTB AFSEL R ((( volatile unsigned long ) 0 x40005420 ) )
6 #d e f i n e GPIO PORTB DEN R ((( volatile unsigned long ) 0 x4000551C ) )
7 #d e f i n e GPIO PORTB PCTL R ((( volatile unsigned long ) 0 x4000552C ) )
8
9 #d e f i n e GPIO PORTA DATA R ((( volatile unsigned long ) 0x400043FC ) )
10 #d e f i n e GPIO PORTA DIR R ((( volatile unsigned long ) 0 x40004400 ) )
11 #d e f i n e GPIO PORTA AFSEL R ((( volatile unsigned long ) 0 x40004420 ) )
12 #d e f i n e GPIO PORTA DEN R ((( volatile unsigned long ) 0 x4000451C ) )
13 #d e f i n e GPIO PORTA PCTL R ((( volatile unsigned long ) 0 x4000452C ) )
14
15 #d e f i n e GPIO PORTF DATA R ((( volatile unsigned long ) 0x400253FC ) )
16 #d e f i n e GPIO PORTF DIR R ((( volatile unsigned long ) 0 x40025400 ) )
17 #d e f i n e GPIO PORTF AFSEL R ((( volatile unsigned long ) 0 x40025420 ) )
18 #d e f i n e GPIO PORTF DEN R ((( volatile unsigned long ) 0 x4002551C ) )
19 #d e f i n e GPIO PORTF PCTL R ((( volatile unsigned long ) 0 x4002552C ) )
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 0xFB
23 #d e f i n e SEG 2 0xF7
24 #d e f i n e SEG 3 0xEF
25 #d e f i n e SEG 4 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 : 76543210
34 // 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 [ 4 ] = {0xC1 , // U 11000001
38 0xC0 , // O 11000000
39 0x8E , // F 10001110
40 0 x92 // S 10010010
41 };
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 [ 5 ] = {0 x89 , //H 10001001
46 0 x86 , //E 10000110
47 0xC7 , //L 11000111
48 0xC7 , //L 11000111
49 0xC0 //O 11000000
50 };
51
52 // I n i t i a l i z a t i o n function for ports
53 void i n i t g p i o ( void ) {
54 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
55 SYSCTL RCGCGPIO R |= 0 x23 ;
56 d e l a y c l k = SYSCTL RCGCGPIO R ;
57
58 GPIO PORTB PCTL R &= 0 x00000000 ;
59 GPIO PORTB AFSEL R &= 0xFF ;
60 GPIO PORTB DIR R |= 0xFF ;
61 GPIO PORTB DEN R |= 0xFF ;
62
63 GPIO PORTA PCTL R &= 0 x00000000 ;
64 GPIO PORTA AFSEL R &= 0x3C ;
65 GPIO PORTA DIR R |= 0x3C ;
66 GPIO PORTA DEN R |= 0x3C ;
71

67
68 GPIO PORTF PCTL R &= 0xFFF0FF0F ;
69 GPIO PORTF AFSEL R &= 0 x12 ;
70 GPIO PORTF DEN R |= 0 x12 ;
71 GPIO PORTF DIR R |= 0 x02 ;
72 GPIO PORTF PUR R |= 0 x10 ;
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 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
80 GPIO PORTF DATA R |= 0 x02 ; // RED on , make PF1 h i g h
81 delay (1000000) ;
82 GPIO PORTF DATA R &= 0 x02 ; // RED o f f , make PF1 low
83 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 w h i l e (GPIO PORTF DATA R & 0 x10 )
90 {
91
92 GPIO PORTA DATA R = 0xFF ;
93 GPIO PORTB DATA R = l u t u o f s [ 0 ] ;
94 GPIO PORTA DATA R = SEG 1 ;
95 delay (10000) ;
96
97
98 GPIO PORTA DATA R = 0xFF ;
99 GPIO PORTB DATA R = l u t u o f s [ 1 ] ;
100 GPIO PORTA DATA R = SEG 2 ;
101 delay (10000) ;
102
103
104 GPIO PORTA DATA R = 0xFF ;
105 GPIO PORTB DATA R = l u t u o f s [ 2 ] ;
106 GPIO PORTA DATA R = SEG 3 ;
107 delay (10000) ;
108
109 GPIO PORTA DATA R = 0xFF ;
110 GPIO PORTB DATA R = l u t u o f s [ 3 ] ;
111 GPIO PORTA DATA R = SEG 4 ;
112 delay (10000) ;
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 i n t i =0;
120 w h i l e ( i <100)
121 {
122 GPIO PORTA DATA R = 0xFF ;
123 GPIO PORTB DATA R = l u t h e l l o [ 0 ] ;
124 GPIO PORTA DATA R = SEG 1 ;
125 delay (10000) ;
126
127 GPIO PORTA DATA R = 0xFF ;
128 GPIO PORTB DATA R = l u t h e l l o [ 1 ] ;
129 GPIO PORTA DATA R = SEG 2 ;
130 delay (10000) ;
131
132 GPIO PORTA DATA R = 0xFF ;
133 GPIO PORTB DATA R = l u t h e l l o [ 2 ] ;
134 GPIO PORTA DATA R = SEG 3 ;
135 delay (10000) ;
136
137 GPIO PORTA DATA R = 0xFF ;
138 GPIO PORTB DATA R = l u t h e l l o [ 4 ] ;
139 GPIO PORTA DATA R = SEG 4 ;
140 delay (10000) ;
141 i ++;
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 unsigned long i ;
147 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 while (1) {
155 display uofs () ;
156 w a i t f o r k e y ( ) ; // p o l l i n g SW1 (PF4)
157 display helo () ;
158 }
159 }