Microcontrollers

Getting started

To prepare and test your system for use with the programs in this library collection, perform the following steps:
  1. Software installation - Install the necessary software on a Windows-based PC. All the necessary software is free.
  2. Hardware bring-up - Build or purchase a PIC24-based board.
  3. Test the PIC - Using the bootloader, program the PIC with the echo program and test. This verifies proper installation of your hardware/software setup.

Software installation

  1. Download and install the latest version of the MPLAB IDE (the Full Release Zipped Installation under the Downloads section near the bottom). For v8.10, the buttons to click during installation are: next, next, I accept/next, next, next, next, next, yes, finish.
  2. Download and install the latest student version of the MPLAB C Compiler for PIC24 MCUs. Notes:
    1. MPLAB must be installed before the compiler
    2. Accept all the defaults when installing; in particular, click "Yes" to the "Modify Environment?" question (see images below). No new program will show up on your start menu; instead, the compiler will now be available in MPLAB. That enables the compiler to properly register itself with MPLAB.
    3. See the BugMeNot for a userid/password if you don't want to create one.
  3. If you do not have the Microsoft .NET framework installed, download and install the .NET runtime by visiting the Microsoft Download Center and searching for '.NET runtime'. Install version 2.0 or later. If you're not sure if the .NET framework is installed, try running the bootloader in the following step; if it fails, download the .NET framework.
  4. Download and unzip the latest version of the PIC24 library collection. Notes:
    1. We recommend installing to a directory without spaces; on pre-Vista windows, installing on the desktop violates this rule, since the desktop directory is "C:\Documents and settings\...".
    2. Test the bootloader, a .NET application, by executing bin\winbootldr.exe. A GUI should pop up. If that fails, consult the bootloader documentation for troubleshooting steps.

Hardware bring-up

There are several options for acquiring the hardware necessary to run a PIC24:

Wiring up a PIC24

  1. Begin by following the prototyping walk-through.
  2. Using an ICSP programmer, program the bootloader onto your PIC.
  3. Install the FTDI drivers (use the setup executable link) to communicate with your PIC via an FTDI cable.

SparkFun PIC24 breakout board

  1. Purchase a SparkFun PIC24 breakout board. This comes with bootloader firmware already installed as well as the 'reset.c' application (code/chap8/reset.mcp) with the bootloader's baudrate set to 57600.
  2. At a minimum, install 0.1" pitch headers in the 6-pin connection for use with an FTDI cable. Installing headers in the remaining positions allows access to much of the functionality of the chip.
  3. Install the FTDI drivers (use the setup executable link) to communicate with your PIC via an FTDI cable.
  4. For advanced users, purchase a SparkFun ICSP connector for use with an ICSP programmer.
  5. This video has more information on the Sparkfun PIC24 breakout board.

Explorer16 board

The Explorer16 is a development board available from Microchip - it comes in 100 pin and 44 pin versions. The CPU is set by a plug-in module, of which each board has a few available. The directory /code/explorer16_100p contains a few examples for the Explorer16/100 pin board. These projects can use the source files from the other directories, but the macro EXPLORER16_100P either must defined in the include/pic24_all.h file or it must be added to the MPLAB project (Use Project->Build Options-> Project, click on the MPLAB C30 tab, and in Macro Definitions click 'Add', and add EXPLORER16_100P). The EXPLORER16_100P macro causes the default UART to be UART2 since that is what is used on the Explorer16/100 pin board. It also causes the clock option to use the external 8 MHz oscillator, with the PLL used to produce a 16 MHz FCY for PIC24F and 40 MHz for PIC24H.

Microchip Microstick

The Microchip Microstick is a new low-cost platform for PIC24 and dsPIC33 experimentation. It is a breakout board for 28-pin devices that offers on-board debugging and in-circuit programming (like a PICKIT). The board plugs into a breadboard, with access to all pins for maximum versatility.  This page shows how to use the Microchip Microstick with the book examples.

FTDI cable

A simple solution for connecting your PIC24 to a PIC is an FTDI TTL-232R-3V3 USB to TTL level serial converter cable. One end plugs into a PC's USB port while the other provides TTL-compatible serial I/O lines which can be directly connected to your PIC's serial I/O pins. In addition, the cable provide +5V, which can be used to power your board through a regulator. For more information, see the FTDI datasheet. Sources:
  • Digi-key, Mouser, etc.
  • SparkFun sells their own FTDI board, which provides the same functionality as the cable.

ICSP programmers

If you purchase an unprogrammed PIC24 chip, you'll need to program it with the bootloader, after which the bootloader can be used to program the PIC24 with your code. If the bootloader gets corrupted, it must be reprogrammed. Finally, advanced users may which to remove the bootloader and program the PIC24 via ICSP. Several options exist:
  • Microchip sells the PICkit2, a low-cost programmer; only the PICkit2, not the various starter kits, are required. This is the solution used by the authors of this site. The direct link to buy just a PICkit2 by itself (part number PG164120) from Microchip is here. This video discusses using the PICkit2 with PIC24 CPUs.
  • A second, more expensive option is the Microchip ICD2.
  • Third-party programmers can (possibly) be used.
To program your PIC with the bootloader using a PICkit2:
  1. Install the PICkit 2 software from Microchip's website.
  2. Connect the PICkit 2 programmer up to your board. The arrow on the PICkit corresponds to pin 1.
    • The PICkit may need to have an OS downloaded. If the PICkit software complains, click Tools -> Download PICkit 2 Operating System.
  3. In the PICkit software, select Device Family -> PIC24.
    • If the PIC is not automatically found, press and hold the MCLR button on the board. When you release the MCLR button, click the "Read" button in the PICkit software.
    • Check to make sure your device is properly recognized and that the
  4. Load on the bootloader via File -> Import Hex. Be sure to select the appropriate PIC architecture. Additionally, take note of the information contained in the bootloader's name (i.e. baudrate).

Testing with echo

With the necessary software installed and hardware acquired, test your system by programming it with a simple echo program. The echo program waits for a UART1 RX character, increments it, then echos it back. To test:
  1. Before programming the PIC with the echo program, be sure to have all the required software installed and the bootloader loaded (see the steps above).
  2. Open MPLAB, then open the echo project using Project -> Open.
  3. The project should compile as is; build the project with Project -> Make (or press F10). To troubleshoot:
    1. Click Configure -> Select Device and make sure the appropriate device is selected.
    2. Click Project -> Build Options -> Project. Under the Directories tab, "Show directories for:" the Include Search Path and make sure the relative paths are meaningful for your setup.
  4. Start the PIC24 bootloader application by opening bin/winbootloader.exe.
  5. Hook the USB cable up to your computer and to your PIC.
  6. In Bully bootloader, select the appropriate COM port and baud rate and check "OpenCOM."
    1. The baud rate is determined by the bootloader firmware loaded on the target PIC24 micro (the baudrate is typically 57600).
    2. If you do not know which COM port to use, in Windows select Start -> Control Panel -> System (or Windows Key + Pause|Break). Select the Hardware tab and click Device Manager. In the new window, expand the "Ports (COM & LPT)" entry and look for "USB Serial Port (COMx)".
  7. Click the HexFile button and navigate within the Open dialog box to your MPLAB's project directory.
  8. Select the appropriate .hex file, and click Open.
  9. Press and hold the MCLR button on your miniBully24. Immediately after you release the MCLR button, click the Program button in the Bully bootloader. You should see the lower output box scrolling with information.
  10. At the top of Bully bootloader is a white text input field. Type the letter 'a' into the box, and click Send.
  11. Check to see that the letter 'b' appeared in the top output box, just below the input text field. More bootloader documentation is found at Bully Bootloader GUI documentation.

Hardware Debugging Tools


A extremely useful hardware debugging tool is a USB logic analyzer. The Intronix Logicport is an inexpensive one that can prove invaluable in tracking down hardware problems. Here is a screenshot for the reverse string application of Chapter 10 that demonstrates two PIC24 CPUs communicating over the SPI port.