Lab Exercise 2: Symbolic Constants

 Objective

The purpose of this lab is to illustrate the difference between symbolic constants declared with the const keyword and those declared with the #define preprocessor directive. Upon completion of this lab, you will see that constants declared with the const keyword will consume program memory locations, while constants declared with #define require no memory at all. You will also understand how both types of constants are displayed in watch windows within the MPLAB® X Integrated Development Environment.

Software Tools

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

Exercise Files

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

 Procedure

1

Open the Project

Start MPLAB® X, then click on the Open Project Main_Open_Project.png icon on the main toolbar.

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab02.X folder.

Select Open Project OpenProjectButton.png.

2

Debug Project

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator to begin the simulation.

Wait for the UART 1 Output window to finish printing.

Click on the Halt Debug_Pause.png button. This will stop execution so that we may examine the variables and their values.

3

What just happened?

Just like the last lab, we took a pre-configured project and compiled its code. The program creates two constants. One constant is declared using the const keyword and the other is declared using the #define compiler directive. The constant declared with const will be placed in program memory as a “constant variable”. The constant declared with #define however, requires no memory whatsoever, and we will see this in MPLAB® X as we look at the Watches Window and the Program Memory View.

 Results

 Analysis

4

Watches Window

NewWatch2.png

Open the Watches Window (Windows-> Debugging-> Watches -or- (Alt + Shift + 2)).

Right click in the Watches Window. Select “New Watch”.

Attempt to add CONSTANT1 and CONSTANT2 to the Watch list.

Only CONSTANT2 appears on the list of Global Symbols to be added to the Watches Window. This is because only CONSTANT2 has an address in physical memory.

Constant2.png

5

Program Memory

ProgramMemory.png

Open the Memory View window of the Program Memory Space (Window-> PIC Memory Views-> Program Memory).

Scroll down to the address listed from the Watches Window (this example uses 0xE72). Observe CONSTANT2 occupies an address. CONSTANT1 does not appear anywhere in the Program Memory.

6

End Debug Session

Clear the UART 1 Window - put the cursor in the UART 1 Window then enter Ctrl +L. This will clear the UART 1 Window before your next simulation.

Erase the Watches Window by right clicking in the Watches Window and selecting Delete All.

End the Simulation Session by clicking the Finish Debugger Session Debug_Finish_Debugger_Session.png button.

Then CLOSE the Project by right clicking on Lab02 from the Projects Window and then selecting Close.

 Conclusions

While there may be some circumstances when you need to declare a constant variable using const, in the overwhelming majority of cases, you will be better off using #define. In a microcontroller, with its limited memory resources, constants declared with const can quickly consume valuable program memory space, which could in some cases make the difference between using a part with 8K of memory or being forced into a more expensive part with 12K or 16K of memory. Constants declared with #define are handled by the preprocessor by substituting the text label with the constant’s value before the code actually gets compiled. Therefore, no memory on the microcontroller is used to accommodate constants declared with #define. However, MPLAB® X is still capable of displaying either kind of constant in a watch window if desired. Constants declared with const will be shown with their program memory address and a ‘P’ next to it. Constants declared with #define will simply be shown without any address next to it.

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.