Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

UT.6.03x Embedded Systems - Shape the World - Lab 02: Hello LaunchPad

Hi, I’m back and I’m gonna show my work in lab 2 of UT.6.03x. This will be a full guide to using Keil uVision 4.


I would like to talk about doing the labs in this course. First, always start the lab in the folder \KEIL_ROOT_DIRECTORY\Labware\ and then execute LAB_NAME.uvproj. In this case, I use C:\Keil\Labware\Lab02_HelloLaunchPad\Lab02_HelloLaunchPad.uvproj. The following windows will appear:


If the editor doesn’t appear, double click on main.c to open it (Highlighted on the screenshot above).

Second, click on the Options for Target button (highlighted):


The following dialogue will appear. Take a look at Target and Debug tabs. Make sure the Operating System in Target tab is TExaS.


Next, go to the Debug tab. In order to test the lab on the simulator, choose the Use Simulator button, then click OK.


In order to test the lab on the real board, choose the Use button on the right of the Simulator, and use Stellaris ICDI.


Don’t touch anything else! Just click OK and complete the setup.

To turn the lab grader on, we need to Build first, and then Start the Debug Session.


Look at the Build Output window after building the project. All errors (and warnings) must be fixed before the execution of the program. In this case, I don’t need to fix anything.


Next, click Start the Debug Session to start debugging, a warning dialogue will appear. Click OK.

A new environment will appear.


Debugging buttons are very important. Each button has its own function. Following images indicate each of those.

  1. Step: To step one line and to step into loops and functions.

  2. Step Over: To step over the current line. It’s useful in stepping over long loops and functions.

  3. Step Out: To step out of the current function or loop. Use it to exit an ongoing loop or function.

  4. Run to Cursor Line: To run the debug up to cursor line and pause right there.

  5. Run: Run the entire program all at once.

Next, open the port F simulator of the LaunchPad. This is also where we get graded on simulation. It’s a good practice to test the code on the simulator before running it directly on the LaunchPad.




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.

We don’t need to modify any lines of the code, but I did clean the code up. So, see below for my code, or view it on my GitHub.

 * UTAustinX: UT.6.03x Embedded Systems - Shape the World
 * Lab 2: Hello LaunchPad
 * File Name: main.c
 * Description: If the left switch SW1 is...
 *     not pressed: the LED toggles blue-red.
 *     pressed: the LED toggles blue-green.
 * Compatibility: EK-TM4C123GXL
 * Author: Phi Luu
 * Location: Portland, Oregon, United States
 * Created: March 05, 2016
 * Updated: June 22, 2017

 * LaunchPad built-in hardware
 * SW1 (left switch) is negative logic PF4 on the LaunchPad
 * SW2 (right switch) is negative logic PF0 on the LaunchPad
 * Red LED connected to PF1 on the LaunchPad
 * Blue LED connected to PF2 on the LaunchPad
 * Green LED connected to PF3 on the LaunchPad

#include "TExaS.h" // grader library

// Constant declarations to access port registers
// using symbolic names instead of addresses

#define GPIO_PORTF_DATA_R     (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R      (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R    (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R      (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R      (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R     (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R       (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R    (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R     (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R        (*((volatile unsigned long *)0x400FE108))

// Global variables
unsigned long switch_input; // input from PF4

// Function prototypes
void PortFInit(void);
void Delay(void);
void EnableInterrupts(void);

int  main(void) {
    // Setup
    // initialize the TExaS grader lab 2
    TExaS_Init(SW_PIN_PF40, LED_PIN_PF321);
    PortFInit();        // initialize port F
    EnableInterrupts(); // the grader uses interrupts

    // Loop
    while (1) {
        switch_input = GPIO_PORTF_DATA_R & 0x10; // read PF4 into switch

        if (!switch_input) {                     // zero means SW1 is pressed
            GPIO_PORTF_DATA_R = 0x08;            // LED is green
        } else {                                 // 0x10 means SW1 is not pressed
            GPIO_PORTF_DATA_R = 0x02;            // LED is red
        Delay();                                 // wait 0.1 sec
        GPIO_PORTF_DATA_R = 0x04;                // LED is blue
        Delay();                                 // wait 0.1 sec

 * Initializes port F pins for input and output.
 * PF4 and PF0 are input SW1 and SW2 respectively.
 * PF3, PF2, PF1 are outputs to the LED.
 * These five pins are built in the LaunchPad.
void PortFInit(void) {
    volatile unsigned long delay;

    SYSCTL_RCGC2_R    |= 0x00000020;     // 1) unlock F clock
    delay              = SYSCTL_RCGC2_R; // allow time for clock to start
    GPIO_PORTF_LOCK_R  = 0x4C4F434B;     // 2) unlock Port F
    GPIO_PORTF_CR_R    = 0x1F;           // allow changes to PF4-0
    GPIO_PORTF_AMSEL_R = 0x00;           // 3) disable analog function
    GPIO_PORTF_PCTL_R  = 0x00000000;     // 4) clear bit PCTL
    GPIO_PORTF_DIR_R   = 0x0E;           // 5) PF4, PF0 input; PF3, PF2, PF1 output
    GPIO_PORTF_AFSEL_R = 0x00;           // 6) no alternate function
    GPIO_PORTF_PUR_R   = 0x11;           // enable pullup resistors on PF4, PF0
    GPIO_PORTF_DEN_R   = 0x1F;           // 7) enable digital pins PF4-PF0

 * Waits 0.1 sec.
 * @assumption   80-MHz clock
void Delay(void) {
    unsigned long volatile time;

    time = 727240 * 200 / 91; // 0.1sec

    while (time) {