Lab 2: MPLAB Introduction

This lab introduces you to the Microchip MPLAB X Integrated Design Environment. The objectives of this lab are:

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:

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.

Pre-lab checkoff: 

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:

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:

The TA will take off a significant number of points (20 points, a zero for the lab report).

Proper comments

Hints

MPLAB

Assembly Language


;; 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

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)