Lab 19: Using Macros in a define statement

Last modified by Microchip on 2024/01/10 20:46

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.

Reference Materials

Exercise Files

Procedure

Open the Project

Start MPLAB X IDE, then click on the Open Project Open Main Project button 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 Open Project Button.


Debug Project

Click on the Debug Project Debug Main Project button button. This will build and send the program to the simulator.

Click on the Continue Debug Continue button button. This begins the simulation. Wait for the UART 1 Output window to finish printing.

Click on the Halt Debug Pause button button.


Results

Lab19 Results in UART 1 Output Window

After running the program, the UART1 Output window
will display the following:

x = 9

SPBRG = 25

Code Analysis

Line 12
This first macro may be used to compute the square of any variable passed to it. Some care must be exercised when using it, but using macros like this can simplify your code and make it more readable:

#define square(m) ((m) * (m))

It may be used in your code much like a function. The benefit of using a macro like this is that it doesn’t care what data type the parameter has. The disadvantage is that the code will be compiled inline and take up more space than a function call if it is used frequently.

Line 13
This handy utility macro may be used to calculate the value to write to a UART’s baud rate generator control register. This particular macro is intended for the PIC18 family but could be modified easily for other PIC families.

#define BaudRate(DesiredBR, FoscMHz)

(FoscMHz * 1000000)/DesiredBR)/64)-1)

Note that the desired baud rate (DesiredBR) and oscillator frequency (FoscMHz) must be known at compile time. In other words, you can only pass constants to a macro. Trying to pass a variable, whose value cannot be known at compile time, will produce an error.
Using a macro like this makes it much easier to change the parameters at design time, or to make your code more easily customizable for the future.

Line 34
The square() macro is invoked here by passing the value 3 to it. It will return a value of 9 to be stored in the variable x.
x = square(3);

This line of code will not generate any extra overhead. It will be the same as if you had written:
x = 9;

Line 37
This line is used to calculate the value to be loaded into the SPBRG register, which controls the baud rate of the USART on a PIC18:

SPBRG = BaudRate(9600, 16);

Just like above, no extra overhead is generated here. This line will generate the same assembly code as:

SPBRG = 25;


End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Finish Debugger Session button button.
Clear out the UART 1 Output window by entering (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.