Tuesday, 3 December 2013

How to set PIC16F628A configuration bits in code ?


This post provides the method to set configuration bits in the code for PIC16F628A microcontroller. You have to set configuration bits in order to make PIC microcontroller work correctly. Here I am going to explain the procedure of selecting configuration bits for PIC16F628A. A typical example code for setting configuration bits in the code (using MPLAB + 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". You should 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. Snapshot of these macros is shown below.






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.

Code


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.


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.

Notes and References

[1] For details check manual of HI-TECH C Chapter 3.2.2.
[2] You can download LED blinking code for PIC16F628A, from the 'Downloads' section at the bottom of this page.
[3] For details of these settings, check the PIC16F628A datasheet section "Special features of the CPU".

No comments:

Post a Comment