Lab 2: MPLAB Introduction
This lab introduces you to the Microchip MPLAB X Integrated Design Environment. The objectives of this lab are:
Become familiar with the MPLAB X environment.
Do some simple programming tasks using PIC24 assembly language.
As always, read through the entire lab and scan the supplied files before starting to work.
Prelab (20 points)
If you've not already done so, download and extract the following archive into C:\ece3724\pic24_code_examples . This lab uses files from the chap3/ directory.
View this video tutorial before you enter the lab. The tutorial introduces you to the MPLAB X IDE and discusses simulation of the mptst_word program.
Note the following:
2:19 - "PIC Memory Views" is no longer an option in MPLAB X IDE 6.15. To open the program memory window, select Window -> Target Memory Views -> Program Memory.
3:15 - Window -> Target Memory Views -> File Registers (right below Program Memory)
3:25 - Instead of scrolling through, you can click the magnifying glass below the output tab and search for line 1000.
4:05 - Window -> Debugging -> Watches (or just Alt-Shift-2)
4:09 - Another way to add expressions to the Watch window is to right-click in the source code and select "New Watch..."
You can also highlight variables to save typing, or select multiple expressions at the same time:
Variables are found in Global Symbols. Registers are found in SFR's (Special Function Registers). Important: To watch a working register, add WREG#. For example, to watch W2, select WREG2. If you're not able to add variables to the Global Symbols list, debug the program once first and try again.
5:12-5:41 - Ignore this; the webpage isn't there anymore.
9:25 - Debug -> Debug Project
10:04 - Your file registers will actually be empty at this point as the software breakpoint takes place before the line is executed. So they should look like this:
Pre-lab checkoff:
Start MPLAB X, open the mptst_word project, and debug it. Stop the debugger session.
Open the program memory tab to view your program, which starts at 0x0200.
Open the file registers tab to view your program's data, which starts at 0x1000.
Open the watch tab then add u16_i, u16_j, u16_k, and WREG0, WREG1, WREG2.
Set a breakpoint in the program. Debug the project again.
Show the TA that you can single-step through instructions (10 pts) and find variables in data memory. (10 pts)
Note: In all tasks, if your number begins with a 0, omit it, or the program will give odd results, as a prefix of 0 indicates an octal number. So if your number is #0123, just use #123. If you've already finished the lab and done something else, (e.g. changed it to another digit, used a different number, nothing but observed discrepancies), just note it in your report.
Task 1: mptst_word.s (20 points)
Modify the i = 2047 assignment to be the last four digits of your student ID (the one on your student ID card). Reassemble the program, and re-simulate it (Debug Project). Take a screenshot after running the program of the Watch window contents (showing u16_i, u16_j, u16_k, and WREG0, WREG1, WREG2). Take a screenshot of the data memory (File Registers window contents).
Checkoff: Show a TA that you modified the number as well as the comments in your program. If you're unsure about what screenshots you need, ask.
Report:
A. Screenshot of Watch window
B. Screenshot of File Registers
C. Calculations that verify the screenshot values match the expected result (warning, i = 2047 is in decimal, as is the last four digits of your student ID; the values in the screenshot are in HEX). Make it a NO-BRAINER for the TA to see that you have verified the screen values!
Task 2: myadd.s (20 points)
Create a copy of the mptst_word project:
Right-click on the mptst_word project in the Projects window and select Copy (or Project -> Copy in MPLAB 6.20+) then enter myadd as the project name.
Open mptst_word.s and select File > Save As... then enter myadd.s.
In the Projects window, under myadd > Source Files, right click on mptst_word.s and select Remove From Project. Now, right-click on Source Files and choose Add existing item..., then select myadd.s (which is one directory up from the default location in the project subdirectory). Close the mptst_word project.
Edit myadd.s, removing the code between After the register assignment comes the input, process, and output steps. and ; The code is complete, but the processor doesn't stop.
Delete the highlighted section. Note that I left a portion in and commented it out to give me a template. If you do this make sure you remove it before you submit.
5. Remove the .space declarations above the code. You can now use this file as a start for a new program.
Erase this. Again, I left one there to remind me how to do it.
Your MSU student ID is a 9-digit decimal number: Y8Y7Y6Y5Y4Y3Y2Y 1Y0. Write a program to add the four digit hex number formed by 0xY3Y2Y1Y0 to the four-digit hex number formed by 0xY7Y6Y5Y4. Reserve space for two 16-bit variables in data space named u16_lsp and u16_msp to hold the hex values 0xY3Y2Y1Y0 and 0xY7Y6Y5Y4 respectively. Reserve space for a variable named u16_sum to hold the sum of u16_lsp and u16_msp.
The following C code describes our program. You must translate each line of the C program to assembly instruction(s). You MUST include, in comments, a register assignment followed by one line of C code, followed by your translation to assembly.
uint16_t u16_lsp;
uint16_t u16_msp;
uint16_t u16_sum;
u16_lsp = 0xY3Y2Y1Y0; // Note that this is a hex value
u16_msp = 0xY7Y6Y5Y4; // Same as above
u16_sum = u16_lsp + u16_msp;
The Chapter 3 lecture slides, "Introduction to PIC Microcontroller," available on Canvas, contains all the assembly code you will need.
Use the watch window to watch variables u16_lsp, u16_msp, and u16_sum as well as the registers you use. Delete the variables you were watching before (you can leave registers if you're still using them). Also, use the data memory window to monitor the memory locations corresponding to these variables. Write your program, simulate it, and verify that you receive the correct results. After running your program, take a screenshot of the Watch window contents. Take a screenshot of the the data memory (File Registers window contents).
Checkoff: Show a TA your program running.
Report:
A. Screenshot of Watch window
B. Screenshot of File Registers
C. Calculations that verify the screenshot values match the expected result. Make it a NO-BRAINER for the TA to see that you have verified the screen values!
D. Attachment: myadd.s (With proper comments and formatting - see Report below for details)
Task 3: mysub.s (20 points)
Create a project named mysub using the same procedure given in Task 2 (Project > Copy..., etc.) with the corresponding assembly language file named mysub.s. Close the myadd project. Using the digits of your MSU student ID, write an assembly language program that does the following C program. You must translate each line of the C program to assembly instruction(s).
uint8_t u8_i;
uint8_t u8_j;
uint8_t u8_k;
uint8_t u8_l;
uint8_t u8_m;
u8_i = Y1Y0; // Note that this is a decimal number.
u8_j = Y3Y2; // Decimal.
u8_k = Y5Y4; // Also decimal.
u8_l = u8_i + u8_j;
u8_m = u8_k – u8_l;
Variables u8_i though u8_m are all 8-bit (byte) variables. ALL operations are byte operations. Note that this time around, Y1Y0, Y3Y2,and Y5Y4 are in decimal.
Use the watch window to watch variables u8_i though u8_m as well as the registers you use. Delete the variables you were watching before (you can leave registers if you're still using them). Also, use the data memory window to monitor the memory locations corresponding to these variables. Write your program, simulate it, and verify that you receive the correct results. After running your program, take a screenshot of the Watch window contents. Take a screenshot of the the data memory (File Registers window contents).
Checkoff: Show a TA your program running.
Report:
A. Screenshot of Watch window
B. Screenshot of File Registers
C. Calculations that verify the screenshot values match the expected result. If the expected m value is less than zero, the watch window will display it as a positive value. In that case, please mention this and briefly explain why.
D. Attachment: mysub.s (With proper comments and formatting)
Report
Your report should have the reporting requirements needed for Tasks 1, 2, and 3. Put the screenshots in the PDF document. Attach the two source files separately.
Your code should have a header on it that looks like this:
for myadd.s
Not like this:
If the code you submit:
Begins with the default header ; .. Copyright (c) 2013 Bryan A. Jones, Robert B. Reese, and J. W. Bruce ("AUTHORS") rather than your own name.
Is labeled with the wrong filename e.g. myadd.s says it's mptst_word.s
Contains blatantly erroneous information about what it does e.g. "This file adds two numbers" when it actually subtracts them.
Lacks comments indicating which assembly language source lines implement which C statements and giving register assignments for each line of C.
The TA will take off a significant number of points (20 points, a zero for the lab report).
Proper comments
Hints
MPLAB
To enable word wrap: Tools -> Options -> Editor -> Formatting -> Line Wrap
To change keyboard shortcuts (For example, you can make "Debug Project" F5, so you don't have to use the mouse every time): Tools -> Options -> Keymap
Here are some tips and tricks for working with MPLAB. However, if you prefer, you can use another text editor or IDE. When you save and tab back to MPLAB X to debug, the file you were working on should automatically refresh.
Make sure:
the project you're working on is the only one open.
the source file you're working on is part of that project.
If your project won't even build, try recreating the project following the steps from Task 2. If you've written any code, copy it into Notepad, and save it on your desktop. If you overwrite/erase a program you need to hand in, you are responsible for recreating it to obtain credit.
Comment each bit of code as you write it. Don't wait until you're about to submit it to add the comments. If your code isn't commented, the TA will not help you debug it.
Assembly Language
In the ebook and lecture slides, variable names are prefaced with underscores. In the lab we do not do this for reasons of compatibility with existing code.
You can't move a literal (number) right into data memory (variable). You have to move it into a register and then move it from the register to the variable. If you want to put it into a 16-bit variable (u16_something), you can use register W0-W7. If it's an 8-bit number, it has to go into WREG.
If you're doing anything with 8-bit variables (u8_something), you need to be using .b after your instructions.
If you have confusion on the usage of WREG and W0, refer to the Chapter 3 lecture notes. In short, if you're using byte-sized quantities and a data memory location in the same instruction it's WREG, otherwise it's W0.
;; 8-bit operation - use WREG
mov.b u8_i, WREG
;; 16-bit operation - call it W0
mov W0, u16_i
;; This is okay - we can use other registers with 16-bit variables
mov u16_i, W3
;; This may run, but don't do it
mov u16_j, WREG
;; This is an error - if W0 interacts with the data memory in byte mode we must call it WREG
mov.b u8_i, W0
;; This is an error - registers W1 through W15 do not support byte mode when copying to data memory locations
mov.b W2, u8_j
;; This is fine, it copies the least significant byte in W0 to the least significant byte in W1. We're not doing anything with data memory.
mov.b W0, W1
See the troubleshooting page for further assistance.
Sample Calculations
Question: Add 0xA + 0xF. Show calculations that verify the screenshot values match the expected result.
Example satisfactory answer:
In Task 4, we add two random hexadecimal numbers, 0xA and 0xF.
0xA in decimal is 10. 0xF in decimal is 15.
10+15 = 25, which is 0x19 in hex.
This value is stored in u16_foo, located at data memory location 0x1234.
As seen in the below screenshots, in the Watch window, u16_foo contains 0x19. In the File Registers screenshot, location 0x1234 also contains 0x19.
(clear screenshots of Watch window and File Registers window showing u16_foo and 0x1234 respectively)
Example unsatisfactory answer:
A+F = 19 (see screenshots for calculations)