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.
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|
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;
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.
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.
|Starting address of
|Ending address of
|Register holding w|
|Register holding x|
|Register holding y|
|Register holding z|
|Register holding return value|
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.
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.
|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
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.
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.
Each bonus functionality can be achieved in conjunction with either A or B functionality. Each is worth 5 points.
Circle: Instead of a bouncing block, create a bouncing circular ball!
Background image: Create a background image for your game! Be sure to get your instructor's approval of the image to ensure it will count for the bonus points.
Keep score: Each time the ball bounces off the paddle, increment your decimal score. Use the fonts.h file.
Other: do you have another suggestion? Ask your instructor!
Note: The maximum lab grade cannot exceed 105.
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:
One program demo shall provide all graded functionalities.
Boxes must be well formed (i.e., have homogenous number of pixels on a side)
Controls and movement must be responsive
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)
Ball / box shall be able to traverse across the screen to within one pixel of each edge
No part of the ball / box shall leave the screen
|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||25||COB 19/20 Oct|
|Bonus Functionality||------ Circle ------ Background ------ Score ------ Other ------||5 each||COB 19/20 Oct|
|Code||10||COB 19/20 Oct|
|Total||Max of 105 points||100||M/T|