How to set PIC16F877A configuration bits in code ?
This post provides the method
to set configuration bits in the code for PIC16F877A 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 PIC16F877A. 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 PIC16F877A.
Step 1:
The first step to start writing the configuration bits is to check "Special Features of the CPU" section in the datasheet of PIC16F877A. 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 "pic16f877a.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 1:
The first step to start writing the configuration bits is to check "Special Features of the CPU" section in the datasheet of PIC16F877A. 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 "pic16f877a.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.
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[1]. The
code to make an LED blink with PIC16F877A[2] 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 PIC16F877A are[3],
·
External HS crystal is
selected as CPU clock source.
·
Watchdog is disabled.
·
Power up timer is
enabled.
·
Program memory code
protection is disabled.
·
Brown out is enabled.
·
Low voltage
programming is disabled.
·
Internal EEPROM data
memory protection is disabled.
·
Flash program memory
write is enabled.
·
In circuit debugger is
disabled, RB6 and RB7 are general purpose pins.
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 PIC16F877A, from the 'Downloads' section at the bottom of this page.
[3] For details of these settings, check the PIC16F877A datasheet section "Special features of the CPU".
[2] You can download LED blinking code for PIC16F877A, from the 'Downloads' section at the bottom of this page.
[3] For details of these settings, check the PIC16F877A datasheet section "Special features of the CPU".
Downloads
LED blinking code using PIC16F877A was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.
No comments:
Post a Comment