Using Config Bits in Microcontroller Based Projects

Microcontrollers are typically devoted to a specific objective, so whereas they’re very versatile units lately, there are a selection of various (typically conflicting) choices for a way you may want the {hardware} to work. You may suppose that this configuration is completed in software program, however what if you need the {hardware} to behave a sure means from the very begin? Conserving in thoughts that Microchip PIC units can “boot” in a matter of milliseconds, there must be a means you telling the {hardware} how one can behave prematurely of it beginning up. This text covers how one can get your microcontroller based mostly venture working the way in which you anticipate when it begins up.

Enter config bits, or, as they was once known as, config fuses. Again within the days when microcontrollers have been program-once units, you actually did blow a fuse as a way to program them. Right now, most micros have flash reminiscence that may be programmed tens of hundreds of occasions, however there are nonetheless one-time-programmable (OTP) units about.

In any case, there is a bunch of “config phrases” that outline how the micro goes to behave from the get-go. Let us take a look at a few of these choices (we’ll select the PIC18F2620 for instance) after which see how one can program them. Sourceboost provides you all of the config strings in a ready-to-use format for together with in your venture. The format has the config bits suffixed with the identical identify because the config phrase so you’ll be able to make sure that the proper bits are getting into the proper phrase. After all, your config phrases shall be totally different should you’re utilizing a distinct microcontroller, however you must be taught sufficient right here to level you in the proper route.

Within the 18F2620, the config bits are saved in flash, however at a excessive reminiscence location. They’re saved at 0x300001 and upwards. For historic causes as a result of means reminiscence was beforehand organized on PIC microcontrollers, config phrases are numbered with every quantity having a “excessive” and “low” byte.

CONFIG1L does not exist on the 18F2620. If it did, it could dwell at 0x300000 location.

CONFIG1H accommodates bits that specify oscillator choices. Typically, in Embedded Adventures initiatives we attempt to use exterior crystals which give extra correct and dependable outcomes. This does expend two pins nonetheless, and generally these may be extra necessary than the velocity at which the chip is working. The exterior crystal oscillator is known as the HS oscillator (should you’re doing the everyday factor of attempting to run the micro as quick as it should go). Within the 18F2620, you can even allow the PLL module which offers you a four-times velocity increase. From CONFIG1H are additionally choices to allow the fail-safe clock monitor (that switches to the interior oscillator ought to the exterior one fail, and an choice to allow the switching between totally different oscillator sources.

We typically advocate that you just use an exterior crystal, and switch off any oscillator switching / failover modes. In prototyping, it is very important have dependable, repeatable outcomes (largely so you’ll be able to scale back the axes of error). It’s doable to configure the 18F2620 to start out up utilizing the interior oscillator (which runs at 8Mhz, or 32Mhz with the PLL enabled) after which change to the exterior one (we use a 10Mhz crystal with PLL enabled providing you with 40Mhz). That provides you quicker startup from interrupts in sleep mode or on bootup, if that is necessary, nonetheless for prototyping, that is typically not needed.

Our beneficial config string for CONFIG1H:


CONFIG2L handles brown-out reset and the ability up timer. Brown Out Reset (or BOR) is the flexibility for the microcontroller to reset itself if the provision voltage falls under a specified threshold. It’ll stay in reset state till the provision goes again over the edge. This could imply some battery saving (at the very least, as soon as the battery is flat past a sure degree, it’s going to cease draining energy as rapidly) – however at that time there is not any performance accessible anyway. The ability on timer waits on preliminary power-on for the provision to go above the BOR voltage threshold, then hangs round for one more 65ms earlier than kicking issues off for actual. This can assist make sure that your energy provide is regular earlier than attempting to execute any code.

Now we have seen some fairly unpredictable outcomes from the 18F2620 when testing some LED show panels. We thought the software program was crashing or resetting the micro when the truth is, what was occurring was the BOR was being tripped as extra LEDs have been lit and the ability provide dropped. As such, until you’ve got obtained an actual want for it, we advocate switching it off. Leaving the ability provide to settle earlier than beginning the firmware, is mostly a good suggestion (you are in all probability not going to note the 65ms it takes).

#pragma DATA _CONFIG2L, _BOREN_OFF_2L & _BORV_2_2L & _PWRT_ON_2L

CONFIG2H provides you settings for the watchdog timer. This can be a timer that resets the PIC in a sure period of time (you’ll be able to broadly specify how lengthy that is likely to be). It is likely to be good plan to reset your PIC should you’re doing one thing from which you may by no means get better – though hopefully you are written adequate code that this can not occur! Alternatively, the watchdog timer may pull the PIC out of sleep mode. This implies you’ll be able to organize for the PIC to fall asleep for a sure period of time (if nothing else occurs). For prototyping, we advocate switching the watchdog timer off. For those who do allow it, be sure to reset the watchdog timer commonly or – nicely, you’ll be able to guess what’s going to occur.

#pragma DATA _CONFIG2H, _WDT_OFF_2H & _WDTPS_128_2H

CONFIG3H has a set of configuration objects. You possibly can select for the RE3 pin to be accessible (as enter solely) or for that pin to be MCLR (which resets the micro when pulled low). For prototyping, with the ability to reset the micro by urgent a button is less complicated than pulling the ability out.

The LPT1OSC bit provides the choice of a “excessive energy” mode or a “low energy mode”. The low energy mode is extra delicate in excessive noise environments. So if battery utilization is essential, you could design your circuit fastidiously. For prototyping, in fact, we’d advocate leaving this in excessive energy mode.

PBABEN, permits you to specify if PORTB pins 0 – 4 ought to get up as analog enter pins or digital pins upon reset. After all you’ll be able to change this in software program within the ADCON1 register at any time.

The CCP module is a Seize / Evaluate / PWM module and a ultimate CONFIG3H bit permits the “steering” of this output to be on both RC1 or RB3. That is helpful to have the ability to change should you require the opposite performance accessible on one among these pins (RB3 can also be analogue enter 9 and RC1 may be Timer 1 oscillator enter). Alternatively, your PCB structure is likely to be simpler should you may transfer this output to 1 or the opposite. For prototyping, typically this does not matter both means.


CONFIG4L accommodates some fascinating choices and reveals very clearly how necessary it’s to have the ability to configure the PIC earlier than startup.

The DEBUG bit permits {hardware} debugging. You possibly can step via code, set breakpoints and so forth – you probably have your software program set accurately and this bit cleared. {Hardware} debugging additionally requires the unique use of RB6 and RB7 (additionally used for ICSP programming). In all honesty, we discover that serial output is mostly adequate to search out bugs, though you’ll be able to debug loads of PICs with merely a PicKit2.

XINST permits for the 18F prolonged instruction set mode. This is not utilized by Supply increase and should you change it on by mistake (whatever the compiler you are utilizing) there shall be some fairly unpredictable outcomes. One in all our readers spent days looking for what was flawed together with his code solely to understand the XINST bit was set.

LVP provides you the flexibility to program the PIC utilizing ICSP with out requiring the “greater” Vpp voltage. There’s a catch nonetheless – you could dedicate the RB5 pin (PGM) to being an indicator in case you are in programming mode or not. Given the PicKit2 generates the proper voltage anyway, there does not appear to be loads of worth in switching this on for prototyping functions.

Lastly, the STVREN provides the choice of getting the PIC reset if the stack overflowed or underflowed. If, for instance, you name too many nested features, it could enable the PIC to reset. As soon as reset, you’ll be able to look at why the PIC reset and report a fault. We advocate leaving this on – in case your PIC boots in the midst of doing one thing, you’ll be able to moderately assume that you’ve got used up the stack area (doable while you’re deep in a nested scenario after which an interrupt happens).


CONFIG5L provides the choice of specifying if any one among 4 contiguous blocks of reminiscence are protected against any makes an attempt by the PIC to alter (ie write) the flash reminiscence values.

#pragma DATA _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L

CONFIG5H givest the choice of defending the block of reminiscence beginning at 0x0000 to 0x7fff) – utilized by some bootloaders, in addition to a bit to permit the safety of EEPROM reminiscence. The PicPack bootloader, “Boostbloader”, resides in higher reminiscence, that means that whereas it’s barely much less sturdy than a locked-down, bodily protected bootloader, it does not require the “double soar” for interrupts that bootloaders positioned in decrease reminiscence do. We might present this as an possibility sooner or later.


CONFIG6L permits the safety of reminiscence blocks being write protected.

#pragma DATA _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L

CONFIG6H, CONFIG7L and CONFIG7H give extra granular safety, permitting blocks of reminiscence to be protected against when the code being executed will not be in the identical block. We may, for instance, make a bootloader that was “secret” and unable to be prepared by some other code.




Whereas strictly talking they are not config phrases, the DEVID phrases enable code to find out which mannequin of the chip is working and which {hardware} revision. That is necessary when there are bugs within the {hardware} which might be fastened over totally different revision ranges and code must execute in a different way based mostly on the consequence.

Making configs

You’ve got in all probability labored out by now that the Supply increase compiler’s config defines are cleverly conceived so to merely & collectively the suitable choices; this implies you need not work out precisely which bit positions do what. The #pragma DATA provides you the flexibility to shove a byte in a specific location (in our case, right into a config phrase slot).

Different PICs

This is the config we use with the PIC16F88:


Yep, that is proper, it is only one phrase lengthy. A few of these choices will in all probability be acquainted to you already.


In case your chip will not be behaving and also you wish to work out should you’ve made a mistake in your config bits, and helpful trace is to make use of Microsoft’s MPLAB IDE to import the. Hex file you’ve got created by clicking on Menu | Import… then Configure | Configuration Bits… This is the display screen you may discover when importing the PIC18F2620 Bootloader.

You possibly can even change the config bits if you need, then export once more as a. Hex file, though take into account this does not change your supply code itself.

What you’ve got in all probability realized right here is that the bootloader positively has config bits embedded within the. Hex file. Our ordinary PicPack applications do not – since we assume you are working with a bootloader already put in. For those who do wish to program instantly with ICSP, in fact you may want to make use of config bits to make your PIC run the way in which you plan. Try the configbits.h file utilized by Bootloader – we’re beginning to construct up a library of helpful config bits together with some configuration choices to show frequent issues on or off (these you naturally place within the config.h file in your venture).

Leave a Reply

Your email address will not be published. Required fields are marked *