Snippet:Configuration bits

From Curuxa

Configuration bits are individual settings that change the low-level behavior of a microcontroller. They are not stored in RAM but they are not part of your program either. They are special settings you must define in your code so they'll be transferred to the microcontroller while you are burning/programming a binary file with your program into it.

Each microcontroller supports its own configuration bits. You can find them under the section "Special features of the CPU" in the datasheet. Then, you should need to check the library for each microcontroller (pic16f88.h, pic16f887.h...) and see how they are called in that library, so you can setup them..

Features you can usually activate/choose/configure:

  • Use internal, external, fast or slow oscillator
  • Reset microcontroller when supply voltage is lower than a certain level
  • Use a certain pin as a reset or as a digital I/O
  • Protect program memory so it can't be read by third-parties after you have programmed it
  • Delay the beginning of your program after the microcontroller has been powered up to avoid errors
  • Enable or disable watchdog
  • ...and many more depending on the microcontroller

Remember, you must setup them in your code. If you don't, then your microcontroller probably won't work because he won't know which type of oscillator it should use, etc.

Sample usage

The following examples show how to setup the configuration bits in the standard way used in most other examples: no code protection, no debug, internal oscillator, no support for reset button, watchdog disabled...

MBP8

typedef unsigned int config;
config at 0x2007 __CONFIG = _CPD_OFF & _CP_OFF & _BOD_OFF  & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO;


MBP14

typedef unsigned int config;
config at 0x2007 __CONFIG = _CPD_OFF & _CP_OFF & _BOD_OFF  & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO;


MBP18

typedef unsigned int config;
config at _CONFIG1 __CONFIG = _CP_OFF & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_IO;


MBP40

typedef unsigned int config;
config at _CONFIG1 __CONFIG = _CP_OFF & _DEBUG_OFF & _FCMEN_OFF  & _IESO_OFF & _CPD_OFF & _LVP_OFF & _BOR_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO;


Sample program

// sample program for MBP40
 
#include <pic16f887.h>
 
//now your configuration bits
typedef unsigned int config;
config at _CONFIG1 __CONFIG = _CP_OFF & _DEBUG_OFF & _FCMEN_OFF  & _IESO_OFF & _CPD_OFF & _LVP_OFF & _BOR_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO;
 
 
//you can define more things here...
 
void main() {
    //setup stuff...
 
    while(1) {
        //do things here...
    }
}