SFR Macros

If you are writing code that can be used by several devices, accessing special function registers can be problematic since the names of registers can change from one device to another or the register might not be available on all devices. Typically, programmers duplicate device-specific code and compile it conditionally, based on the particular device being used. The following example attempts to write to a port latch if one is available; otherwise, it writes directly to the corresponding port.

If you are using a recent MPLAB® XC compiler, you can alternatively compile similar code based on the presence of the actual register symbol rather than based on a device, as in:

Compiling against the register allows the code to be more versatile and target more devices, and since it is checking the actual register you are about to access, it is more reliable.

Note that some registers also have aliased symbols in the device-specific header file. These register aliases might not have macro definitions available and should not be used as shown above.

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