Lab Exercise 19: Macros with #define

 Objective

This demo illustrates some of the many uses for macros. Macros created with the #define directive can help simplify and add flexibility to your code. Macros are operations that will be performed by the compiler when it is building your code. So anything you could compute at compile time can be handled by the compiler for you.

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 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 Lab19.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.
Click on the Continue Debug_Continue.png button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt Debug_Pause.png button.

 Results

3

End Debug Session

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

Clear out the UART 1 Output window (Ctrl + L).

Close the Project.

 Conclusions

Much like enumerations, macros can make your code more readable and easier to maintain. However, unlike enumerations, there is much more potential for misuse. So, extreme care must be exercised when writing a macro and when invoking a macro.

© 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.