How to Set PIC16F628A configuration bits in code !

This article provides the method to set configuration bits in the code for PIC16F628A microcontroller. To set configuration bits in order to make PIC microcontroller work correctly. A typical example code for setting configuration bits in the code (using Proteus + HI-TECH C compiler) is also provided. Following steps are required to setup configuration bits for PIC16F628A.

Step 1:
The first step to start writing the configuration bits is to check "Special Features of the CPU" section in the datasheet of PIC16F628A  In this section, first chapter is about "Configuration bits". Read this section and identify what values you want to put in the configuration register.

Step 2:
Open the file "pic16f628A.h" from the directory "C:\Program Files\HI-TECH Software\PICC\9.83\include". In the start of this file configuration bit macros are defined. Codes from text file of these macros is shown below.


#ifndef    _HTC_H_
#warning Header file pic16f628a.h included directly. Use #include <htc.h> instead.

/* header file for the MICROCHIP PIC microcontroller
 *  16F628A

#ifndef __PIC16F628A_H
#define __PIC16F628A_H

// Configuration mask definitions
// Config Register: CONFIG
#define CONFIG               0x2007
// Oscillator Selection bits
// RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
#define FOSC_EXTRCCLK        0xFFFF
// RC oscillator: I/O function on RA6/OSC2/CLKOUT pin, Resistor and Capacitor on RA7/OSC1/CLKIN
#define FOSC_EXTRCIO         0xFFFE
// INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
#define FOSC_INTOSCCLK       0xFFFD
// INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
#define FOSC_INTOSCIO        0xFFFC
// EC: I/O function on RA6/OSC2/CLKOUT pin, CLKIN on RA7/OSC1/CLKIN
#define FOSC_ECIO            0xFFEF
// HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
#define FOSC_HS              0xFFEE
// XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
#define FOSC_XT              0xFFED
// LP oscillator: Low-power crystal on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
#define FOSC_LP              0xFFEC
// Watchdog Timer Enable bit
// WDT enabled
#define WDTE_ON              0xFFFF
// WDT disabled
#define WDTE_OFF             0xFFFB
// Power-up Timer Enable bit
// PWRT disabled
#define PWRTE_OFF            0xFFFF
// PWRT enabled
#define PWRTE_ON             0xFFF7
// RA5/MCLR/VPP Pin Function Select bit
// RA5/MCLR/VPP pin function is MCLR
#define MCLRE_ON             0xFFFF
// RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD
#define MCLRE_OFF            0xFFDF
// Brown-out Detect Enable bit
// BOD enabled
#define BOREN_ON             0xFFFF
// BOD disabled
#define BOREN_OFF            0xFFBF
// Low-Voltage Programming Enable bit
// RB4/PGM pin has PGM function, low-voltage programming enabled
#define LVP_ON               0xFFFF
// RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming
#define LVP_OFF              0xFF7F
// Data EE Memory Code Protection bit
// Data memory code protection off
#define CPD_OFF              0xFFFF
// Data memory code-protected
#define CPD_ON               0xFEFF
// Flash Program Memory Code Protection bit
// Code protection off
#define CP_OFF               0xFFFF
// 0000h to 07FFh code-protected
#define CP_ON                0xDFFF


Step 3:
Here we have to choose appropriate macros to be used in the code to set configuration bits correctly. For each configuration bit we have a choice between some macros. For example, for watchdog timer enable bit, we have a choice between WDTE_ON or WDTE_OFF. If we want to enable the watchdog timer, then WDTE_ON macro should be used in the code. But if we want to disable watchdog, then WDTE_OFF macro should be used in the code to set the configuration bit accordingly. Similarly, you can select appropriate macros for other configuration bits.


We can program configuration bits in the code using __CONFIG macro. The code to make an LED blink with PIC16F628A is shown below in the figure. You can see how __CONFIG macro is used here, to set the desired configuration bits in the code.

Microchip PIC16F628A, LED Blinking Code Using ' C ' Language, Time Delay 1 Seconds:

 * Created:   Mon Jun 8 2015
 * Processor: PIC16F628A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed

#include <htc.h>
//Configuration Word


//Define Pins

#define LED    RB5

//Define CPU Frequency
//This must be defined, if __delay_ms() or __delay_us() functions are used in the code

#define _XTAL_FREQ 20000000

//Main Function Start from here

void main(void)
   // Write your code here
    TRISB5 = 0;                   //Make LED pin an output
    LED = 0;                        //Turn LED off        
   while (1)
      LED = !LED;               //Toggle LED pin state
      __delay_ms(1000);    //1000 milli second delay


You have to place the __CONFIG macro outside the main function, preferably in the start of the file. All the selected macros are separated with the & (AND operator) in code. In this code selected configuration bits for PIC16F628A are,

  • External HS crystal is selected as CPU clock source.
  • Watchdog is disabled.
  • Power up timer is enabled.
  • MCLR pin is enabled.
  • Brown out is enabled.
  • Low voltage programming is disabled.
  • Data memory protection is disabled.
  • Program memory code protection is disabled.

Using this procedure you can easily write configuration bit settings in the code. You can leave your comments in the comment section below.

More From