Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

UT.6.03x Embedded Systems - Shape the World - Lab 05: Functions in C

In this blog, I’ll go through lab 05 of UT.6.03x - Functions in C. I kept reminding myself of going back to Arduino, yet here I am. I actually enrolled in the course and so I had to follow its deadline so that I could get my work done and earn a certificate. For now, I’ll be playing around with the Tiva C-Series LaunchPad. I think I’ll come back to Arduino after a while.

OVERVIEW:

Lab 5 focuses on C programming at an introductory level. First, the lectures talked about flowcharts and history of C. I recommend skimming through the history of C and concentrating more on the flowchart since it helps people acquire computational thinking. The second thing we need to know is input/output statement, code organizing, operators, and data types. Next, they talked about functions in C, conditional branching, and loop in C. These are some of the most important elements of programming.

Alright, let’s take a look at my lab in this chapter.

CODE:

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 code in main.c, or view the project folder on my GitHub.

/**
 * UTAustinX: UT.6.03x Embedded Systems - Shape the World
 * Lab 5: Functions in C
 *
 * File Name: main.c
 *
 * Description: Calculates the area of a rectangle.
 *     If Length or Width is in the close interval [3,20], Area = Length * Width
 *     Otherwise, Area = 0
 *
 * Compatibility: EK-TM4C123GXL
 *
 * Author: Phi Luu
 * Location: Portland, Oregon, United States
 * Created: March 06, 2016
 * Updated: June 22, 2017
 */

#include <stdio.h> // standard C library
#include "uart.h"  // functions to implement I/O
#include "TExaS.h" // lab grader functions

// Function prototypes
unsigned long Calculate_Area(unsigned long l, unsigned long w);
void          EnableInterrupts(void);

int           main(void) {
    // Setup
    unsigned long length, width, area;

    // initialize the TExaS grader lab 5
    TExaS_Init(UART_PIN_PA0, UART_PIN_PA1);
    UART_Init();        // initialize UART for printing
    printf("\nThis program calculates areas of rectangular rooms.\n");
    EnableInterrupts(); // the grader needs interrupts

    // Loop
    while (1) {
        // get length input
        printf("\nGive length: ");
        scanf("%ld", &length);
        // get width input
        printf("\nGive width: ");
        scanf("%ld", &width);
        area = Calculate_Area(length, width); // calulate the area
        // write the value on the screen
        printf("\nArea of the room = %ld\n", area);
    }
}

/**
 * Computes the area of a rectangular room whose sides are given.
 *
 * @param   l  the length of a rectangle
 * @param   w  the width of a rectangle
 *
 * @return     the area of the rectangular room.
 *             0 if
 *                 the length is less than 3, or
 *                 the width is less than 3, or
 *                 the length is greater than 20, or
 *                 the width is greater than 20
 */
unsigned long Calculate_Area(unsigned long l, unsigned long w) {
    unsigned long result;

    if ((l < 3) || (w < 3) || (l > 20) || (w > 20)) { // out of range cases
        return 0;                                     // area = 0
    }
    result = l * w;                                   // area = length * width
    return result;
}

SIMULATOR TESTING:

REAL-BOARD TESTING:

Grading on the real board is a little bit different from previous labs, but I’ll show the process on the following video: