Sunday 28 May 2017

pic - 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?



Answer




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 
__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 
__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:



No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...