Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

UT.6.03x Embedded Systems - Shape the World - Lab 08: Switch and LED Interface

Hello. I’ve just bought my new LaunchPad. Today, I’m going to log my progress on lab 08 of this course - LEDs and Switches. This is the first lab that we are going to interface LEDs and switches. Breadboard also appears for the first time in this course. I’m excited to get into the mixture of electronics and programming.


First, I’ll summarize what I’ve learned in chapter 8.

  1. Switch:
    For more information about the switch, see its datasheet.

  2. Pull-up resistor vs. Pull-down resistor & Positive logic vs. Negative logic:
    This is the main idea of this chapter. We will dig into the relationship between electronics and logic.
    A pull-up resistor is connected to the power-supply voltage (VCC) and represents negative logic, whereas a pull-down resistor is connected to ground (GND) and represents positive logic.
    However, what are positive logic and negative logic?
    Remember, the LaunchPad always reads 0 = open and 1 = closed.

  3. Floating state and how to prevent it:
    When the switch is closed, the LaunchPad will read 1. When the switch is open, the LaunchPad looks like it’s reading 0. However, it’s not exactly 0. It’s “float”.
    To prevent “float”, we need to look up the datasheet of the microcontroller. I forgot where I got it, but I learned that the current limit for I/O pins is 2 microamps, and the pin will read from 0V - 0.8V as negligible. Let the voltage read in when the switch is open be 0.02V. According to Ohm’s law, we have to put a 10-kilohm resistor as a pull-down resistor to make the switch work as a positive logic.

  4. LED Interface:
    Read the LED’s data sheet before attempting to do anything to it.
    Each LED has an operating point at which it operates at maximum efficiency. Knowing the operating point helps us to put the right value of resistor for the LED and prevent it from becoming a firework on a 3.3V circuit.
    To calculate the right value of a resistor for the LED,
    In this lab, we need a 500-ohm resistor to make the LED work properly, but using a 470-ohm resistor is good enough.

  5. Next, let’s talk about a design example. There are 6 steps: Requirements Document, Data Flow Graph, Code Flow Charts, Building the Software, Testing in the Simulation, and Testing on the Real Board. That’s all we need to know.

Let’s build the circuit!






Because Fritzing doesn’t have a part for EK-TM4C123GXL, I’ll draw the schematic on paper.


Here is the real board layout:



Note that to be able to run and grade the labs, you have to follow the software requirements. Unfortunately, the software and the course don’t support Linux. However, if you follow the official instruction from Texas Instruments, you can upload and run your program on the LaunchPad.

See below for my full code, or view it on my GitHub.

 * UTAustinX: UT.6.03x Embedded Systems - Shape the World
 * Lab 8: Switch & LED Interface
 * File Name: main.c
 * Description:
 * The first lab building the circuit on the breadboard
 * The switch is pressed: The LED flashes 5 Hz
 * The switch is released: The LED is steadily on.
 * Compatibility: EK-TM4C123GXL
 * Author: Phi Luu
 * Location: Portland, Oregon, United States
 * Created: March 22, 2016
 * Updated: June 22, 2017

 * Required hardware I/O connections
 * Switch connected to PE0
 * LED connected to PE1

#include "TExaS.h"
#include "tm4c123gh6pm.h"

// Constant declarations to access port registers
// using symbolic names instead of addresses
#define GPIO_PORTE_DATA_R     (*((volatile unsigned long *)0x400243FC))
#define GPIO_PORTE_DIR_R      (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R    (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_PUR_R      (*((volatile unsigned long *)0x40024510))
#define GPIO_PORTE_DEN_R      (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_LOCK_R     (*((volatile unsigned long *)0x40024520))
#define GPIO_PORTE_CR_R       (*((volatile unsigned long *)0x40024524))
#define GPIO_PORTE_AMSEL_R    (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R     (*((volatile unsigned long *)0x4002452C))
#define SYSCTL_RCGC2_R        (*((volatile unsigned long *)0x400FE108))
#define PE0                   (*((volatile unsigned long *)0x40024004))
#define PE1                   (*((volatile unsigned long *)0x40024008))

// Function prototypes
void EnableInterrupts(void);  // enable interrupts
void PortEInit(void);         // port E initialization
void Delay(unsigned long ms); // delay ms milliseconds

int  main(void) {
    // Setup
    // activate grader and set system clock to 80 MHz
    TExaS_Init(SW_PIN_PE0, LED_PIN_PE1, ScopeOn);
    PortEInit();        // initialize port E
    EnableInterrupts(); // enable interrupts for the grader
    PE1 = 0x02;         // start with LED on

    // Loop
    while (1) {
        Delay(100);        // wait 100 ms

        if (PE0 == 0x00) { // if the switch is not pressed
            PE1 = 0x02;    // turn on the LED steadily
        } else {           // if the switch is pressed:
            PE1 ^= 0x02;   // toggle the LED

 * Initializes port E pins for input and output
void PortEInit(void) {
    volatile unsigned long delay;

    SYSCTL_RCGC2_R    |= 0x00000010;     // 1) E clock
    delay              = SYSCTL_RCGC2_R; // delay
    GPIO_PORTE_LOCK_R  = 0x4C4F434B;     // 2) unlock PortE
    GPIO_PORTE_CR_R   |= 0x03;           // allow changes to PE1, PE0
    GPIO_PORTE_AMSEL_R = 0x00;           // 3) disable analog function
    GPIO_PORTE_PCTL_R  = 0x00;           // 4) GPIO clear bit PCTL
    GPIO_PORTE_DIR_R  |= 0x02;           // 5) PE0 input, PE1 output
    GPIO_PORTE_AFSEL_R = 0x00;           // 6) no alternate function
    GPIO_PORTE_DEN_R  |= 0x03;           // 7) enable digital pins PE1, PE0

 * Delays in units of milliseconds
 * @param  ms   number of milliseconds to delay
 * @assumption  80-MHz clock
void Delay(unsigned long ms) {
    unsigned long count;

    while (ms) {
        count = 15998; // approximately 16000 to delay 1 ms

        while (count) {



Lab 8 is the first lab we work on the breadboard, so it’s very interesting. In the next post, I’ll come up with my tips on the Delay() function. Thanks for reading!