Lab 5: Configuration Bits

 Objective

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

 Materials

Hardware Tools (Optional)

Tool About Purchase
Explorer16-50px.png
Explorer 16
Development Board
ICD3-50px.png
MPLAB® ICD 3
In-Circuit Debugger

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC16
C Compiler

Exercise Files

The contents of the following zip file need to be placed in this directory:
C:\MTT\TLS0101

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

1

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 File_OpenProject.png

Browse to C:\MTT\TLS0101\ConfigBitsLab.X and select Open Project

2

Open the ConfigBits.c File

Double click on ConfigBits.c under Source Files folder to open the C file in the editor window.

ConfigBits.png

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.

3

Build the Project

Click on the Main_Rebuild_Project.png icon. This will clean and build the project.

4

PIC Memory Views Window

Open the Configuration Bits window

Window -> PIC Memory View -> Configuration Bits

ConfigurationBitsWindow.png

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.

ConfigBitsOpen.png

5

Modifying the Configuration Bits

Click on the text in the Setting column indicating the “Watchdog Timer is disabled”

Watchdog.png

Observe that a pull down menu with the available options appears

Watchdog2.png

Select the option “Watchdog Timer is enabled”

ConfigChange.png

When the configuration bit setting is changed you will notice the changes show up in RED on the Configuration Bits window

6

More Changes

Repeat step 5 so that the WDT Prescaler is set to 128 and the Watchdog Timer Postscaler is set to 512.

ChangedConfig.png

7

Generating the Configuration Code

Click the button titled “Generate Source Code to Output

GenerateCode.png

A tab in the Output window will open and display the code needed to generate the configuration bit settings.

Compare.png

Compare the code in the Output Window to the code currently in ConfigBits.c to ensure the changes you want have been coded.

8

Putting the New Code into the Project

Right click on the newly opened “Config Bits Source” tab and select “Save As

SavingConfigBits.png

Save the file as C:\MTT\TLS0101\ConfigBitsLab.X\ConfigBits.c

SaveConfig.png

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.

9

Verify Changes

Rebuild the Project to incorporate the changes Main_Build_Project.png

Verify the configuration bits for the project have been modified by opening the Configuration Bits PIC Memory View window.

Window -> PIC Memory View -> Configuration Bits

FinalConfig.png

Verify your coded changes are in the Configuration Bit Settings

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.