This lab uses a simple program to illustrate how configuration bit settings can cause a system’s behavior to deviate from what is written in the code. During this lab the student will learn:
- How to read the current settings of configuration bits
- How to alter the code settings changing the configuration bits
- How to program the configuration bits into the PIC
Hardware Tools (Optional)
|Tool|| About|| Purchase|
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
The contents of the following zip file need to be placed in this directory:
| Windows|| Linux|| Mac OSX|
Project and Source Files
Open the Config Bits Lab
Open MPLAB® X IDE
Close any open projects in MPLAB® X IDE by right clicking on the project name and selecting Close or by going to File and choosing Close All Projects.Click the Open Project icon
Browse to C:\MTT\TLS0101\ConfigBitsLab.X and select Open Project
Open the ConfigBits.c File
Double click on ConfigBits.c under Source Files folder to open the C file in the editor window.
The content of ConfigBitsLab.c (the main source file for this project) is not considered in this lab; instead we will concentrate on ConfigBits.c. This file contains the compiler directives to set the PIC configuration bits for this project. Based on the contents of these directives, the compiler will append the appropriate words to the build image before the image is programmed into the PIC. Best practices call for the programmer to code the desired configuration bit settings somewhere in the project. If the configuration settings are not coded the compiler will generate the default configuration bit settings.
Configuration bits can be set in any source file within the project. Quite often configuration bits are set within the main.c file. They can also be defined in an included header (.h) file. This lab uses a separate file called ConfigBits.c to program the configuration bits which makes it easier to manage them as we will see in the following steps.
Build the ProjectClick on the icon. This will clean and build the project.
PIC Memory Views Window
Open the Configuration Bits window
Window -> PIC Memory View -> Configuration Bits
Once you have decided what the configuration bits settings should be, there are two options to build them into your code. One method is to look into the processor header file and compiler reference material to find the specific syntax for setting the configuration bit, and then enter the code into the project - OR - you can have MPLAB® X generate the correct code for you. In this lab we will show you how to automatically generate the appropriate code for the configuration bits. We will then put the new code into ConfigBits.c
The Configuration Bits window informs us the Watchdog Timer is currently disabled. For this example we have decided to enable the WDT with the prescaler set to 128 and the postscaler set to 512.
Modifying the Configuration Bits
Click on the text in the Setting column indicating the “Watchdog Timer is disabled”
Observe that a pull down menu with the available options appears
Select the option “Watchdog Timer is enabled”
When the configuration bit setting is changed you will notice the changes show up in RED on the Configuration Bits window
Generating the Configuration Code
Click the button titled “Generate Source Code to Output”
A tab in the Output window will open and display the code needed to generate the configuration bit settings.
Compare the code in the Output Window to the code currently in ConfigBits.c to ensure the changes you want have been coded.
Putting the New Code into the Project
Right click on the newly opened “Config Bits Source” tab and select “Save As”
Save the file as C:\MTT\TLS0101\ConfigBitsLab.X\ConfigBits.c
Overwrite the existing file when asked.
The text may be saved as a header file (.h) instead of a C file. You may also simply “cut and paste” the content of this window to a file of your choosing.