Lab2: C 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 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, 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

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

The code really does very little and is described below.

Lab02C.png

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.

Uart2.png

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

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 8 K of memory or being forced into a more expensive part with 12 K or 16 K 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.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
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.