CSC 2600 Lab 6 MIPS (and PIC) programming

We're going to do some assembly language programming for the MIPS family architecture, using one of the commercial processors which have licensed it.

The one we're going to use is the PIC32MX110F016B, which is a microcontroller built by Microchip. The PIC32MX110F016B has 19KB of flash memory and 4KB of RAM, which is not a lot of memory, but more than we probably need for the labs we're going to implement on it.

The PIC32MX110F016B costs $2.35, but a running system requires a breadboard, some bypass capacitors, and a 3.3V power supply. The breadboard is the most expensive part of the system, but of course we already have a number in the lab. For this first MIPS project, you'll build the system on a breadboard, and modify a program I wrote.

The goal of this lab is write a MIPS assembly program which flashes a short message on a 7-segment display

So the main purpose is to become familiar with writing code in MIPS assembler. But because it is dismaying to write a program which you can't execute, we'll set up a PIC microcontroller system on a breadboard so that you can see the program run.

Programming a microcontroller.

A microcontroller is a small computer which fits on a single chip. Like other computers, the microcontroller follows a sequence of instructions, one at a time, to carry out a program. However, because the microcontroller is so physically small, usually we carry out most of the development of the program on a PC, and only copy the program onto the chip when we hope (often wrongly) that we have found all the problems.

In this session, we connect up a PIC 32MX110F016B microcontroller in a circuit, copy a small program into it, and run it. Here is the circuit diagram for the microcontroller circuit:

Here is a photo of a completed version: br>
You can see the wires to the battery clip in the picture, but the battery happens to be unplugged.

Setting up the software

Before we can actually use this circuit, we need to set up the program on the microcontroller, and before we can do that, we need to install the appropriate software in the PC. The software we will install is:

Writing and compiling your program

Today we are writing programs in assembly language. I'm giving you a program to start with, and you will modify it. You'll also want configuration.s, which describes the operating environment. Much of the code in Lights.S is initialization. I tried to comment it well, but to understand it completely, you'd need to read through The quick reference for MIPS instructions and the datasheet for the PICM110 family.

The program begins at the label

and goes through the instruction on each line, then moves to the next line, unless the instruction is a branch, jump, or return instruction. Instructions in those families execute the instruction following (the branch delay slot) and then transfer control to another sequence of instructions.

The names of the various pins and registers are hidden inside a file which we include at the top of the program in the line

#include <p32xxxx.h>

Complete the wiring

I (think) I initialized fourteen pins, enough to display two digits. The picture above only shows one segment of the display wired up with a resistor between it and its port, but you should connect the other thirteen segments as well.

Then complete the program. I only wrote code to put my initials onto one digit, but I want you to make your name flow across the two 7-segment displays, so that (for example) they'd display:

St / tE / EP / PH / HE / En/ n / T / TA / AY / YL / Lo / or / r / / S / ...

Writing your own program in MPLABX

MPLABX has a builtin editor and simulator, which you can use to debug your code.

Set up a new project, by choosing File > New Project. This will be a Microchip Embedded project, a Standalone Project. On the next screen, specify the 32-bit MCU Family, and the first chip on list is the PIC32MX110F016 chip; on the following screen, say that you'll debug using the simulator. On the next screen, you choose the xc32 toolchain, of which the assembler is a part, and on the final screen you give the project a name.

To add a new file with the .S extension (case sensitive!) choose File > New File > Other > emptyfile and type the name and .S extension.

To test your code, choose Debug > Debug project

The simulator supports breakpoints; that is, you can mark a spot for execution to stop by clicking in the left margin of the builtin editor (on the line number.) You can step through your code in the simulator using the icons, or you can just let it run from breakpoint to breakpoint. You can view various parts of the CPU memory by choosing Window > PIC Memory Views, and viewing the Peripheral Registor memory for PORTA and PORTB tells you which pins the program has turned on -- not quite as convenient to read as the LEDs on the board, but easier to debug with.

When you think you've found all the bugs, you can program the flash in the microController.

Use the PicKit2 and the pic32prog software to program the chip

Use Project > Rebuild Project to get a current version of the program into (in my case) /home/staylor/MPLABXProjects/YNiLights.X/dist/default/production/YNiLights.X.production.hex

Place the PicKit2 programmer on the six pin programming header as shown: Plug the PicKit2 USB cable into the PC. I usually remove the capacitor from pin1 to ground at this point, although sometimes things work without doing so.

Check your circuit by typing the command

This is called a "probe" command; if all goes well, the software will report on your chip. If it can't find it, you may have a wiring error.

Now program the chip by typing (in my case)

pic32prog /home/staylor/MPLABXProjects/YNiLights.X/dist/default/production/YNiLights.X.production.hex
If all goes well, the software will tell you how many bytes it programmed and how long it took.

Run your program by removing the programmer, replacing the capacitor between pin 1 and GND, and connecting the power.

What to turn in

I want you to to prepare a word document which includes: Submit it in Blackboard.