You are on page 1of 18

Experiment 1: Introduction to TM4C LaunchPad

Name: P. Harish Shanmugam


Roll no: CED19I032

Date: 18 January, 2021

Objective:
To become experimentally familiar with the basic layout of the TM4C123GH6PM microcontroller, and to
learn how to interface with LEDs and switches on different ports.

Simulation Procedure:
Run Mode Clock Gating Control Register 2 (RCGC2):

This register controls the clock gating logic in normal Run mode. Each bit controls a clock enable for a
given interface, function, or module. If set, the module receives a clock and functions. Otherwise, the
module is unclocked and disabled.

Bit/Field Description
0 Port A Clock Gating Control
1 Port B Clock Gating Control
2 Port C Clock Gating Control
3 Port D Clock Gating Control
4 Port E Clock Gating Control
5 Port F Clock Gating Control

A value of 1 in the specified bit enables the clock for the specified port.

A value of 0 in the specified bit disables the clock for the specified port.

SYSCTL_RCGC2_R |= 0x00000020; // Activate the clock for Port F

SYSCTL_RCGC2_R |= 0x00000008; // Activate the clock for Port D


GPIO Lock (GPIOLOCK):

The GPIOLOCK register enables write access to the GPIOCR register. Writing 0x4C4F.434B to the
GPIOLOCK register unlocks the GPIOCR register. The value of the GPIOCR register determines which bits
of the GPIOAFSEL, GPIOPUR, GPIOPDR, and GPIODEN registers are committed when a write to these
registers is performed. Writing any other value to the GPIOLOCK register re-enables the locked state.
Reading the GPIOLOCK register returns the lock status rather than the 32-bit value that was previously
written.

GPIO Direction (GPIODIR):

The GPIODIR register is the data direction register. Setting a bit in the GPIODIR register configures the
corresponding pin to be an output, while clearing a bit configures the corresponding pin to be an input.
All bits are cleared by a reset, meaning all GPIO pins are inputs by default.

Value Description
0 Corresponding pin is an input
1 Corresponding pin is an output

GPIO_PORTF_DIR_R = 0x0E; // Input: PF4, PF0 Output: PF3-1

GPIO_PORTD_DIR_R |= 0x09; // Output: PD0, PD3

GPIO Analog Mode Select (GPIOAMSEL):

The GPIOAMSEL register controls isolation circuits to the analog side of a unified I/O pad. Because the
GPIOs may be driven by a 5-V source and affect analog operation, analog circuitry requires isolation
from the pins when they are not used in their analog function.
Value Description
0 The analog function of the pin is disabled, the isolation is enabled,
and the pin is capable of digital functions
1 The analog function of the pin is enabled, the isolation is disabled,
and the pin is capable of analog functions

GPIO_PORTF_AMSEL_R = 0x00; // Disable analog functions on all pins of Port F

GPIO_PORTD_AMSEL_R &= ~(0x09); // Disable analog functions on pins PD0 and PD3 of Port D

GPIO Alternate Function Select (GPIOAFSEL):

The GPIOAFSEL register is the mode control select register. If a bit is clear, the pin is used as a GPIO and
is controlled by the GPIO registers. Setting a bit in this register configures the corresponding GPIO line to
be controlled by an associated peripheral. Several possible peripheral functions are multiplexed on each
GPIO. The GPIO Port Control (GPIOPCTL) register is used to select one of the possible functions.

Value Description
0 The associated pin functions as a GPIO and is controlled by the GPIO
registers
1 The associated pin functions as a peripheral signal and is controlled
by the alternate hardware function

GPIO_PORTF_AFSEL_R = 0x00; // Disable alternate hardware function on all pins of Port F

GPIO_PORTD_AFSEL_R&= ~(0x09); // Disable alternate hardware function on PD0 and PD3


GPIO Port Control Register (GPIOPCTL):

The GPIOPCTL register is used in conjunction with the GPIOAFSEL register and selects the specific
peripheral signal for each GPIO pin when using the alternate function mode.

Each of the four bits represent the Mux Control for the corresponding pin. When a bit is set in the
GPIOAFSEL register, the corresponding GPIO signal is controlled by an associated peripheral. The
GPIOPCTL register selects one out of a set of peripheral functions for each GPIO, providing additional
flexibility in signal definition.

GPIO_PORTF_PCTL_R = 0x00000000; // Disable Mux Control on all pins of Port F

GPIO_PORTD_PCTL_R &= ~(0x09); // Disable Mux Control on PD0 and PD3

GPIO Digital Enable (GPIODEN):

The GPIODEN register is the digital enable register.

Value Description
0 The digital functions for the corresponding pin are disabled
1 The digital functions for the corresponding pin are enabled

GPIO_PORTF_DEN_R = 0x1F; // Enable digital functions on all pins of Port F

GPIO_PORTD_DEN_R |= 0x09; // Enable digital functions on PD0 and PD3


GPIO Pull-Up Select (GPIOPUR):

The GPIOPUR register is the pull-up control register. When a bit is set, a weak pull-up resistor on the
corresponding GPIO signal is enabled.

Value Description
0 The corresponding pin's weak pull-up resistor is disabled
1 The corresponding pin's weak pull-up resistor is enabled

GPIO_PORTF_PUR_R = 0x11; // Enable pull-up resistor for PF0 and PF4 (switches)
Exercise 1:
To toggle the onboard LED (R or B or G) on Port F and observe the resulting waveform in debug mode.

Code:

#include "tm4c123gh6pm.h"
#include <stdint.h>

void PortF_Init(void){ volatile unsigned long delay;


SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F
delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out
GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0
GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0
}
unsigned long Led;
void Delay(void){unsigned long volatile time;
time = 145448; // 0.1sec
while(time){
time--;
}
}
int main(void){
PortF_Init(); // make PF1 out (PF2 built-in LED)
while(1){
Led = GPIO_PORTF_DATA_R; // read previous
Led = Led^0x04; // toggle blue LED, PF2
GPIO_PORTF_DATA_R = Led; // output
Delay();
}
}
Output:

Condition: LED on PD2 is toggled


Exercise 2:
To toggle the onboard LED (R or B or G) using SW1 on Port F and observe the resulting waveform in
debug mode.

Code:

#include "tm4c123gh6pm.h"
#include <stdint.h>

unsigned long Switch;

void PortF_Init(void){ volatile unsigned long delay;


SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F
delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out
GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0
GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0
}
unsigned long Led;
void Delay(void){unsigned long volatile time;
time = 145448; // 0.1sec
while(time){
time--;
}
}

// Case: 1 - Activating one switch to toggle the RED LED


int main(void){
PortF_Init(); // make PF1 out (PF1 built-in LED)
while(1){
Switch=GPIO_PORTF_DATA_R&0x11;
if(Switch==0x01)
GPIO_PORTF_DATA_R ^= 0x02;
else
GPIO_PORTF_DATA_R = 0x00;
Delay();
}
}

Outputs:

Condition 1: No LED glows when SW1 is not toggled

Condition 2: LED on PD1 is toggled when SW1 is toggled


Exercise 3:
To control the onboard LEDs using SW1 and SW2 on Port F with the following conditions and observe
the resulting waveform in debug mode.

Switch (PF4) SW-1 Switch (PF0) SW-2 LEDs

1 1 R (0x02)

0 1 G (0x08)
1 0 B (0x04)

0 0 W (0x0E)

Code:

#include "tm4c123gh6pm.h"
#include <stdint.h>

unsigned long Switch;


void PortF_Init(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F
delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out
GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0
GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0
}
unsigned long Led;
void Delay(void){unsigned long volatile time;
time = 145448; // 0.1sec
while(time){
time--;
}
}

int main(void){
PortF_Init(); // make PF1 out (PF1 built-in LED)
while(1){
Switch=GPIO_PORTF_DATA_R&0x11;
if(Switch==0x11)
GPIO_PORTF_DATA_R = 0x02; // Both SW1 and SW2 are not toggled - Red
else if(Switch==0x01)
GPIO_PORTF_DATA_R = 0x08; // SW1 Toggled; SW2 not toggled
else if(Switch==0x10)
GPIO_PORTF_DATA_R = 0x04; // SW2 Toggled; SW1 not toggled
else if(Switch==0x00)
GPIO_PORTF_DATA_R = 0x0E; // Both SW1 and SW2 are toggled - White
else
GPIO_PORTF_DATA_R = 0x00;

Delay();
}
}

Outputs:

Condition 1: LED on PF1 glows when both SW1 and SW2 are not toggled
Condition 2: LED on PF3 glows when SW1 is toggled and SW2 is not toggled

Condition 3: LED on PF2 glows when SW1 is not toggled and SW2 is toggled
Condition 4: LEDs on PF1, PF2 and PF3 glow when both SW1 and SW2 are toggled
Exercise 4:
To control external LEDs on Port D using the onboard Switches SW1 and SW2 on Port D and observe the
resulting waveforms in debug mode.

Switch (PF4) SW-1 Switch (PF0) SW-2 LEDs

1 1 NO (0x00)

0 1 Port D3 (0x08)
1 0 Port D0 (0x01)

0 0 Port D3 and D0 (0x09)

Code:

#include "tm4c123gh6pm.h"
#include <stdint.h>

unsigned long Switch;

void PortF_Init(void){ volatile unsigned long delay;


SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F
delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out
GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0
GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0
}

void PortD_Init(void){ volatile unsigned long delay;


SYSCTL_RCGC2_R |= 0x00000008; // 1) activate clock for Port D
delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTD_AMSEL_R &= ~(0x09);
GPIO_PORTD_PCTL_R &= ~(0x09);
GPIO_PORTD_DIR_R |= 0x09; // 5) enable PD0 and PD3 as
output
GPIO_PORTD_AFSEL_R&= ~(0x09); // 6) disable alt funct on PD0 and
PD3
GPIO_PORTD_DEN_R |= 0x09;
}

unsigned long Led;


void Delay(void){unsigned long volatile time;
time =145448; // 0.1sec
while(time){
time--;
}
}

int main(void)
{
PortF_Init();
PortD_Init();
while(1)
{
Switch=GPIO_PORTF_DATA_R&0x11;
if(Switch==0x11)
GPIO_PORTD_DATA_R = 0x00;
else if (Switch==0x01)
GPIO_PORTD_DATA_R = 0x08;
else if (Switch==0x10)
GPIO_PORTD_DATA_R = 0x01;
else if (Switch==0x00)
GPIO_PORTD_DATA_R = 0x09;
else
GPIO_PORTD_DATA_R = 0x00;
}
}
Outputs:

Condition 1: No LED glows when both SW1 and SW2 are not toggled

Condition 2: LED on PD3 glows when SW1 is toggled and SW2 is not toggled
Condition 3: LED on PD0 glows when SW1 is not toggled and SW2 is toggled

Condition 4: Both LEDs on PD0 and PD3 glow when both SW1 and SW2 are toggled
Conclusion:

In this experiment, we have learnt about the basic layout of the TM4C123GH6PM microcontroller, the
uses of various GPIO resistors to perform basic operations, and how to interface with switches and LEDs
on different ports.

You might also like