I do not quite understand how I'm meant to enter programming mode and then modify the configuration word? Can I modify the programming word in my C code (e.g. within the main function)?
The configuration words are mapped in program/instruction memory. They are mapped at an address location which is not accessible during normal device operation (it can be accessed only during programming mode). These configuration bits specify some of the modes of the device, and are programmed by a device programmer, or by using the In-Circuit Serial Programming (ICSP) feature of the midrange devices. So you should set these configuration bits in your code, but outside of any functions, using a compiler specific #pragma or macro.
From the XC8 User Guide:
The configuration bits for baseline and mid-range devices can be set with the
__CONFIG macro which was supported in HI-TECH C, for example:
#include <xc.h>
__CONFIG(WDTDIS & HS & UNPROTECT);
To use this macro, ensure you include in your source file. For
devices that have more than one configuration word, each subsequent
invocation of __CONFIG() will modify the next configuration word in
sequence. Typically this might look like:
#include <xc.h>
__CONFIG(WDTDIS & XT & UNPROTECT); // Program config. word 1
__CONFIG(FCMEN);
The easiest way to set your device's configuration bits is through MPLAB X. Instructions taken from here:
- From the main menu select Window ▶ PIC Memory Views ▶ Configuration Bits
- In the configuration bits window, click on any value in the Option column and it will turn into a combo box that will allow you to select the value you desire.
- Click on the Generate Source Code to Output button
- The IDE will automatically generate the code necessary to initialize all the configuration bits to the settings you specified in the window. This code may now be copied and pasted into one of your source files, or you may save it to its own file and add it to your project. To save the file, right click anywhere in the output window and select Save As from the popup menu.
Further reading:
pragmas are PIC18s, not PIC16s. – Randomblue Nov 02 '12 at 10:09