PIC24 Tutorial – Part 3 – Device Configuration – Config Words (Fuse Bits)

For this tutorial you’ll need the following:

  • A PIC24 hardware platform – such as our picoTRONICS24 or nanoTRONICS24 microcontroller development boards
  • Microchip’s FREE MPLAB X IDE – make sure you’ve got the latest version – they are updating it all the time
  • Microchip’s FREE C30 or XC16 C Compiler – also make sure you have the latest version
  •  We also assume you have working knowledge of C/C++ – if you don’t have this there a many good C/C++ tutorials availible on the internet.

This tutorial has been designed to work seamlessly with our picoTRONICS24 series development boards. Click here to download the example project for this tutorial – it’s the same project we used for Tutorial 2.

What’s a Config Word?

All Microchip microcontrollers contain a series of configurable settings that are arranged outside of your normal program. The PIC24F series of microcontrollers are no exception. These settings are commonly called Configuration Words, Configuration Bits, Config Words or Fuse Bits. These are special directives that tells the microcontroller what to do on the most basic level – ie at a lower level than you program. In the PIC24 series devices, they include the parameters for selecting which programming pins to use, watch dog timers, JTAG programming configurations, alternate pin configurations, and most importantly oscillator selection. There maybe many more depending on your chip. This tutorial aims to give you a general overview of how to set these config words, with a particular focus on the PIC24FJ64GB004 chip used on our picoTRONICS24 and nanoTRONICS24 development boards. Although all the principles apply for whatever PIC24 series device you choose to use.

Lets Get Started

You will have noticed the following lines of code in the example code provided in the last tutorial and perhaps thought they were a bit weird. Well these are the Configuration Bits that tell the underlining PIC processor hardware how to behave.

[code language=”cpp”]
// Microcontroller config words (fuses) – get these wrong and nothing will work correctly, if at all!!
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR32 & WDTPS_PS8192)
_CONFIG2(IESO_OFF & FNOSC_FRCPLL & OSCIOFNC_OFF & POSCMOD_NONE & PLL96MHZ_ON & PLLDIV_DIV2 & FCKSM_CSECME & 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)
[/code]

As I said every microcontoller in the PIC24 series has these special registers called Configuration Bits. You can find the details of what these Configuration Bits do in your chips datasheets available from the Microchip website. They are usually listed under the section Special Features. So I guess that obvious question everyone thinks is how do I know what functions and which ones to use for my device or project? To work this out you’ll need to look at the datasheets for your particular microcontroller. Luckily the datasheets contain a detailed explanation of every possible option available on each device. Once you know which options you need for your project, it’s time to configure them up.

Now you’ve checked the datasheet and know what config bits you need it is time to add them to your project. To do this you need to look in the MPLAB X header file for your device. We’re using the PIC24FJ64GB004, which means we need to look in the p24FJ64GB004.h header file (this is correct for the XC16 compiler, if you’re still using C30 it is a different header file). In here you’ll find a series of macros that are used to set the configuration bits. Below the macro declarations there is the list of options which correspond to the options listed in the datasheets.

Example Configuration Bits

To finish off this tutorial we’ll go through a few of the configuration bits used in our example project and explain what they do. Lets start with Config Word 1

Config Word 1

[code language=”cpp”]
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR32 & WDTPS_PS8192)
[/code]

Now what does each of these actually mean:

JTAGEN_OFF – This turns JTAG off on our microcontroller. Our particular device, whilst it has JTAG, doesn’t support it fully so it isn’t that useful, thus we turn it off.
GCP_OFF – This disables code protection – we don’t need this as we’re happy for people to read the code off the microcontroller
GWRP_OFF – Allow writes to the flash memory
ICS_PGx1 – Use pins PGEC1 and PGED1 for programming and debugging
FWDTEN_OFF – Disable the watchdog timer – you’ll notice in later tutorials that we turn this on
WINDIS_OFF – Not relevant as the watchdog timer is disabled by the above config bit
FWPSA_PR32 – Not relevant as the watchdog timer is disabled by the FWDTEN_OFF config bit
WDTPS_PS8192 – Not relevant as the watchdog timer is disabled by the FWDTEN_OFF config bit

Config Word 2

Configuration Word 2 is particularly important, as if you get the oscillator settings wrong it is quite likely your PIC won’t function correctly or start-up at all. If you are having problems getting your program to load this is probably a good spot to start your debugging.

[code language=”cpp”]
_CONFIG2(IESO_OFF & FNOSC_FRCPLL & OSCIOFNC_OFF & POSCMOD_NONE & PLL96MHZ_ON & PLLDIV_DIV2 & FCKSM_CSECME & IOL1WAY_OFF)
[/code]

IESO_OFF – Disable to speed oscillator statup – we don’t need this
FNOSC_FRCPLL – Use the fast RC Oscillator with Postscaler and PLL module (FRCPLL) – we need this as we use USB on our devlopment boards with the internal oscillator. A very useful feature of the PIC24FJ64GB004 device!
OSCIOFNC_OFF – Use the OSCO pin as a clock output (CLKO) – if you want to use RA3 as an IO pin you’ll need to change this to OSCIOFNC_ON
POSCMOD_NONE – Primary Oscillator disabled – we’re using the internal oscillator
PLL96MHZ_ON – 96 MHz PLL Startup is enabled automatically on microcontroller start-up – we want this for USB
PLLDIV_DIV2 – Oscillator input divided by 2 (8 MHz input) – again we want this for USB
FCKSM_CSECME – Clock Switching and Fail-Safe Clock Monitoring enabled in software (we use this in a future tutorial)
IOL1WAY_OFF – The IOLOCK bit can be set and cleared using the unlock sequence

Config Word 3

[code language=”cpp”]
_CONFIG3(WPFP_WPFP0 & SOSCSEL_IO & WUTSEL_FST & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPENDMEM)
[/code]

WPFP_WPFP0 – Write Protection Flash Page Segment Boundary – Page 0 in our case, which is disabled
SOSCSEL_IO – Secondary Oscillator pins have digital I/O functions (RA4, RB4)
WUTSEL_FST – Voltage regulator (the on chip one) wake-up time – fast regulator start-up time used
WPDIS_WPDIS – Segmented code protection disabled – not something we use (or need)
WPCFG_WPCFGDIS – Last page and Flash Configuration words are unprotected – again something we don’t need
WPEND_WPENDMEM – Write Protect from WPFP to the last page of memory – another code protection bit we don’t need

Config Word 4

[code language=”cpp”]
_CONFIG4(DSWDTPS_DSWDTPS3 & DSWDTOSC_LPRC & RTCOSC_LPRC & DSBOREN_OFF & DSWDTEN_OFF)
[/code]

DSWDTPS_DSWDTPS3 – Deep Sleep Watchdog Postscale equals 1:128 (132 ms) – not relevant as we aren’t currently using the deep sleep watchdog
DSWDTOSC_LPRC – Deep Sleep Watchdog Timer Oscillator Select – DSWDT uses Low Power RC Oscillator (LPRC) – again not relevant
RTCOSC_LPRC – RTCC Reference Oscillator Select – RTCC uses Low Power RC Oscillator (LPRC) – we’ll use this in a later tutorial. If you have the secondary oscillator soldered onto your picoTRONICS24 development board you can use the RTCOSC_SOSC config bit instead.
DSBOREN_OFF – Deep Sleep brownout disabled
DSWDTEN_OFF – Deep Sleep Watchdog Timer disabled

Wrapping it all up

Hopefully this tutorial has helped you understand how the configuration words are setup in a PIC24F series microcontroller. If you are still a bit confused by them or aren’t sure what settings to choose, we recommend you use our default configuration – this should work in all simple setups.

Good luck!

Comments are closed.