factory default PIC configuration bits

Discussion in 'Embedded Systems and Microcontrollers' started by tracecom, Jul 28, 2013.

  1. tracecom

    Thread Starter AAC Fanatic!

    Apr 16, 2010
    I read on the web that on a factory fresh PIC the configuration bits are all set to "1". True or false? And if false, how are they set from the factory?
  2. JohnInTX


    Jun 26, 2012
    That's mostly true but I wouldn't assume its always the case for every part. Many early PICs don't have the default specified at all, expecting that to be done at blast time. For a long and content life, I would recommend that your code always specify a full configuration that is qualified with the PIC you are using in case you ever change it e.g.
    Code ( (Unknown Language)):
    1.   ifdef __P184520
    2.    CONFIG....
    3.   else
    4.    ERROR "Check config bits."
    5.   endif
    On a related note, early versions of MPASM defaulted unused config bits to various values. Later versions explicitly set unused bits to 1. Its been an issue when you try to validate a newly built .HEX after upgrading MPLAB. I've often found that a new .HEX is not the same as the old .HEX and the difference is in the setting of unused bits in the config reg.

    Have fun.
  3. tracecom

    Thread Starter AAC Fanatic!

    Apr 16, 2010

    Thanks for the reply. Since I originally posted my question, I read that PIC programmers erase the devices before every programming, including the configuration bits. Thus, the configuration bits are all set to "1" before the PIC is programmed. Based on your comments above, I surmise that this is not always true, i.e., it's still better to set them all in every program rather than depend on the bits not explicitly programmed being set to "1" by default. Correct?

    What piqued my curiousity was that in PBP3, the .info files for each PIC describe the defaults as prescribed by PBP3 during the programming process, and also describe all the configuration bit choices for that particular PIC. When I compared the two, I saw that PBP3 leaves some of the configuration bits out in its default programming.

    I know that's a convoluted explanation, but it's the best I can do without writing a lot more. I have attached a .info file for the 12F683 as an example of what I mean. I changed the name of the file to a .txt so it will open with Notepad.

  4. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    This doesn't answer your question, but what I do is always include a config.inc file in my .asm code that defines the processor, loads the .inc file for that processor, and sets the bits for the application. Changing config bits in the future is as simple as adding or removing comment marks ";" at the beginning of each line. Here's an example

    Code ( (Unknown Language)):
    1. ;********************************
    2. ;** config.inc -- 18F84J90     **
    3. ;** Author: joeyd999           **
    4. ;** Derived from p18f84j90.inc **
    5. ;********************************
    7. ;define the processor and include the definitions
    9.     processor   18f84j90
    10.     include     "p18f84j90.inc"
    12. ;configuration bit settings:
    14. ;   Background Debugger Enable bit (define ICD to enable):
    16. #ifdef ICD
    17.     config  DEBUG = ON           ;Background debugger enabled; RB6 and RB7 are dedicated to In-Circuit Debug
    18. #else
    19.     config  DEBUG = OFF          ;Background debugger disabled; RB6 and RB7 configured as general purpose I/O pins
    20. #endif
    22. ;   Extended Instruction Set Enable bit:
    23.     config  XINST = OFF          ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    24. ;   config  XINST = ON           ;Instruction set extension and Indexed Addressing mode enabled
    25. ;
    26. ;   Stack Overflow/Underflow Reset Enable bit:
    27.     config  STVREN = OFF         ;Reset on stack overflow/underflow disabled
    28. ;   config  STVREN = ON          ;Reset on stack overflow/underflow enabled
    29. ;
    30. ;   Watchdog Timer Enable bit:
    31.     config  WDTEN = OFF          ;WDT disabled (control is placed on SWDTEN bit)
    32. ;   config  WDTEN = ON           ;WDT enabled
    34. ;   Code Protection bit (define ICD or NOCODEPROTECT to disable):
    36. #ifdef ICD
    37.     config  CP0 = OFF            ;Program memory is not code-protected
    38. #else
    40. #ifdef NOCODEPROTECT
    41.     config  CP0 = OFF             ;Program memory is code-protected
    42. else
    43.     config  CP0 = ON            ;Program memory is not code-protected
    44. #endif
    46. #endif
    48. ;   Two-Speed Start-up (Internal/External Oscillator Switchover) Control bit:
    49.     config  IESO = OFF           ;Two-Speed Start-up disabled
    50. ;   config  IESO = ON            ;Two-Speed Start-up enabled
    52. ;   Fail-Safe Clock Monitor Enable bit:
    53.     config  FCMEN = OFF          ;Fail-Safe Clock Monitor disabled
    54. ;   config  FCMEN = ON           ;Fail-Safe Clock Monitor enabled
    56. ;   Default/Reset System Clock Select bit:
    57.     config  FOSC2 = OFF          ;INTRC enabled as system clock when OSCCON<1:0> = 00
    58. ;   config  FOSC2 = ON           ;Clock selected by FOSC1:FOSC0 as system clock is enabled when OSCCON<1:0> = 00
    60. ;   Oscillator Selection bits:
    61.     config  FOSC = HS            ;HS oscillator
    62. ;   config  FOSC = HSPLL         ;HS oscillator, PLL enabled and under software control
    63. ;   config  FOSC = EC            ;EC oscillator, CLKO function on OSC2
    64. ;   config  FOSC = ECPLL         ;EC oscillator, PLL enabled and under software control, CLK function on OSC2
    66. ;   Watchdog Timer Postscaler Select bits:
    67.     config  WDTPS = 1            ;1:1
    68. ;   config  WDTPS = 2            ;1:2
    69. ;   config  WDTPS = 4            ;1:4
    70. ;   config  WDTPS = 8            ;1:8
    71. ;   config  WDTPS = 16           ;1:16
    72. ;   config  WDTPS = 32           ;1:32
    73. ;   config  WDTPS = 64           ;1:64
    74. ;   config  WDTPS = 128          ;1:128
    75. ;   config  WDTPS = 256          ;1:256
    76. ;   config  WDTPS = 512          ;1:512
    77. ;   config  WDTPS = 1024         ;1:1024
    78. ;   config  WDTPS = 2048         ;1:2048
    79. ;   config  WDTPS = 4096         ;1:4096
    80. ;   config  WDTPS = 8192         ;1:8192
    81. ;   config  WDTPS = 16384        ;1:16384
    82. ;   config  WDTPS = 32768        ;1:32768
    83. ;
    84. ;   ECCP2 MUX bit:
    85. ;   config  CCP2MX = ALTERNATE   ;ECCP2/P2A is multiplexed with RE7 in Microcontroller mode or with RB3 in Extended Microcontroller mode
    86.     config  CCP2MX = DEFAULT     ;ECCP2/P2A is multiplexed with RC1
    tracecom likes this.
  5. JohnInTX


    Jun 26, 2012
    I didn't explain that as well as I should have..
    When the device is erased prior to programming all bits in ROM, including the configuration word, are '1'. The config word will thus set the PIC to whatever '1' means in for the particular thing it controls. For the 12F683 FOSC bits in the CONFIG register
    Code ( (Unknown Language)):
    1. 111 = RC oscillator: CLKOUT function on GP4/OSC2/CLKOUT pin, RC on GP5/OSC1/CLKIN
    means that if you don't specify a CONFIG word in some way, the PIC will use the RC oscillator etc. But you knew that..

    There have been several ways to specify the CONFIG over the years. The original way was to use the _CONFIG directive which ANDed several values (declared in p12F683.h) with '0' in strategic points. In FOSC above, the value to AND for the XT oscillator (FOSC=001) would be B'11111111 11111001'. ANDing the values for the various fields yields the net value to blast into the CONFIG register and as you correctly surmised, the other bits are expected to be '1' from erasing.

    Setting CONFIG this way is supported for legacy reasons but has been superseded by CONFIG field=value as shown in joeyd999's example. The reason is that as PICs get more complex, they will have many CONFIG registers which can be a pain to manage. The new CONFIG directive is more portable when moving to a similar PIC or sometimes even going to a later version of the same P/N which may have minor changes to what's in the CONFIG register to support enhancements to the part. Rather than generate a literal value with _CONFIG, use CONFIG field=value and let the assembler sort it out. Your code will also be easier to read.

    When its all said and done, the CONFIG settings will be in your .HEX file to be blasted along with the code. Here is where our confusion was. The .HEX generated by MPASM has used different values for the UNUSED bits in the CONFIG records in the .HEX. They used to be 0 but now are 1. 1 is better. Its not unheard of that a user will take a .HEX for one PIC and blast another P/N (i.e. moving from a CMOS part to its direct flash replacement). Sometimes while the basic architecture is the same, the later PIC may have different features that are controlled by the CONFIG reg e.g. code protection capabilities etc. Setting unused CONFIG bits to 0 would cause any new features to be enabled by 'default', not always a good thing. So you were correct in your assumption that unspecified CONFIG bits should not cause a problem if you can live with the defaults. My problem with that is you are leaving yourself open to missing something important and causing yourself grief.

    So, my personal rules for CONFIG are:
    Examine every field and explicitly set it in code, even if setting it to the default condition.
    Use the later CONFIG field=value construct to free your code from the particulars of the CONFIG register(s).
    Qualify the config settings with the processor type as I showed above to force an assembly time error to remind you to recheck the CONFIG settings if you decide to change PICs.
    Use assemble-time switches as required to automate any config changes necessitated by a debugger. For example, RealICE requires that the watchdog and code protection be disabled for debugging. My production code always uses both so I have a switch (if DEBUGGING ; or something like that) that disables them for debug then switches in a completely different CONFIG block to enable them when building for release.

    Take a look at the Programming Specifications for your PIC. Written for those designing programming hardware, its also a great insight into the CONFIG hardware.

    Finally, when moving from one version of MPASM to another I compare new and old .HEX files to be sure the two versions generate the same code. Back in the day, MPASM was rather arbitrary about how it handled things such as case sensitivity in macros, the order of include files etc. sometimes generating a faulty programming image with no warnings from the assembler. Its much better now (default CONFIG values notwithstanding) but old habits die hard, especially when the scars are still visible. That was how I found the different (and yes, mostly benign) differences in the unused CONFIG settings.

    Have fun!
    Last edited: Jul 29, 2013
    tracecom likes this.
  6. atferrari

    AAC Fanatic!

    Jan 6, 2004
    I always set explicitly the whole thing. No doubts aftwerwards

    Code ( (Unknown Language)):
    1. TITLE     "PWM EXPLORER 01"
    3.   SUBTITLE  "Started    15.06.09"
    4.   SUBTITLE  "Completed  18.06.09"
    5.   SUBTITLE  "Author Agustiin Tomaas Ferrari Nicolay - Buenos Aires"
    6. ;------------------------------------------------------------------------------
    8. ;Default radix is DECimal!
    9. ;Power control PWM testing, oriented to obtain a DC voltage range.
    11. ;XTAL = 4 MHz - HSPLL enabled.
    12. ;On line delay routines updated to 16 MHz (4 * 4 MHZ)
    13. ;------------------------------------------------------------------------------
    15.   Processor 18F4431
    16.   LIST  B=4, C=80, MM=ON, R=DEC, ST=ON, T=OFF
    18.   INCLUDE "P18F4431.INC"
    19. ;------------------------------------------------------------------------------
    22.   CONFIG  BORV =27    ,WDTEN =OFF   ,WINEN =OFF   ,WDPS =1        ,T1OSCMX =OFF
    24.   CONFIG  PWM4MX =RB5 ,SSPMX =RC7   ,FLTAMX =RC1  ,STVREN =ON     ,LVP =OFF            
    25.   CONFIG  DEBUG =OFF  ,CP0 =OFF     ,CP1 =OFF     ,CP2 =OFF       ,CP3 =OFF            
    26.   CONFIG  CPB =OFF    ,CPD =OFF     ,WRT0 =OFF    ,WRT1 =OFF      ,WRT2 =OFF            
    27.   CONFIG  WRT3 =OFF   ,WRTB =OFF    ,WRTC =ON     ,WRTD =OFF      ,EBTR0 =OFF          
    tracecom likes this.