Experiment 7
Experiment 7
Objective
The objective of this lab is to give you a first foot in the door exposure to the programming of
I/O, which when executed by the microcontroller (TI LM4F120, an ARM Cortex-M4) simply
blinks LED located on the development board.
Introduction to GPIO
A GPIO pin is a pin that can be configured through software to be either a digital input or a
digital output. GPIO outputs let you translate logical values within your program to voltage
values on output pins and it is these voltage outputs that help your microcontroller exert control
over the system into which it is embedded.
Configuring Peripherals
The fundamental initialization steps required to utilize any of the peripheral are:
LED flashing code is implemented using an infinite loop which toggles bit 28 of the address
0x2009C020 after a certain delay. The delay is implemented using the loop that just increments
the loop counter until the condition is satisfied. Figure – shows the two different approaches.
The shorter flowchart leads to smaller code size and the longer flowchart translates to an inef-
ficient code.
57
58 CHAPTER 7. DIGITAL INPUT/OUTPUT INTERFACING AND PROGRAMMING
Start Start
Delay loop
Increment/ No Counter Increment/ No Counter
Decrement reached final Decrement reached final
the counter value? the counter value?
Yes Yes
Increment/ No Counter
Decrement reached final
the counter value?
Yes
Turn LED on
The overall structure of the program is shown in listing below. The program begins by defining
macros for the relevant register addresses. The main routine follows the initialization steps
described above then enters a loop in which it toggles an LED and waits for sometime.
#i n c l u d e ” l m 4 f 1 2 0 h 5 q r . h”
i n t main ( v o i d ) {
// Enable p e r i p h e r a l s
. . . (1) . . .
// C o n f i g u r e p i n s
. . . (2) . . .
while (1) {
// Turn ON LED
... (3)...
// Delay f o r a b i t
. . . (4) . . .
// Turn OFF LED
. . . (5) . . .
}
}
59
Where is LED?
The Stellaris LaunchPad comes with an RGB LED. The LED can be configured for use in any
custom application. The following table 6.1 shows how the LED is connected to the pins on the
microcontroller. Figure 6.1 shows the physical connection of the LED.
LED Configuration
We will follow the steps stated above to configure the on-board LED.
The RCGCGPIO register provides software the capability to enable and disable GPIO modules
in Run mode. When enabled, a module is provided a clock and accesses to module registers are
allowed. When disabled, the clock is disabled to save power and accesses to module registers
generate a bus fault. This register is shown in Figure 6.2. The clock can be enabled for the
GPIO port F by asserting the 6th bit of RCGGPIO register.
Following command can be used to enable clock signal for GPIO port F
Figure 7.3: General-Purpose Input/Output Run Mode Clock Gating Control (RCGCGPIO)
After enabling the clocks, it is necessary to configure any required pins. In this case, a single
pin (PF3) must be configured as an output. To use the pin as a digital input or output, the
corresponding bit in the GPIODEN register must be set and then setting a bit in the GPIODIR
register configures the corresponding pin to be an output.
The commands used to set the corresponding bits in GPIODEN and GPIODIR registers are
given as follows
After configuring the LED as an output, now we want to toggle it after regular intervals. LED
can be turned ON and OFF by setting and resetting the corresponding bits in the GPIODATA
register.
Introducing a Delay
We cannot observe the toggling of LED because of very high frequency. We introduce a delay
loop in order to observe the toggle sequence of the LED. The syntax for the loop is shown in
the following figure
int counter = 0;
while ( counter < 200000){ // ( 4 )
++c o u n t e r ;
}
Source Code
The complete C and assembly language code for the program is given as follows
C language Code
50 // Delay f o r a b i t .
51
52 f o r ( ulLoop = 0 ; ulLoop < DELAY; ulLoop++)
53 {
54 }
55 }
56 }
; Directives
PRESERVE8
THUMB ; Marks t h e THUMB mode o f o p e r a t i o n
; I n i t i a l i z i n g some c o n s t a n t s
SYSCTL RCGCGPIO R EQU 0 x400FE608
main
NOP
; S e t t h e d i r e c t i o n f o r PORT F
LDR R1 , =GPIO PORTF DIR R
LDR R0 , [ R1 ]
ORR R0 , #GPIO PORTF PIN3 EN
STR R0 , [ R1 ]
; D i g i t a l enable f o r PORT F
LDR R1 , =GPIO PORTF DEN R
LDR R0 , [ R1 ]
ORR R0 , #GPIO PORTF PIN3 EN
STR R0 , [ R1 ]
; I n f i n i t e l o o p LED flash
LED flash
; Delay l o o p
LDR R5 , =DELAY
delay1
SUBS R5,#1
BNE delay1
; Delay l o o p
LDR R5 , =DELAY
delay2
SUBS R5,#1
BNE delay2
B LED flash
ALIGN
Exercises