For years, Microchip PIC microcontrollers dominated; PIC16F84 hacks and projects are everywhere. The 8-bit 16F and 18F lines are supported by several coding environments and easy-to-build serial port programmers. Microchip’s 16-bit PIC24F is cheaper, faster, and easier to work with, but largely absent from hacks and projects.
We recently used a Microchip PIC24F microcontroller in a mini web server project, but didn’t find many introductory references to link to. In this article we’ll cover some PIC 24F basics: support circuitry and programming options. We’ll also talk about our favorite features, and how we figured them out. Our next article will outline a web server on a business card based on the PIC 24F.
The basic circuit
Main system power supply
Peripherals and pins on the 24F PICs operate between 2.0 and 3.8volts. This is a big advantage over older PICs because the 24F can directly interface modern 3.3volt components like SD memory cards. Some 16F and 18F PICs will run at 3.3volts, but usually at drastically reduced speeds. As always, put a 0.1uF capacitor between each power pin and ground to decouple the chip from the power supply (C1, C2).
Core power supply
The processor core requires a separate 2.5volt supply. A built-in 2.5volt regulator can be enabled by connecting the DISVREG pin to ground, and placing a 10uF capacitor between the Vcap/VDDCORE pin and ground (C3). We’ve not experienced any problems using a 10uF low ESR electrolytic capacitor, but in the future we’ll use a tantalum capacitor as specified in the datasheet.
Speed and crystal
PIC 24Fs have a max clock speed of 32MHz, and complete one operation every 2 clock cycles for a top speed of 16 million instructions per second (MIPS). Most 24Fs have an internal 8MHz oscillator, but you can also use an external crystal for a more precise timebase. An internal phase lock loop (PLL) can multiply any clock signal by four.
We used a common option: 8MHz internal oscillator multiplied by four (32MHz), with full IO functions on the external oscillator pins. The clock mode is set with CONFIG2. Use these settings to run a PIC 24F at 32MHz using the internal oscillator and PLL:
// Internal FRC OSC with 4x PLL @ 32MHz //from p24FJ64GA002.h: //FNOSC_FRCPLL - internal oscillator //OSCIOFNC_ON - enable the oscillator pins as IO //POSCMOD_NONE - Primary (external) oscillator disabled _CONFIG2(FNOSC_FRCPLL & OSCIOFNC_ON &POSCMOD_NONE)
Microchip’s standard 5 wire in circuit serial programming (ICSP) connection is used to program the 24F. ICSP consists of a clock line (PGC), bi-directional data line (PGD), master clear and reset (MCLR), and connections to power (V+) and ground (GND).
The MCLR function resets the chip when voltage levels are too low to operate. Enable it with a 2000 (2K) ohm resistor (R12) from the system power supply to the MCLR pin. Optionally, add a button (S1) from MCLR to ground for a manual reset switch. The programmer also connects to the MCLR pin to reset the PIC and control programming modes.
PIC 24Fs have several sets of programming pins labeled PGDx and PGCx. Choose the set most convenient for your design. One catch: you can’t use the clock pin of one set and the data pin of another, you have to use the same pair.
The primary pin pair used for debugging is programmed in CONFIG1 with the ICS_PGX option. This only effects debugging; programming is still possible from any pin pair.
Coding and Programming
Unfortunately, the 24F can’t be programmed with the hobbyist-favorite serial port programmers. These are usually 5volt programmers that place 13volts on the MCLR pin. 24F PICs are rated for 3.8volts maximum on the MCLR and programming pins, old serial port programmers will destroy them.
The ICD2 is Microchip’s cheapest programmer for the full 24F line. An education discount is available if you have a .edu email. There are numerous clones too, most notable is the Olimex PIC-ICD2 clone, also sold by Sparkfun. We’ve never used it, but it’s supposed to be an exact clone. You can also try your hand at building a DIY ICD2 clone, we’ve had luck with the PiCS Rev B in the past. You’ll probably need to build an adapter to use a homebrew ICD2 with a PIC 24F.
MPLAB is a free development environment for coding, compiling, and debugging all PIC microcontrollers. We like to program in C, so we downloaded the free, evaluation/student edition of the Microchip C30 compiler that integrates into MPLAB. HI-TECH’s C compiler is a fairly popular alternative if you’re not thrilled about MPLAB.
Microchip’s low-voltage 18FxxJ line, such as the Ethernet enabled 18F97J60, can only be programmed a few hundred times. That’s fine for production, but really unfriendly to a developer. We’re exceedingly happy to note that the 24F can be programmed at least 10,000 times.
New features and improvements
We made a list of the things we liked best about the PIC 24F after using it in a project. Not all of them are new, sometimes little improvements make designs much simpler.
8-bit vs 16-bit
C programmers won’t notice many differences between 8-bit and 16-bit architectures. Native 16-bit math operations will save you a few cycles if you do 16-bit integer math. Memory and registers are 16-bits long, meaning the default 16-bit variable type counts to 65,536, rather than 255.
Peripheral pin select
Peripheral pin select (PPS) is our favorite feature on the PIC 24F. The digital peripherals SPI, UARTs, timers, etc can be connected to almost any pin on the chip.
PCB designs get really creative because the pin arrangement on a microcontroller rarely matches that on the peripheral you’re interfacing. Compare these two designs. The design on the left uses looping, winding traces to connect a SD card without jumper wires. On the right, we used PPS to assign pins in a way that lined up perfectly with the SD card. We spent caffeine fueled nights routing the board on the left, but only hours on the other. We’ll find it difficult to ever work with a PIC 16F or 18F again because of the complete and total awesomeness of PPS.
Input and output pins are assigned differently: pins are assigned to inputs, outputs are assigned to pins. A peripheral input, such as the “serial data input” (SDI) signal of an SPI interface, is set by putting a pin number in its register. In the C30 compiler, SDI of SPI1 and SPI2 are assigned like this:
// Inputs //SDI1 B12/23/RP12 //SDI2 B1/5/RP1 RPINR20bits.SDI1R = 12; //SDI1 = PORTB12 RPINR22bits.SDI2R = 1; //SDI2 = PORTB1
Output functions are handled in the opposite way. A group of registers represent the programmable pins (RPORx). Peripheral outputs are assigned to each pin. Assign the SPI “serial data output” and “clock output” lines like this:
// Outputs //SDO1 B11/22/RP11 //CLK1 B10/21/RD10 RPOR5bits.RP10R = SCK1OUT_IO; //RP10 = SCK1 RPOR5bits.RP11R = SDO1_IO; //RP11 = SDO1 //SDO2 B3/7/RP3 //CLK2 B2/6/RP2 RPOR1bits.RP2R = SCK2OUT_IO; //RP2 = SCK2 RPOR1bits.RP3R = SDO2_IO; //RP3 = SDO2
Check the device datasheet and the IO with PPS datasheet (PDF) for a complete list of peripheral (RPINRxx) and pin (RPORx) registers.
Individually configurable pull-up/pull-down resistors
Pull-up and pull-down resistors hold inputs at a known level when there’s no other signal. Illustrated below on the left (S1), a pull-up resistor (R1) normally holds the signal high (1). A button press pulls the signal to ground (0). Without a pull-up resistor, the value on the microcontroller pin will fluctuate wildly (state undefined) until a button press pulls it to ground (0).
Internal pull-up resistors make it easier to route a button on a circuit board. An internal resistor holds the signal high until the button pulls it low, saving a resistor and power supply trace (S2). PIC 16Fs and 18Fs sometimes have an all-or-nothing pull-up on 8 pins, but the 24F adds individually configurable pull-up resistors. See the IO datasheet (PDF).
CRC hardware module
Cyclic redundancy check (CRC) values are used to verify the integrity of data. Your PC calculated CRCs for the TCP packets that carried this page over the web. The 24F has a hardware CRC module that does tedious CRC calculation without processor involvement. Check out the datasheet (PDF) and example code (ZIP).
Real time clock and calender
Microchip added a hardware real time clock and calendar module (RTCC) to every 24F. It’s always been easy to add an interrupt-based clock to a microcontroller, but this module takes care of everything without timing concerns.
The RTCC module requires a 32.768khz watch crystal (Q1) to be connected to the SOSCx pin pair. Don’t forget 2 suitable capacitors for your crystal, we used 27pF (C1,C2). There’s a datasheet for the RTCC module (PDF), and example code (ZIP).
Microchip continues their tradition of offering products in a range of package sizes. Low pin count parts are available in through-hole (DIP) and several surface mount sizes. As with all manufacturers, though, the largest, coolest, chips are only produced in surface mount packages. Microchip is a fan of 64, 80, and 100 pin thin quad flat packs (TQFP), a square chip with an equal number of pins on all sides. TQFP isn’t terribly difficult to solder, but the circuit boards can be a pain to make at home.
The past was dominated by 8-bit PIC 16F and 18F-based microcontroller projects. 16-bit PICs, however, have been largely neglected. If you’re already considering a PIC for your next project, check out the 24F series. The peripheral pin select feature alone is worth the switch — it simplifies circuit boards, reduces routing time, and saves board space. We were able to fit an entire PIC 24F web server on a business card using a home-etched PCB. Our next article will introduce this simple server prototype.
The project archive (ZIP) contains the base schematic for the PIC24FJ64GA002, and a custom 28pin part we added to an existing PIC 24F part library. Both are for use with Cadsoft Eagle, a freeware version is available for most popular platforms.