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 IDE.
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 IDE, then click on the Open Project 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
2
Debug Project
Click on the Debug Project 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 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 IDE as we look at the Watches window and the Program Memory View.
Results
The code really does very little and is described below.
Line 12
A text substitution constant called CONSTANT1 is created using the preprocessor directive #define.
Line 13
A constant variable called CONSTANT2 is declared using the const keyword.
Line 18
A variable called variable1 is declared and initialized with the value of CONSTANT1.
Line 19
A variable called variable2 is declared, but we cannot initialize it with CONSTANT2 because a variable may not be initialized at declaration with another variable. CONSTANT2 is a constant variable (a variable whose value may not be changed in code), and therefore it may not be used in this case.
Line 39
Now that we are into executable code, we can now assign the value of the constant variable CONSTANT2 to the variable variable2.
Lines 44-45
These lines simply print out the value of the two constants to the UART 1 Output window.
The UART 1 Output window should show the text that is output by the program, indicating the values of the two symbolic constants in the code.
Analysis
4
Watches Window
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.
5
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 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 IDE 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.