Quick Links
Downloads
|
File |
Instructions |
|
Extract zip file inside of a CprE211 directory on your U: drive |
Reminder
Since you will be keeping track of many files in Cpr E 211, create a
directory on your U: drive for Cpr E 211. Make sure that at the end of
your lab that you create a copy of all necessary files for your partner as
well.
Step 1 - Download and Unzip HelloWorld
For this step of the lab, copy the HelloWorld.zip file onto your U: drive. Unzip the file inside your Cpr E 211 directory. Your final result should look something like the following:
(Click on the picture
to expand to a full size JPEG)
Step 2 - Open CodeWarrior
To open CodeWarrior, select the following options:
Start à Programs à Metrowerks CodeWarrior à CodeWarrior IDE
(Click on the picture to expand to a full
size JPEG)
Once CodeWarrior has launched, you will be presented with a blank screen from which to either create or load a project. All C and assembly files are grouped together into project files. Through the project file, the IDE (Integrated Development Environment) hides the details such as makefiles from the user. Thus, the process of compiling and running code is dramatically simplified.
Step 3 - Open the Hello World Project in CodeWarrior
Select the Open icon from the
CodeWarrior toolbar.
By default, CodeWarrior will try to
search for CodeWarrior project files (*.mcp). Now, navigate to the
HelloWorld directory on your U: drive and select the HelloWorld.mcp file to
open.
Once the file is opened, the
HelloWorld project file should appear in the IDE.
Double click on main.c to open up
the file for editing. Notice how the C keywords are appropriately
highlighted. Most IDEs will support some sort of color coding to assist
in programming.
Step 4 - Run Hello World
Turn on the PowerBox via the switch on the back of the PowerBox unit. For all resets of the PowerBox, use this switch to cycle the power for the system. Make sure that the green LEDs on the board are on before proceeding to the next step.
From the CodeWarrior menu, select Project
à
Debug or press Ctrl+F5. CodeWarrior will automatically attempt
to compile, download and run the code. If there are any syntax errors,
the code will not be downloaded. The debugging is controlled by BDM
(Background Debugging Mode), which is connected via the parallel port of the PC
and external hardware inside of the PowerBox.
The code you are running is being executed inside the PowerBox (on the PowerPC processor of the MPC555). The desktop PC is used to remotely debug the MPC555 microcontroller. In fact, you can turn off the PC and the code will still keep running on the PowerBox.
When the program starts in debug
mode, it will pause at the first line of the main function. Notice the blue arrow next to the first bracket for main (click
on the picture for more info). To move forward in the code, select Debug
à
Step Over or press F10. Notice that there are several
commands for debugging:
Press F5 to run the program to completion. You should see a "Hello World!" pop up on the LCD screen of the QTerm on the right-hand-side of the PowerBox. If you have any problems, contact the TA for assistance.
Step 5 - Step-by-Step Debugging
Press Project à
Debug or Ctrl+F5 again to download the program. Notice how the
blue arrow denotes the current location of where you are at in the
program.
Click on the LCD_PutString
line and press F9 to set a breakpoint. A breakpoint simply tells
the debugger to stop when it reaches that point. Notice how when you
press F9, a small red dot appears to the left of the line. Press F5
to run to the breakpoint. Now, press F10 and watch the HelloWorld
appear on the LCD screen. If you want to remove a breakpoint, simply
press F9 on the same line.
NOTE: You can also right click on a line with the mouse and set a breakpoint to stop on the line.
Try the same procedure again except with using F11 instead of F10 on the function lines. Notice how the IDE automatically opens up QTerm.c and shows you the C code.
Step 6 - Modify Hello World
Change the HelloWorld program to print "Welcome to Cpr E 211!" and the names of your lab group members. Verify that your solution works and move on to the next section. You will need to use additional function calls to LCD_PutString to display your names on the LCD. Make sure to only use a \r, not a \n for the QTerm LCD.
After you have modified the code,
a red check mark will appear in the project window next to the file you modified. You
need to make the binary executable again before sending it to the PowerBox for your
changes to take effect. Press F7 to make the binary executable.
Show
your TA the operation of the HelloWorld program.
Downloads
|
File |
Instructions |
|
Copy files to the HelloWorld directory on your U: drive |
|
|
Unzip this folder to the HelloWorld folder and open PowerPC.ht |
Step 1 - Open HyperTerminal
Directly loading HyperTerminal
Click on the PowerPC.ht file.
Running HyperTerminal from a saved file
From the Windows desktop, select the following options:
Start à Accessories à Communications à HyperTerminal
When HyperTerminal first loads, you will be
prompted to select an icon for the connection. Click the Cancel button to
skip creating a new connection. Click Fileà
Open from the HyperTerminal menu. Open the PowerPC.ht
in HyperTerminal.
HyperTerminal will be used in Cpr E 211 for display to the PC. The program will also be used to send text files to the PowerBox for use in later labs. For now, you will simply be using the program to demonstrate how to send information to the desktop PC via the serial port. All of the options are set correctly in the PowerPC.ht file for connecting to the PowerBox. Click here to see a view of the PowerBox and the types of communications that it uses.
Once you have opened the PowerPC.ht file, HyperTerminal uses the information in the file to determine the correct settings. For lab, the PowerBox is connected to the desktop PC on COM2 at 9,600 bps.
Step 2 - Modify HelloWorld to add in support for PC communication
Next, you will be adding several C
files to the HelloWorld project in order to communicate with the desktop
PC. If you click on the Serial Source Code (UART) file group in
the HelloWorld project, you will see various C source and header files
listed. For most designs in industry, the actual C and assembly code will
be split into multiple files in order to make the design easier to
manage. In addition, it allows you to reuse previous code for newer
projects without entirely rewriting the code.
For this case, you will be adding to
the project two C files that have already been written (Serial_PC.c and
Serial_PC.h). The header file lists the function prototypes for #include
in other files, and the C file contains the actual source code. Now, add
the two files to the Serial Source Code (UART) group by clicking on Project
à
Add Files.
Place
the two C files into the HelloWorld directory with the other C files. The
files will appear at the bottom beneath all of the folders. Drag the two
files into the Serial Source Code (UART) group. Now, you should
see the two files in the HelloWorld project in the Serial Source Code (UART)
group.
Press F7 to have CodeWarrior compile your code. The new source code should compile without a problem. Make sure that you have included both Serial_PC.c and Serial_PC.h into the project.
Step 3 - Modify main.c to send output to the PC
Double click on Serial_PC.h to open the file. Notice the
four function prototypes in the file. The two functions that you are
interested in for the lab are PC_Init and PC_PutString.
Notice how the functions are extremely similar to the LCD functions.
Now, write and insert the necessary C code into the main.c code to send the message "Testing the serial connection" across to the PC; also send the date and time of your lab section.
You will need to use PC_Init and PC_PutString to initialize the serial port and send a string to the PC. What happens if you only use a \r in your code? Consider the definitions for \r and \n
\r - Carriage Return - Take the cursor back to the left-most position
\n - Line feed - Drop the cursor down one line
Try running your program with various combinations of \r and \n and observe the effects. (Hint: You may see this again on a quiz or exam.)
Compile and run your program using Ctrl+F5.
Look at the HyperTerminal and you should see
the text you just printed appear on the HyperTerminal. Note that the text
from the previous section should still appear on the LCD. Show this to
your TA after you are done.
Downloads
|
File |
Instructions |
|
Copy the file to your U: drive inside your Cpr E 211 directory |
Step 1 - Create a new project
Close the Hello World project using File à Close.
Create a new project by
pressing the new project on the toolbar. The first button on the toolbar
creates a new file whereas the second button on the toolbar creates a new
project.
Change the destination directory for the
project to your U: drive and your Cpr E 211 directory
Change
the name of the project to Lab1. This will create a Lab1 directory under
your Cpr E 211 directory. Make sure to change the name after you change
the destination directory, otherwise a new directory for Lab 1 will not be
created.
Double click on the Cpr E 211 - PowerPC option
in the project list
Select the C code option from the list.
This will copy the necessary skeleton files for compiling a C project and set
the project options appropriately.
At this point, you should have created a
Lab1.mcp project file which contains the appropriate files for C interfacing.
Now, using the steps from Section 2 for adding files, download and add the Lab1.c file to the C Source Files group.
In order to link the Lab1 code with main.c, you will need to make a few changes as listed below:
The screenshot to the left shows what main.c
should look like after the changes are made.
Run the code and watch the results on the LCD. Change the DIP switches and notice the resulting behavior.
Now, place a breakpoint in main.c on the call to the Go_Lab1 function.
Step 2 - Viewing variables in CodeWarrior
Step into the Go_Lab1 function.
Notice the four variables declared at the beginning of the function. The
* notation denotes that these variables are pointers to a memory location
containing a char (byte or 8 bits). The pointers are used to access the
memory-mapped I/O of the PowerBox. In this lab, the use of pointers is
already taken care of for you. You will be using the two DIP switches and
the two LED bargraphs of the PowerBox in this program.
Notice
how the variables are displayed above the code. Right click on one of the
variable values to display a list of possible formats for showing the
variables. Note that you can display the variable value in nearly any
type. Remember, a variable is just a sequence of bytes, and it is up to
you, the programmer, to decide what the sequence of bytes actually means.
Step through the code and notice how
the values of the variables change after the assignment statements. You
can click on the + of the variable to display the actual value that the
pointer is pointing to. By default, the value is displayed as an ASCII
char. Change the type to hexadecimal or binary to make it easier to
follow.
The msleep command affects how fast the loop will be executed. The loop is created using a while(1) loop which is also known as an infinite loop. The parameter being passed to the msleep function is the number of milliseconds to sleep. You may change this as necessary.
In the loop, you will notice two calls to PrintSwitch. Do not worry about the pointer notation used to pass the values themselves. Scroll down to the PrintSwitch function. In order to send output to the QTerm, all output is sent using strings or single characters. Since printf is not available, sprintf (string print function) is used to write to a string first and then write the string to the LCD display. Step through the code to see how the array of characters is changed as sprintf is executed.
Show
your TA the bargraph and switch variable values using CodeWarrior.
Step 3 - Modify the Lab1 code – Optional (Extra Credit)
Write and insert a loop to print out the binary value assigned to the variable byVal. Print Bit 7 first and go from MSb (Most Significant Bit - Bit 7) to LSb (Least Significant Bit - Bit 0). Use the hints in the C file or ask your TA if you have difficulties. The carriage returns are already specified in the C code and are not necessary.
Take a look at this example of how the code should work.
Show
your TA the operation of Lab1 code.
Downloads
|
File |
Instructions |
|
Copy the file to your U: drive inside your Cpr E 211 directory |
Step 1 - Create a new project
Close the current project using File à Close.
Select File à New
On the window that appears click and select CprE211 - Power PC 555 Stationery
On the subsequent window, enter a project name and set the location your 211 directory under U: that you created for all CprE211 Projects.
In the Select Project Stationery window, select Assembly Code (from C linkage) option. This is because we are going to deal with assembly programs. Selecting the "C code" option would enable us to code in C using CodeWarrior.
NOTE: By default, CodeWarrior saves projects with the extension ".mcp"
Now a new Project is created for you, and the main project window appears. Note on its toolbar that icons appear for actions like "Make" and "Run". On expanding the C + Assembly Source Files dropdown, you will notice 2 files: main.c and MainSource.asm
Main.c:
Study the C program in main1.c (by double clicking it, it opens). The JumpAsm( ) function in main( ) calls the assembly code. An assembly code subroutine is called by a branch and link statement: bl StartAsm.
MainSource.asm:
Next, take a look at the assembly code and comments in MainSource.asm.
Assembly Program to add 3 numbers
Download Lab1.asm to your project directory in U:. Once this is done the next important step is to add this file to your project.
Delete the existing MainSource.asm file after you add Lab1.asm to your project.
Select "Project à Add files".
Select the downloaded "Lab1.asm" file and see the file appear on the project window. It should be added to the "Runtime" listing of files. You can also move it to the "C+Assembly Source Files" section by dragging it.
Open Lab1.asm and observe the structure of the program, line by line, and the comments for each line. Use QuickrefPPC.html for short explanations on assembly language instructions. This is just an introduction; you will study the PowerPC instruction set in more detail later.
Take note of which registers (named r#) are loaded with the 3 numbers to be added, and insert appropriate instructions to perform addition (a+b+c), placing the final sum in register r31.
Running the program
Turn on the PowerBox via the switch on the back of the unit.
From the CodeWarrior menu, select Project à Enable Debugger. Then select Project à Debug or press Ctrl+F5. CodeWarrior will automatically attempt to compile, download and run the code. If there are any syntax errors, the code will not be downloaded.
Now you see that the blue arrow points at the start of main( ). This is the current program pointer. We can execute a program step by step (one instruction at a time) by making this arrow move one line after another.
To move forward in the code, select Debug à Step Over or press F10. Recall that there are several commands for debugging:
It is handy to use F10 and F11 in debugging. Press F5 to run the program to completion.
Setting Breakpoints
Press Project à Debug or Ctrl+F5 again to download the program. Notice how the blue arrow denotes the current location of where you are at in the program.
Press F9 to set a breakpoint on some line. Press F5 to run to the breakpoint. Now, press F10 and watch the progress. If you want to remove a breakpoint, simply press F9 on the same line.
Step-by-Step Debugging
Press Project à Debug or Ctrl+F5 again to download the program. Now hit F10 once. The blue arrow now points at JumpAsm( ). Now the aim is to ENTER INTO THE FUNCTION JumpAsm( ) and proceed step by step. So hit F11 (Step Into) and notice that the pointer moves to the first line in JumpAsm( ). Hit F10 again to proceed until bl StartAsm (which transfers control to the assembly function we added in the other file). Now hit F11 and watch the control flow enter into the assembly code.
Watching Registers
This modified program adds 3 numbers – 10, 20 and 30 – and places the result in GPR31 (General Purpose Register). The data memory is declared outside the body as DataSeg. Select Window à Registers Window à GPR. Now you can see registers R0 to R31, the stack pointer SP, the pogram counter PC, and a few other general purpose registers. This window is very important for debugging. Registers get updated on every access and their pattern can be observed as we step through the code.
Now hit F10 repeatedly and watch the registers change. As we step through registers r31, r30 and r29, we can see that the respective values (from the data segment) are loaded into the registers one after another. Finally r31 stores the sum. At blr, the flow is transferred back to the calling code.
Report the final answer and your understanding
of the code to the TA.
As time permits, continue to familiarize yourself with the CodeWarrior
environment by exploring more options.
Last Step - Finish Up
Turn off the PowerBox when you are finished. Make sure that the area where you are working is clean before you leave. Log off the PC that you were working on.