Using Config Bits in Microcontroller Based Projects

Microcontrollers tend to be dedicated to a particularmodule and a final CONFIG3H bit allows the "steering"
purpose, so while they're very flexible devices theseof this output to be on either RC1 or RB3. This is
days, there are a number of different (often conflicting)handy to be able to change if you require the other
options for how you might want the hardware tofunctionality available on one of these pins (RB3 is also
work. You might think that this configuration is done inanalogue input 9 and RC1 can be Timer 1 oscillator
software, but what if you want the hardware to act ainput). Alternatively, your PCB layout might be easier if
certain way from the very start? Keeping in mind thatyou could move this output to one or the other. For
Microchip PIC devices can "boot" in a matter ofprototyping, generally this doesn't matter either way.
milliseconds, there needs to be a way you telling the#pragma DATA _CONFIG3H,
hardware how to behave in advance of it starting up._CCP2MX_PORTC_3H & _PBADEN_OFF_3H &
This article covers how to get your microcontroller_MCLRE_ON_3H
based project working the way you expect when itCONFIG4L contains some interesting options and
starts up.shows very clearly how important it is to be able to
Enter config bits, or, as they used to be called, configconfigure the PIC before startup.
fuses. Back in the days when microcontrollers wereThe DEBUG bit allows hardware debugging. You can
program-once devices, you really did blow a fuse instep through code, set breakpoints and so on - if you
order to program them. Today, most micros havehave your software set correctly and this bit cleared.
flash memory that can be programmed tens ofHardware debugging also requires the exclusive use
thousands of times, but there are stillof RB6 and RB7 (also used for ICSP programming). In
one-time-programmable (OTP) devices about.all honesty, we find that serial output is generally
In any case, there's a bunch of "config words" thatsufficient to find bugs, although you can debug a lot of
define how the micro is going to behave from thePICs with merely a PicKit2.
get-go. Let's look at some of these options (we'llXINST allows for the 18F extended instruction set
choose the PIC18F2620 as an example) and then seemode. This isn't used by Sourceboost and if you
how to program them. Sourceboost gives you all theswitch it on by mistake (regardless of the compiler
config strings in a ready-to-use format for including inyou're using) there will be some pretty unpredictable
your project. The format has the config bits suffixedresults. One of our readers spent days trying to find
with the same name as the config word so you canwhat was wrong with his code only to realise the
ensure that the right bits are going in the right word. OfXINST bit was set.
course, your config words will be different if you'reLVP gives you the ability to program the PIC using
using a different microcontroller, but you should learnICSP without requiring the "higher" Vpp voltage. There
enough here to point you in the right direction.is a catch however - you need to dedicate the RB5
In the 18F2620, the config bits are stored in flash, but atpin (PGM) to being an indicator if you are in
a high memory location. They're stored at 0x300001programming mode or not. Given the PicKit2
and upwards. For historical reasons due to the waygenerates the right voltage anyway, there doesn't
memory was previously arranged on PICseem to be a lot of value in switching this on for
microcontrollers, config words are numbered with eachprototyping purposes.
number having a "high" and "low" byte.Finally, the STVREN gives the option of having the PIC
CONFIG1L doesn't exist on the 18F2620. If it did, itreset if the stack overflowed or underflowed. If, for
would live at 0x300000 location.example, you call too many nested functions, it would
CONFIG1H contains bits that specify oscillator options.allow the PIC to reset. Once reset, you can examine
Generally, in Embedded Adventures projects we trywhy the PIC reset and report a fault. We recommend
and use external crystals which give more accurateleaving this on - if your PIC boots in the middle of doing
and reliable results. This does use up two pinssomething, you can reasonably assume that you've
however, and sometimes these can be moreused up the stack space (possible when you're deep
important than the speed at which the chip is running.in a nested situation and then an interrupt occurs).
The external crystal oscillator is referred to as the HS#pragma DATA _CONFIG4L, _STVREN_ON_4L &
oscillator (if you're doing the typical thing of trying to run_LVP_OFF_4L & _DEBUG_OFF_4L &
the micro as fast as it will go). In the 18F2620, you can_XINST_OFF_4L
also enable the PLL module which will give you aCONFIG5L gives the option of specifying if any one of
four-times speed boost. From CONFIG1H are alsofour contiguous blocks of memory are protected from
options to enable the fail-safe clock monitor (thatany attempts by the PIC to change (ie write) the flash
switches to the internal oscillator should the externalmemory values.
one fail, and an option to enable the switching between#pragma DATA _CONFIG5L, _CP0_OFF_5L &
different oscillator sources._CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
We generally recommend that you use an externalCONFIG5H givest the option of protecting the block of
crystal, and turn off any oscillator switching / failovermemory starting at 0x0000 to 0x7fff) - used by some
modes. In prototyping, it is important to have reliable,bootloaders, as well as a bit to allow the protection of
repeatable results (mostly so you can reduce theEEPROM memory. The PicPack bootloader,
axes of error). It is possible to configure the 18F2620"Boostbloader", resides in upper memory, meaning that
to start up using the internal oscillator (which runs atwhile it is slightly less robust than a locked-down,
8Mhz, or 32Mhz with the PLL enabled) and then switchphysically protected bootloader, it doesn't require the
to the external one (we use a 10Mhz crystal with PLL"double jump" for interrupts that bootloaders located in
enabled giving you 40Mhz). That gives you fasterlower memory do. We may provide this as an option
startup from interrupts in sleep mode or on bootup, ifin the future.
that's important, however for prototyping, this is#pragma DATA _CONFIG5H, _CPB_OFF_5H &
generally not necessary._CPD_OFF_5H
Our recommended config string for CONFIG1H:CONFIG6L allows the protection of memory blocks
#pragma DATA _CONFIG1H, _OSC_HSPLL_1H &being write protected.
_IESO_OFF_1H & _FCMEN_OFF_1H#pragma DATA _CONFIG6L, _WRT0_OFF_6L &
CONFIG2L handles brown-out reset and the power up_WRT1_OFF_6L & _WRT2_OFF_6L &
timer. Brown Out Reset (or BOR) is the ability for the_WRT3_OFF_6L
microcontroller to reset itself if the supply voltage fallsCONFIG6H, CONFIG7L and CONFIG7H give more
below a specified threshold. It will remain in reset stategranular protection, allowing blocks of memory to be
until the supply goes back over the threshold. This canprotected from when the code being executed is not
mean some battery saving (at least, once the batteryin the same block. We could, for example, make a
is flat beyond a certain level, it'll stop draining power asbootloader that was "secret" and unable to be ready
quickly) - but at that point there's no functionalityby any other code.
available anyway. The power on timer waits on initial#pragma DATA _CONFIG6H, _WRTC_OFF_6H &
power-on for the supply to go above the BOR voltage_WRTB_OFF_6H & _WRTD_OFF_6H
threshold, then hangs around for another 65ms before#pragma DATA _CONFIG7L, _EBTR0_OFF_7L &
kicking things off for real. This can help ensure that_EBTR1_OFF_7L & _EBTR2_OFF_7L &
your power supply is steady before trying to execute_EBTR3_OFF_7L
any code.#pragma DATA _CONFIG7H, _EBTRB_OFF_7H
We have seen some pretty unpredictable results fromWhile strictly speaking they're not config words, the
the 18F2620 when testing some LED display panels.DEVID words allow code to determine which model of
We thought the software was crashing or resettingthe chip is running and which hardware revision. This is
the micro when in fact, what was happening was theimportant when there are bugs in the hardware that
BOR was being tripped as more LEDs were lit andare fixed over different revision levels and code needs
the power supply dropped. As such, unless you've gotto execute differently based on the result.
a real need for it, we recommend switching it off.Making configs
Leaving the power supply to settle before starting theYou've probably worked out by now that the
firmware, is generally a good idea (you're probably notSourceboost compiler's config defines are cleverly
going to notice the 65ms it takes).conceived so that you can simply & together the
#pragma DATA _CONFIG2L, _BOREN_OFF_2L &appropriate options; this means you don't need to work
_BORV_2_2L & _PWRT_ON_2Lout exactly which bit positions do what. The #pragma
CONFIG2H gives you settings for the watchdog timer.DATA gives you the ability to shove a byte in a
This is a timer that resets the PIC in a certain amountparticular location (in our case, into a config word slot).
of time (you can broadly specify how long that mightOther PICs
be). It might be good plan to reset your PIC if you'reHere's the config we use with the PIC16F88:
doing something from which you might never recover -#pragma DATA _CONFIG, _CP_OFF &
although hopefully you're written good enough code_CCP1_RB0 & _DEBUG_OFF &
that this cannot happen! Alternatively, the watchdog_WRT_PROTECT_OFF & _CPD_OFF &
timer can also pull the PIC out of sleep mode. This_LVP_OFF & _PWRTE_OFF & _WDT_OFF &
means you can arrange for the PIC to go to sleep for_PWRTE_ON & _BODEN_OFF &_MCLR_ON &
a certain amount of time (if nothing else happens). For_INTRC_IO
prototyping, we recommend switching the watchdogYep, that's right, it's just one word long. Some of these
timer off. If you do enable it, make sure you reset theoptions will probably be familiar to you already.
watchdog timer regularly or - well, you can guess whatTroubleshooting
will happen.If your chip is not behaving and you want to work out
#pragma DATA _CONFIG2H, _WDT_OFF_2H &if you've made a mistake in your config bits, and handy
_WDTPS_128_2Hhint is to use Microsoft's MPLAB IDE to import the.hex
CONFIG3H has a collection of configuration items. Youfile you've created by clicking on Menu | Import... then
can choose for the RE3 pin to be available (as inputConfigure | Configuration Bits... Here's the screen you'll
only) or for that pin to be MCLR (which resets thefind when importing the PIC18F2620 BoostBloader.
micro when pulled low). For prototyping, being able toYou can even change the config bits if you want, then
reset the micro by pressing a button is easier thanexport again as a.hex file, although keep in mind this
pulling the power out.doesn't change your source code itself.
The LPT1OSC bit gives the option of a "high power"What you've probably realised here is that the
mode or a "low power mode". The low power modebootloader definitely has config bits embedded in
is more sensitive in high noise environments. So ifthe.hex file. Our usual PicPack programs don't - since
battery usage is very important, you need to designwe assume you're running with a bootloader already
your circuit carefully. For prototyping, of course, weinstalled. If you do want to program directly with ICSP,
would recommend leaving this in high power mode.of course you'll need to use config bits to make your
PBABEN, allows you to specify if PORTB pins 0 - 4PIC run the way you intend. Check out the configbits.h
should wake up as analog input pins or digital pins uponfile used by Boostbloader - we're starting to build up a
reset. Of course you can change this in software inlibrary of handy config bits including some configuration
the ADCON1 register at any time.options to turn common things on or off (these you
The CCP module is a Capture / Compare / PWMnaturally place in the config.h file in your project).