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.
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
Open the ProjectStart 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
Debug ProjectClick 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.
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.
The code really does very little and is described below.
A text substitution constant called CONSTANT1 is created using the preprocessor directive #define.
A constant variable called CONSTANT2 is declared using the const keyword.
A variable called variable1 is declared and initialized with the value of CONSTANT1.
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.
Now that we are into executable code, we can now assign the value of the constant variable CONSTANT2 to the variable variable2.
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.
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.
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.
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.