## Monday, February 13, 2012

### Getting started with the PIC24FJ64GB002 and PIC32MX110F016B

Microchip's new MPLABX development environment (free download) came out of beta a few weeks ago and the PICkit3 programmer/debugger was on special offer. So I figured this was as good a time as any to pick up a PICkit3 and to get a 'hello world' program running on the PIC24FJ64GB002 and PIC32MX110F016B samples I had in my parts drawer. Both are available in 28 pin DIP packages. (Can't beat DIPs and breadboards for quick prototyping!)

Programming the 8 bit PIC range such as 12F and 16F series with the PICkit2 programmer is simple: just connect the five ICSP pins MCLR, Vdd, GND, PGD, PGC from the programmer to the corresponding pins on the chip. Nothing else is needed. The chip can then be probed and programmed. I prefer to use the command line tool.  For example to verify that a chip was present:
pk2cmd -P
and then I'd upload myprogram.hex firmware with something like
pk2cmd -P -Fmyprogram.hex  -M -R
and apply power at 3.3V for testing (internal oscillator must be configured if there is no external crystal):
pk2cmd -P -T -A3.3

The PIC24FJ64GB002 and PIC32MX110F016B chips are not as straight forward. My naive attempt at treating them like the 8 bit PICs didn't work. I noticed searching Microchip's forums that others were have difficulty also. Like most problems relating to programming MCUs the answer is in the datasheet. You either need to read it cover-to-cover or fumble around and experiment. I'm still not sure which approach gets you there faster, but the latter is more fun :-)

There are a few important differences when compared to the older 12F and 16F series. These chips use a dual power supply. The CPU core runs at a lower voltage (usually 2.2V) and the on-chip peripherals run off a higher voltage (usually 3.3V). The core power can be configured to be supplied from an on-chip source or externally. The on-chip supply is selected by grounding the DISVREG pin and connecting Vcap to ground via a 10µF capacitor [Reference PIC24FJ64GB004 family datasheet, section 2 "Guidelines for getting started with 16-bit microcontrollers"].

Another important difference: the chip must be powered up, even for probing and programming. This is not necessary for the 8 bit PICs that I've been using. This one threw me for a bit: MPLABX with PICkit3 kept telling me no target was connected. Finally I twigged that I should have power applied:
right-click on the project and select 'Properties', select PICkit3, select the Power category from the pull down menu, select the desired voltage (usually something close to 3.3V) and check "Power target circuit from PICkit3" checkbox.

So to summarize the setup for both chips (electrically identical, just different names/functions on some pins).

 PIC24FJ64GB002 MCLR (pin 1) to programmer MCLR 10k resistor (approx) between MCLR and Vdd Vdd (pin 13) to programmer Vdd Vdd (pin 28) to programmer Vdd Vss (pin 27) to programmer Gnd Vss (pin 8) to programmer Gnd PGED (choose from port 1,2 or 3) to programmer PGD PGEC (chose from port 1,2 or 3) to programmer PGC. Must be the same port as that used for PGED. Vcore (pin 20) to 10µF capacitor to Gnd DISVREG (pin 19) to Gnd Decoupling capacitor between pins 28,27 Decoupling capacitor between pins 13,8 PIC32MX110F016B MCLR (pin 1) to programmer MCLR 10k resistor (approx) between MCLR and Vdd Vdd (pin 13) to programmer Vdd AVdd (pin 28) to programmer Vdd AVss (pin 27) to programmer Gnd Vss (pin 8) to programmer Gnd PGED (choose from port 1,2 or 3) to programmer PGD PGEC (chose from port 1,2 or 3) to programmer PGC. Must be same port as that used for PGED Vcap (pin 20) to 10µF capacitor to Gnd Vss (pin 19) to Gnd Decoupling capacitor between pins 28,27 Decoupling capacitor between pins 13,8
You should now be able to program the chip with MPLAB and the PICkit3.

Finally the configuration bits must be set just right if you want the PICkit3 in-circuit debugger to work. The syntax used to defining the configuration bits seem seems to vary from compiler to compiler. For the PIC24FJ64GB002  this is what worked for me using the Microchip C30 C compiler version 3.30c:

_CONFIG1(JTAGEN_OFF
& GCP_OFF
& GWRP_OFF
& ICS_PGx2  // NB: set to correct channel
& FWDTEN_OFF
& WINDIS_OFF
& FWPSA_PR32
& WDTPS_PS8192);
_CONFIG2(IESO_OFF
& FNOSC_FRCPLL
& OSCIOFNC_ON
& POSCMOD_NONE
& PLL96MHZ_ON
& PLLDIV_DIV2
& FCKSM_CSDCMD
& IOL1WAY_OFF);
_CONFIG3(WPFP_WPFP0
& SOSCSEL_IO
& WUTSEL_FST
& WPDIS_WPDIS
& WPCFG_WPCFGDIS
& WPEND_WPENDMEM);
_CONFIG4(DSWDTPS_DSWDTPS3
& DSWDTOSC_LPRC
& RTCOSC_LPRC
& DSBOREN_OFF
& DSWDTEN_OFF);

The configuration bits I found that worked for the PIC32MX110F016B (Microchip C32, v2.02) are:

#pragma config FPLLODIV = DIV_1
#pragma config FPLLMUL = MUL_20
#pragma config FPLLIDIV = DIV_2
#pragma config FWDTEN = OFF
#pragma config FPBDIV = DIV_1
#pragma config WDTPS = PS1
#pragma config FCKSM = CSECME
#pragma config OSCIOFNC = OFF
#pragma config POSCMOD = HS
#pragma config IESO = ON
#pragma config FSOSCEN = OFF
#pragma config FNOSC = PRIPLL
#pragma config CP = OFF
#pragma config BWP = OFF
#pragma config PWP = OFF
#pragma config ICESEL = ICS_PGx2 // NB: set to correct channel
#pragma config DEBUG = OFF


Note that both of these chips have several programming ports. Any can be used for programming. However for debugging you must specify which port is being used in the configuration bits.

Jacques Samoun said...

Hi,
just found your settings extremely useful as I was actually experiencing problems debugging my PIC24FJ64GB002 design.
On top of the settings, there is one important and completely stupid thing to remember ...

put your files, project, etc ... in a location for whihc the PATH does not contain any spaces or strange characters !!!!
Interesting that this recurring issue has still not be solved.
On XP, this is still an issue.
Jacques

Crw37 said...

Holy cow thanks! These configs worked perfect for me! for the PIC24FJ64GB002. I have been fighting to light up just a single LED but was confused on what these configs to be set at. This was very helpful. Thanks again.

jdesbonnet said...

You're welcome :) BTW: another one to try is the LPC1114FN28: an ARM Cortex M0 MCU in a 28pin DIP package. About \$2.50 in single quantities. I've got a blog post on a minimal setup for that coming up (absolutely no external components required in this case!).

Abraxas said...

I'm not getting the syntax of the configuration bits. Is this coming from a header file ? Macros ? Can you just explain the first few lines so I can figure out how it works or where they are from ? Thanks.