CSCI 255 Lab

Lab 11 -- PIC24 Control of Switches and LEDs

(This lab was inspired by a laboratory exercise presented in ECE3724: Microprocessors developed by Jones, Reese and Bruce and offered at Mississippi State University.)

Part 0: Overview & Review

In this lab, you will implement switch and LED I/O using the PIC24. You will compile C programs in MPLAB and transfer the executable forms of those programs to the PIC24 using the "bully bootloader" developed at Mississippi State University. The programs that we'll be working with are located in the example code archive that you downloaded to a USB memory stick in Lab 7.

Checking your PIC24 development board

Before you can begin this activity, you must have a fully functional PIC24 development board constructed as described in Lab 10. So... let's begin by verifying the construction of your board. Exchange boards with the team next to you, and verify that the construction of your neighbor's board is correct. Spend the time to check each step of the construction as described in Lab 10; this is time well spent! Discuss any anomalies that you find with your neighbor and assure that all errors are corrected before proceeding. Ask your lab instructor for help if you need it.


 

Part 1: Downloading a Program to the PIC24 Chip using the Bootloader

We will use a "bootloader" to transfer programs to the PIC24 chip via a USB-to-Serial cable. The "bully bootloader," developed at Mississippi State by the authors of your text, consists of two programs: the bootloader firmware that must be installed on the chip, and the bootloader interface software that runs on your desktop PC.

The "bully" bootloader firmware has already been installed on your PIC24 processor---this must done using something like the "PICkit 2." If you are interested in programming a PIC24 processor at home, all of the software used in the lab is freely available, but you'll need something like the PICkit 2 to load the bootloader firmware to the PIC24 chip.

Follow the steps below to load a program onto your PIC24 chip using the bully bootloader.

  1. Plug your USB-to-Serial cable into the 6-pin header on your board and into your PC. Position the power switch on the board to receive power from the USB connection.

  2. Copy bin/winbootldr.exe and bin/devices.txt from the PIC24 example code archive to a target directory of your choice. These two files are the bootloader interface software. Execute winbootldr.exe to run the bully bootloader. Tell your instructor if the bootloader does not run.

  3. In the bootloader window, select the COM port for the USB-to-Serial cable connection. If you do not know the COM port number, then use the Device Manager under Control Panel -> System -> Hardware -> Device Manager to see the serial ports (and other hardware) on your PC. Double-click on Ports, and note which port is labeled as a "USB serial port;" that's the COM port number to specify in the bully bootloader. Select the 57600 baud rate in the bootloader window, and click the OpenCom checkbox to open the port.

    bully

  4. Start MPLAB. Open the project chap8/echo.mcp in the PIC24 example code archive (note: you must start MPLAB first, then open the project file – do not open the project file by double-clicking on it since it does not have a workspace file with it). This project reads a character from the serial port, increments it, and echoes it back (so ‘1’ is echoed as ‘2’, ‘a’ as ‘b’, etc.). This is the program that we'll transfer-to and run-on the PIC24 chip.

  5. Under the MPLAB Configure menu option, select Select Device... and designate PIC24HJ32GP202 as the appropriate device in the drop-down device list. Select OK at the bottom of the window.
    select device

  6. Use the Build All option under the Project menu to compile the echo.mcp project with the C compiler installed as part of MPLAB. If your compile is not successful, ask your instructor for help.

  7. Use the HexFile button in the bootloader to browse to the echo.hex in your example code archive; this is the file produced during compilation in the step above.

  8. Turn off power to your PIC24 processor, then turn it back on again. Click the Program button in the bootloader before 2 seconds have lapsed in order to download the hex program to the PIC24. You should see some text appear in the lower window indicating that the program is downloading.

  9. Test the echo.c program functionality by using the Send button to send some text to the PIC24. You should see it echoed back, with the characters incremented by 1.

  10. Modify the echo.c program to increment the character by 2 instead of by 1. Verify this operation by running it on your PIC24 chip.

Checkoff

Show your instructor the modified echo program in action along with the source code.  

Part 2: LEDs and Switches

Flashing an LED

Begin your investigation of switch and LED I/O with the ledflash project from chapter 8 of the example code library. Select Close from the Project pull-down menu to close the echo project. Use Open from the Project pull-down menu to open chap8/ledflash.mcp. Study the code in ledflash.c and then view this silent tutorial to better understand the statements that you see.

Compile the program and use the bully bootloader to transfer it to the PIC24. Because your processor has already been programmed to blink the LED on pin 15, it will be difficult to detect any difference in its behavior. Modify the blink rate specified in ledflash.c, and transfer it again. Verify that you can see a new behavior.

Add a Switch and an LED

Close the ledflash project and open chap8/ledtoggle_nofsm.mcp. Open ledtoggle_nofsm.c in MPLAB, and study the code. Identify the hardware configuration expected by the program. Create that configuration on your breadboard and then run the program on your PIC24. Verify the expected performance and then show your instructor.

Checkoff

Show your instructor the ledtoggle_nofsm program in action.