07 Digital IO Interfacing and Programming
07 Digital IO Interfacing and Programming
Objective
The objective of this lab is to give you a hands-on exposure to the programming of I/O, which
when executed by the microcontroller (TI LM4F120, an ARM Cortex-M4 based microcontroller)
simply blinks LED on the development board.
Introduction to GPIO
A microcontroller communicates with the outside world either by setting the voltage on the
pin high (usually 5V) or low (usually 0V) or reading the voltage level of an input pin as being
high (1) or low (0). We refer to these pins as general purpose input output (GPIO) pins. Any
GPIO pin 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 voltage outputs help your microcontroller exert control over the system in which it is
embedded.
Configuring Peripherals
The fundamental initialization steps required to utilize any of the peripheral are:
The overall structure of this program is illustrated below. The program begins by including the
addresses relevant peripheral registers. Main routine follows the initialization steps described
above and then enters an infinite loop which toggles an LED and waits for sometime.
68
69
i n t main ( v o i d ) {
/ / Enable p e r i p h e r a l s
. . . (1) . . .
/ / Configure pins
. . . (2) . . .
while (1) {
/ / T u r n ON LED
. . . (3) . . .
/ / Delay f o r a b i t
. . . (4) . . .
/ / T u r n OFF LED
. . . (5) . . .
}
}
Where is LED?
The Stellaris LaunchPad has an RGB LED. This LED can be configured for any custom appli-
cation. Table 7.1 shows how the LED is connected to the pins on the microcontroller. Figure 7.1
shows the physical connection of on-board LED.
LED schematic
LED Configuration
Now we will follow the above mentioned steps to configure the on-board LED.
The RCGCGPIO register provides the capability to enable and disable GPIO modules in Run
mode. When enabled, a module is provided a clock and access to module registers. When
disabled, the clock is disabled to save power and accessing a module register generates a bus
fault. This register is shown in Figure 7.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
SYSCTL_RCGCGPIO_R = 0 x 2 0 ; // (1)
After enabling the clock, it is necessary to configure the 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
71
GPIO_PORTF_DIR_R = 0 x 0 8 ; // (2)
GPIO_PORTF_DEN_R = 0 x 0 8 ;
After configuring the LED (pin PF3) as an output, we want to toggle it after regular inter-
vals. LED can be turned ON and OFF by setting and resetting the corresponding bits in the
GPIODATA register.
GPIO_PORTF_DATA_R = 0 x 0 8 ; // (3)
GPIO_PORTF_DATA_R = 0 x 0 0 ; // (5)
Introducing a Delay
We cannot observe the toggling of LED because of very high frequency. So, 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 ;
}
72 CHAPTER 7. DIGITAL INPUT/OUTPUT INTERFACING AND PROGRAMMING
Source Code
Example 7.1
i n t main ( v o i d )
{
v o l a t i l e unsigned long ulLoop ;
/ / Loop f o r e v e r .
while (1)
{
/ / T u r n on t h e LED .
GPIO_PORTF_DATA_R | = LED_ON ;
/ / Delay f o r a b i t .
f o r ( u l L o o p = 0 ; u l L o o p < DELAY ; u l L o o p + + ) ;
/ / T u r n o f f t h e LED .
GPIO_PORTF_DATA_R &= LED_OFF ;
/ / Delay f o r a b i t .
f o r ( u l L o o p = 0 ; u l L o o p < DELAY ; u l L o o p + + ) ;
}
}