4

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?

Randomblue
  • 11,003
  • 31
  • 106
  • 178

3 Answers3

4

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.

http://www.kwantlen.ca/science/physics/engineering/APSC1299/files_for_lab/pragma_config.html

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

pjc50
  • 46,725
  • 4
  • 65
  • 126
4

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.

Further reading:

m.Alin
  • 10,738
  • 19
  • 63
  • 89
  • 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 02 '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 02 '12 at 14:15
  • @Randomblue Have a try and let us know if it works :-) – m.Alin Nov 02 '12 at 15:29
1

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?

TDL
  • 439
  • 1
  • 5
  • 12