# CSCI 255 Lab 8 -- C Operators

## Preparation

We will be using the MPLAB IDE today; you will also need the PIC24 code example archive downloaded in Lab 7. Make sure that your machine is booted into Windows and start MPLAB. The MPLAB IDE can be found under the from the start button by following All Programs » Microchip » MPLAB IDE vN.NN » MPLAB IDE.

## Part 1: Writing a C program in MPLAB

Your first task is to write a C program using the bitwise operators discussed in class. Specifically, you will need to be familiar with the following operators to accomplish this task:

1. the bitwise operators AND, OR, and XOR
2. the logical right shift and left shift operators

Write a C program that sums the four nibbles, or 4-bit chunks, forming a 16-bit unsigned integer number. For example, given a 16-bit unsigned integer variable, myNumber, storing the value 0x78FA, as shown below:
uint16_t sumOfNibbles, myNumber = 0x78FA;
Your program should set sumOfNibbles to 0x7 + 0x8 + 0xF + 0xA or 0x28, also known as 40.

After you have written your program, compile it and run it on the PIC24 simulator in MPLAB.

You will need to output the sum that you calculate. This can be a little tricky. You must have a #include of stdio.h at the front of your program.
#include <stdio.h>
And you'll need a statement to print sumOfNibbles toward the end of your program.
printf("The answer is %d\n", sumOfNibbles) ;

Then you need to “connect” the chip's UART to the output window. Zeroth, be sure to use the menu choices Debugger » Select Tool » MPLAB SIM to use the built-in simulator. First, press the Debugger menu button and choose the Settings... selection at the bottom of the list. This should bring up a window labeled Simulator Settings. Now select the Uart1 IO tab. Next check Enable Uart1 IO and press the Window button under Output.

### Getting Started

You will need to write your program and run it in MPLAB. Begin, just as we did in Lab 7, by opening an existing MPLAB project from the PIC24 code example archive; use chap6/countOnes.mcp as shown below.

Save that project with the name addNibbles using the Project » Save Project As option from the menu bar. Open the source file countOnes.c and save it with the new name addNibbles.c using the File » Save As from the menu bar. Right-click on countOnes.c and remove it from the project. Right-click the Source Files folder and add addNibbles.c to the project. Open addNibbles.c and delete all code not shown below. (I added the comment in the body of main.)

Now you are ready to write your program!! You may remove any other source code files from the project, but it is not necessary.

### Hints

Remember that the bitwise AND operator can be used with a mask to clear selected bits of a word. For example, referring back to the example above,

uint16_t myNumber = 0x78FA, secondNibble;

secondNibble = myNumber & 0x00F0;

You can also use the right shift operator >> and the left shift operator << to shift the bits in a memory location. Can you remember the C (and Java and Processign) programming statement needed to move the bits in the variable secondNibble above to the right four places?

### Checkoff

 Run your program to calculate the sum of the nibbles in 0x78FA. To see the output of your program select the tab labeled SIM Uart1 in the Output window.

## Part 2: Writing your program in PIC 24 Assembly Language

As discussed in class, there is a close correlation between the C bitwise operators and the corresponding PIC 24 instructions. This will make it relatively easy to translate your C program from Part 1 into PIC 24 assembly language, which is your next task.

Begin by creating a new project for your assembly language program. Follow the instructions provided in Part 1, but this time use chap3/mptst_word.mcp. You might name your new project addNibblesASM to distinguish it from the project created in Part 1.

Rename and replace the assembly language program, the .s file, and then erase all code not shown below before writing the assembly language version of addNibbles. (You might name this file addNibblesASM.s.)

Of course, you can retain more of the assembly code of mptst_word.s if you think that you can use it in your current program. Check your program by running it on the PIC 24 simulator and verifying that the sum of nibbles is correct in memory.

You will need to consult the PIC 24 Family Instruction Set Summary to complete this assignment.

### Checkoff

 Run your program to calculate the sum of the nibbles in 0x78FA. Show the instructor your assembly code and the memory location containing the calculated sum of nibbles.

### Example Solutions

At the end of lab the links below will be made "live" so that you can view sample solutions for the programs that you were asked to write in this lab.