• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

16F628A - The Configuration Word

    Blog entry posted in 'Uncategorised', August 25, 2011.

    The config word is one of the things that scares most PIC newbies. Not to worry though, it's not as tough to understand as most think. Here I will explain the config word on the 16F628A and what it does.

    The config word on all 16F PICs is 14 bits long and each bit in the word is like a switch that switches a function on the PIC on or off. These functions include but are not limited to -

    Program Code Protection (prevents readback of program code via programming hardware)
    Data Code Protection (prevents readback of data EEPROM via programming hardware)
    Low Voltage Programming
    Brown Out Reset
    Sets MCLR as either External Master Clear or digital input
    Power Up Timer
    Watchdog Timer
    Sets the oscillator type (be it internal, external RC or external crystal)

    A full description of config word controlled features as well as the config word itself can be found under the "Special Features of the CPU" section of your PIC's datasheet.

    The features that are controlled by the config word are processor dependent. Some PICs have more features than others, which means they will have more config bits implemented than others and some will even have 2 config words if 1 config word doesn't have enough bits for the amount of config word controlled features on the PIC.

    The config word on all PICs is only writable during programming time (only the programming hardware can access the config word address). This means that you cannot change the config word bits (also referred to as "fuses" by some) in software during program execution. Once they're written, the only way to change them is to reprogram the PIC with different config bit settings.

    On most 16F PICs, the config word resides at ROM address 0x2007. Config words on larger PICs that have two config words (like the 16F887) will reside at addresses 0x2007 and 0x2008.

    On the 16F628A, the config word looks like this -

    Code:

    ; __ ___ _____
    ;| CP | - | - | - | - | CPD | LVP |BOREN|MCLRE|FOSC2|PWRTE|WDTE |FOSC1|FOSC0|
    ;| 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |


    By default, all config bits are initialized to 1's. Any bits whose names have a line above them are "active low". This means that setting their bits to a 0 will enable that feature while setting them to 1 will disable that feature. All other bits are "active high" and work in reverse of active low bits.

    On the 16F628A, bits 9-12 are unimplemented and are read as 0's. However, on the 16F887, all unimplemented config bits are read as 1's.

    Since all of the config bits default to 1's, we only need to program bits that need to be set to 0. For Low Voltage Programming (LVP), Brown Out Reset (BOREN), External Master Clear (MCLRE) and the Watchdog Timer (WDTE), setting these bits to 0 will disable these features and set the MCLR pin as digital input. For Program Code Protection (CP), Data Code Protection (CPD) and Power Up Timer (PWRTE), setting these bits to 0 will enable these features since they are "active low" bits.

    The oscillator type on the 16F628A is specified by bits 1, 2 and 5 (FOSC2, FOSC1 and FOSC0). There are a total of 8 possible oscillator configurations. Writing the following bit patterns to these 3 bits on the 16F628A will set the oscillator type as follows -

    Code:

    ;|FOSC2|FOSC1|FOSC0|
    ;| 1 | 1 | 1 | - External RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
    ;| 1 | 1 | 0 | - External RC oscillator: I/O function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
    ;| 1 | 0 | 1 | - INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
    ;| 1 | 0 | 0 | - INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
    ;| 0 | 1 | 1 | - EC (External Clock): I/O function on RA6/OSC2/CLKOUT pin, CLKIN on RA7/OSC2/CLKIN
    ;| 0 | 1 | 0 | - HS Oscillator: High-speed crystal/resonator (8MHz - 20MHz) on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
    ;| 0 | 0 | 1 | - XT Oscillator: Crystal/resonator (100kHz - 4MHz) on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
    ;| 0 | 0 | 0 | - LP Oscillator: Low-power crystal/resonator (32kHz - 200kHz) on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN


    To write to the config word, we have 2 choices. Some programming software will allow you to configure the config fuse settings in the program by checking the box next to each feature you want to enable and unchecking the box next to each feature you want to disable. The other way (and the most preferred) is to use the "__config" directive at the top of your code file (the underscore next to the word "config" is actually a double underscore).

    To do it in code we can do it 1 of 2 ways. Method 1 is to write either a binary or a hex value that corresponds to the bits you would want to set or clear. As an example, I will illustrate config words for both the 16F887 and the 16F628A in order to illustrate how you would do both single and double config word PICs. On single config word PICs you do not have to specify the config word address since there's only one and the assembler knows which PIC you're programming (when you tell it with the "list" directive as well as when you select the PIC type in MPASM). However, on dual config word PICs, the addresses for both need to be specified.

    Here's how you would do it in code with the following features enabled/disabled -

    16F628A -

    Code Protection Off
    Data Code Protection Off
    Low Voltage Programming Off
    Brown Out Reset Off
    RA5 is External Master Clear
    Power Up Timer On
    Watchdog Timer OFf
    High Speed Oscillator

    16F887 - Same as above but with the following added features -

    Debug Mode Off (for In Circuit Debugger)
    Fail Safe Clock Monitor Bit Off
    Internal/External Switchover Off
    Lower Half of Code Memory Write Protected
    Brown Out Reset Voltage 2.1V

    Code:

    ;Example 1 - Single Config Word 16F628A

    __config b'10000100100010'

    ;Example 2 - Single Config Word 16F628A

    __config 0x2122

    ;| __ 2 | 1 ___ | 2 |_____ 2 |
    ;| CP | - | - | - | - | CPD | LVP |BOREN|MCLRE|FOSC2|PWRTE|WDTE |FOSC1|FOSC0|
    ;| 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |


    ;Example 3 - Dual Config Word 16F887

    __config _CONFIG1,b'10000011100010'
    __config _CONFIG2,b'11100011111111'

    ;Example 4 - Dual Config Word 16F887

    __config _CONFIG1,0x20E2
    __config _CONFIG2,0x38FF

    ;config word 1 (address 0x2007, which equates to the label _CONFIG1)
    ;|_____2 | 0 | ___ __ E _____| 2 |
    ;|DEBUG| LVP |FCMEN|IESO |BOREN1|BOREN0| CPD | CP |MCLRE|PWRTE|WDTE |FOSC2|FOSC1|FOSC0|
    ;| 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 |

    ;config word 2 (address 0x2008, which equates to the label _CONFIG2)
    ;| 3 | 8 | F | F |
    ;| - | - | - |WRT1 | WRT0 |BOR4V | - | - | - | - | - | - | - | - |
    ;| 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |

    ;Example 5 - Dual Config Word 16F887

    __config 0x2007,b'10000011100010'
    __config 0x2008,b'11100011111111'

    ;Example 6 - Dual Config Word 16F887

    __config 0x2007,0x20E2
    __config 0x2008,0x38FF





    Notice that Code Protection and Data Code Protection are set to 1, yet they are disabled. Also, that the Power Up Timer is enabled yet it is set to 0. This is due to them being "active low" bits so they work backwards from the rest.

    Method 2 is to use labels to set the config bits. MPASM comes with include files for each PIC, and in those include files they have labels equated to each bit in the config word. Since the config fuses are initialized to all 1's by default, we only need to use the labels for the fuses that we need to set to 0. The label method is the most preferred because all PICs use pretty much the same labels for their config bits (with some added in as some PICs have more features than others) so this improves code portability between PICs -

    Code:

    ;Example 7 - Single Config Word 16F628A

    include <P16F628A.INC>
    __config _LVP_OFF & _BOREN_OFF & _PWRTE_ON and _WDT_OFF & _HS_OSC

    ;Example 8 - Dual Config Word 16F887

    include <P16F887.INC>
    __config _CONFIG1,_LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC
    __config _CONFIG2,_WRT_HALF & _BOR21V

    ;Example 9 - Dual Config Word 16F887

    include <P16F887.INC>
    __config 0x2007,_LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC
    __config 0x2008,_WRT_HALF & _BOR21V




    Notice that I used the "include" directive to tell MPASM where to find the 16F628A include file to reference for the config fuse labels. It looks those labels up, then sets/clears the fuses accordingly to the bit patterns that each label equates to.

    Also note that I only used the labels needed for bits that need to be set to 0. The "oscillator type label" (_HS_OSC) sets/clears FOSC2-FOSC0 for the respective oscillator type. This label is always needed to tell the PIC which oscillator type you will be using.

    And that's pretty much all there is to the config word on PICs. Each PIC has a different config word with different config bits and bit locations so you MUST consult the data sheet for the PIC you're using in order to know which features are controlled by the config bits (the 16F628A config word as well as the config word bit descriptions can be found on page 96 of the PIC16F627A/628A/648A datasheet). If you plan to use the "config bit label" method to program the config word, you must also consult the includes file for the PIC you're using in order to know what labels to use for each bit.

    Comments
    aswin p ajayan, February 05, 2014
    can you please post on the configuration bits of 18f4550
 

EE World Online Articles

Loading

 
Top