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 icon on the main toolbarNavigate to the folder where you saved the exercise files for this class.
Click on the Lab19.X folder.
Select Open Project .2
Debug Project
Click on the Debug Project button. This will build and send the program to the simulator.Click on the Continue button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt button.
Results
3
End Debug Session
End the Simulation Session by clicking the Finish Debugger Session 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.