Lab 4 - C - "Etch-a-Sketch and Pong"


You've spent the last few lessons transitioning from programming in assembly language to C. In this lab, you'll use C to create an etch-a-sketch-type program that utilizes some subroutines from Lab 3. You'll be expected to write clean, maintainable, modular code that is committed regularly to Git.

Group Work Requirements

If you are considering working on Lab 4 as a group here are the requirements:


Given Code


Data types

Go to Table 5-2 (page 76) of the C Compiler User's Guide to complete the following table. For the type, fill in data type that produces a variable of the given size. For max/min values, write in the maximum and minimum values that can be represented with the data type in that row. Two examples have been given.

Size Signed/Unsigned Type Min value Max value
8-bit unsigned
8-bit signed
16-bit unsigned unsigned short
16-bit signed
32-bit unsigned
32-bit signed -2,147,483,648

When writing embedded C code, it is always a good idea to separate your code from the architecture as much as possible because to make the code easier to change. This is why it is better to use the peripheral register names in your code (e.g. P2IN) rather than their address (e.g. 0x28), and to use peripheral register field names in your code

Because space is limited on microcontrollers, it is a common practice to use variables with a range suitable for the task at hand. Unfortunately, there is no standard among C compilers between the basic data types like char, short, long and the number of bits in the underlying data representation. Furthermore, when writing and reading code, it is not readily apparent how many bits are in a short or long variable. Consequently, we will write our programs using typed definitions that provide an obvious connection between the data type and the number of bits in the representation.

In this lab you will be using a library called stdint that provides type definitions that clearly indicate the data size and if it is signed or unsigned. The following is an excerpt from the library:

typedef   signed char    int8_t;
typedef unsigned char   uint8_t;
typedef          int    int16_t;
typedef unsigned int   uint16_t;
typedef          long   int32_t;
typedef unsigned long  uint32_t;

Calling/Return Convention

Make a test project named Lab4_Prelab and use simpleLab4.c as the main file. While the functioning of the program is not really that important, let's first take some time to understand what is going on in this program before we look at the underlying assembly language. Use CCS to step through the program and examine the a, b, c, d, e variables in main, just after the call to the function func in line 18. Fill in the values for each variable in the table below for the first five iterations of the while loop.

Iteration a b c d e

Now examine the assembly code generated by the compiler by selecting the View -> Disassembly menu item. You should see the disassembly window as a selectable tab in the subwindow where your registers are displayed. To fill in the following table with the appropriate values, you have a few tasks:

1) First, find the code for the function func and write down the starting and ending address in the table below.

2) Next, identify which registers are used to pass the input parameters from main to the function. Write their identities below. If it is not clear which register holds which input parameter, test it out! Go ahead and change the code, so that func only has one input parameter, recompile the code, and then examine the assembly.

3) Finally, determine which register is used to return the value from func to main.

Parameter Value Sought
Starting address of func
Ending address of func
Register holding w
Register holding x
Register holding y
Register holding z
Register holding return value

Cross language build constructs

Answer the following questions:

Question 1: What is the role of the extern directive in a .c file? Hint: check out the external variable Wikipedia page.

Question 2: What is the role of the .global directive in an .asm file (used in lines 60-64)? Hint: reference section 2.6.2 in the MSP 430 Assembly Language Tools v4.3 User's Guide.

(This marks the end of the Prelab.)

Required Functionality (Etch-a-Sketch)

Modify your assembly drawBox function to take in 3 values: an x coordinate, a y coordinate, and a color. You may also use a radius if desired.

Some hints have been included in the lab4.c file to get you started. Create an etch-a-sketch program using the directional buttons of the LCD boosterpack to control the position of the paint brush. The paint brush will draw 10x10 (11x11 if using a radius) blocks of pixels. The user will change the position of the paint brush by pressing the directional buttons. Each button press will move the cursor one block in the direction pressed (see table below). Pressing the auxiliary button (S1) will toggle the mode of the paint brush between filling squares and clearing squares.

Button Function
S4/Up Move the cursor up 1 block
S3/Down Move the cursor down 1 block
S2/Left Move the cursor left 1 block
S5/Right Move the cursor right 1 block
S1/Aux Toggle the color of the paint brush

This program must be written in C and call many of the subroutines written as part of Lab 3, including drawBox.

Bouncing Block Functionality

Create a bouncing block! This block should move across the screen with no more than 10 pixels per jump. It should bounce off the walls appropriately, similar to assignment 7. An adequate delay should be added between each block movement. Your starting position and starting x and y velocities should be initialized in your header, or should be randomly generated.

Pong Functionality

Create Pong on your display! Create a single paddle that will move up and down (or side to side) on one side of the display, controlled by the up and down (or left and right) buttons. The block will bounce off the paddle like it bounces off the wall. When the block misses hitting the paddle, the game will end.

Bonus Functionality

Each bonus functionality can be achieved in conjunction with either A or B functionality. Each is worth 5 points.

Note: The maximum lab grade cannot exceed 105.

Grading - Lab 4

Printable Lab 4 Cutsheet

Turn-ins for Lab 4

  1. Prelab
  2. Proof of functionality (live demo)
  3. Code

1. Prelab

2. Proof of functionality (live demo)

You will be performing a live demo to prove that your code works. Videos may be accepted at instructor discretion.

If the instructor who verifies your demo does not teach your section, it is recommended that you print and provide a copy of the Printable Lab 4 Cutsheet to the instructor evaluating your code functionality. Provide this cutsheet to your instructor NLT when the lab is due.

For full credit your demo must meet the following requirements:

  1. One program demo shall provide all graded functionalities.

    • Hint: After Pong game ends, wait for button press to clear screen and start Etch-a-Sketch functionality.
  2. Boxes must be well formed (i.e., have homogenous number of pixels on a side)

    • Hint: Paint correct number of pixels for your box size
  3. Controls and movement must be responsive

    • Hint: Draw only the pixels you need
    • Hint: Use only the length and number of delays you need
  4. Speed of ball (and paddle) shall be consistent (e.g., ball should not slow down when it collides with an edge or when the paddle is moved)

    • Hint: Draw only the pixels you need
    • Hint: Use only the length and number of delays you need
  5. Ball / box shall be able to traverse across the screen to within one pixel of each edge

  6. No part of the ball / box shall leave the screen

3. Code


Printable Lab 4 Cutsheet

Item Grade Points Out of Date Due
Prelab On-Time ------------------------------------------------------ Late: 1Day ---- 2Days ---- 3Days ---- 4+Days 10 BOC L23
Required Functionality On-Time ------------------------------------------------------ Late: 1Day ---- 2Days ---- 3Days ---- 4+Days 30 COB 19/20 Oct
Bouncing Block On-Time ------------------------------------------------------ Late: 1Day ---- 2Days ---- 3Days ---- 4+Days 25 COB 19/20 Oct
Pong On-Time ------------------------------------------------------ Late: 1Day ---- 2Days ---- 3Days ---- 4+Days 25 COB 19/20 Oct
Bonus Functionality ------ Circle ------ Background ------ Score ------ Other ------ 5 each COB 19/20 Oct
Code On-Time ------------------------------------------------------ Late: 1Day ---- 2Days ---- 3Days ---- 4+Days 10 COB 19/20 Oct
Total Max of 105 points 100 M/T