ifdef can be used in many ways.
The same code can be made compatible with multiple compilers including XC8 and C18. In different compilers, function names and PIC register name definition may be different. Using the #ifdef, the same program can be made compatible with these two compilers.
The below example shows how to incorporate different header files for their compiler counterparts. Three compilers are compatible with this code, XC8, C18 and HiTech compiler.
#if defined(__XC) #include <xc.h> /* XC8 General Include File */#elif defined(HI_TECH_C) #include <htc.h> /* HiTech General Include File */#elif defined(__18CXX) #include <p18cxxx.h> /* C18 General Include File */#endif
Debugging* can be made easy by using the #ifdef. Define the debug routines inside the #ifdef and share the code with anyone. By using a #def macro definition, the testing procedures can be enabled.
#def Test_Mode_enabled /* comment this lines when the code is not in test mode */ #ifdef Test_Mode_enabled UART_Write('Debug enabled'); /* Debug Procedures */#endif*This is not the actual debug mode used by In-Circuit debugger(ICD). That can be invoked by compiler specific directive #ifdef _DEBUG
An example for this method is enable and disable some features by detecting the Microcontroller family.
For example, enable USB features if the Microcontroller is PIC18F4550.
#ifdef _18F4550 USB_Enable(); /* Special features to enable */#endif
These are only some examples and the possibilities are infinite. Use it well..!
Learn more about embedded c visit: http://learn.openlabpro.com/online-courses/embedded-c-course/