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.
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
Open the ProjectStart MPLAB® X, 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 as we look at the Watches Window and the Program Memory View.
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 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.