0% found this document useful (0 votes)
10 views

07 Digital IO Interfacing and Programming

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.

Uploaded by

2017ee184
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

07 Digital IO Interfacing and Programming

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.

Uploaded by

2017ee184
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 7

Digital Input/Output 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:

1. Enable clocks to the peripheral

2. Configure pins required by the peripheral

3. Configure peripheral hardware

Structure of the Program

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

# define register_name (∗(( volatile unsigned long ∗) r e g i s t e r _ a d d r e s s )

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) . . .
}
}

Pseudo code to blink LED

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

GPIO pin Pin Function USB Device


PF1 GPIO RGB LED (Red)
PF2 GPIO RGB LED (Blue)
PF3 GPIO RGB LED (Green)

RGB LED Signals


70 CHAPTER 7. DIGITAL INPUT/OUTPUT INTERFACING AND PROGRAMMING

LED Configuration

Now we will follow the above mentioned steps to configure the on-board LED.

Enabling the Clock

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.

General-Purpose Input/Output Run Mode Clock Gating Control (RCGCGPIO)

Following command can be used to enable clock signal for GPIO port F

SYSCTL_RCGCGPIO_R = 0 x 2 0 ; // (1)

Configuring the Pin as Output

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.

GPIO Digital Enable (GPIODEN)

The commands used to set the corresponding bits in GPIODEN and GPIODIR registers are
given as follows
71

GPIO Direction (GPIODIR)

GPIO_PORTF_DIR_R = 0 x 0 8 ; // (2)
GPIO_PORTF_DEN_R = 0 x 0 8 ;

Toggle the LED

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 Data (GPIODATA)

The commands for toggling LED are as follows

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

The complete source code for the program is given below

Example 7.1

# define SYSCTL_RCGCGPIO_R (∗(( volatile unsigned long ∗) 0 x400FE608 ) )


# define GPIO_PORTF_DATA_R (∗(( volatile unsigned long ∗) 0 x400253FC ) )
# define GPIO_PORTF_DIR_R (∗(( volatile unsigned long ∗) 0 x40025400 ) )
# define GPIO_PORTF_DEN_R (∗(( volatile unsigned long ∗) 0 x4002551C ) )

# define GPIO_PORTF_CLK_EN 0 x20


# define GPIO_PORTF_PIN3_EN 0 x08
# define LED_ON 0 x08
# define LED_OFF ~(0 x08 )
# define DELAY 200000

i n t main ( v o i d )
{
v o l a t i l e unsigned long ulLoop ;

/ / E n a b l e t h e GPIO p o r t t h a t i s u s e d f o r t h e on−b o a r d LED .


SYSCTL_RCGCGPIO_R | = GPIO_PORTF_CLK_EN ;

/ / Do a dummy r e a d t o i n s e r t a f e w c y c l e s after enabling the peripheral .


u l L o o p = SYSCTL_RCGCGPIO_R ;

/ ∗ E n a b l e t h e GPIO p i n f o r t h e LED ( PF3 ) . S e t t h e d i r e c t i o n a s o u t p u t a n d


e n a b l e t h e GPIO p i n f o r d i g i t a l f u n c t i o n . ∗ /
GPIO_PORTF_DIR_R | = GPIO_PORTF_PIN3_EN ;
GPIO_PORTF_DEN_R | = GPIO_PORTF_PIN3_EN ;

/ / 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 + + ) ;
}
}

You might also like