# PIC16: How do I modify the configuration words?

As I understand, the configuration words are different to the standard 8 bit registers. They are 14 bit wide, and they can only be accessed in "programming mode".

From reading the datasheet I do not understand how to enter programming mode and then modify the configuration word. Can I modify the programming word from my C code (e.g. within the main function), or should I somehow instruct my programmer (PICKIT 3) to do some magic before the main function is reached?

-

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:

1. From the main menu select Window ▶ PIC Memory Views ▶ Configuration Bits
2. 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.
3. Click on the Generate Source Code to Output button
4. 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.

-
 Thanks. The Generate Source Code button will generate #pragma config lines, which isn't helpful for me considering the comment you left above: "The construct #pragma config is only supported for the PIC18 devices in the XC8 compiler." – Randomblue Nov 2 '12 at 13:04 @Randomblue What version of XC8 do you use? Just found out that in the latest version (XC8 1.11), you can use #pragmas with PIC16 devices – m.Alin Nov 2 '12 at 14:15 I'm using 1.11. That's great, thanks. – Randomblue Nov 2 '12 at 14:28 @Randomblue Have a try and let us know if it works :-) – m.Alin Nov 2 '12 at 15:29

It has to be done by the programmer (technically this is what the PICKIT 3 is, not a bootloader).

You can configure the configuration words from MPLAB, or by inserting "#pragma config" statements into your C code. This is preferable, as it ends up checked into source control and doesn't get lost.

You cannot modify the configuration words from within a PIC program.

-
 Thanks. It seems there isn't an equivalent of the PIC18 CONFIGURATION SETTINGS ADDENDUM which defines all the pragmas for the PIC16. – Randomblue Nov 2 '12 at 9:59 Yeah, all the examples I find using pragmas are PIC18s, not PIC16s. – Randomblue Nov 2 '12 at 10:09 Hmm, you're right, and now I can't find this either. Are you using MPLAB or MPLAB X? In the latter it's Window -> Pic memory views -> Configuration bits. – pjc50 Nov 2 '12 at 10:23 @Randomblue The configuration setting syntax also depends on the compiler you use. Which one is it? – m.Alin Nov 2 '12 at 10:23 I'm using MPLAB X and XC8. – Randomblue Nov 2 '12 at 10:42

Similar question to one I asked not long before you. That thread may also provide relevant information for you:

How do you set the configuration bits for a PIC 16F1829 in MPLAB X?

-