• AVR Freaks

Hot!How do you use Preprocessor directives for embedded program

Author
madakegajya
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/12/06 02:19:30
  • Location: 0
  • Status: offline
2020/02/24 03:41:06 (permalink)
0

How do you use Preprocessor directives for embedded program

Did you use Preprocessor directives #ifdef, #if, #defined, #else and #elseif  in embedded programming, why did you think you should use Preprocessor directives in your project ?
 
Just to learn, I wrote a program and tested it on PC

#include <stdio.h>
#define X 1
int main(void)
{
#ifdef X
printf("hello\n");
#endif

return 0;
}

 
I have found that  Preprocessor directives mostly used in header file for embedded program 
 
Can anyone tell how do you use them in  embedded programming 
#1

6 Replies Related Threads

    ric
    Super Member
    • Total Posts : 26090
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How do you use Preprocessor directives for embedded program 2020/02/24 21:03:23 (permalink)
    +1 (1)
    They allow you to do a test at compile time. E.g. to optionally disable a whole block of code.
    In your example, the printf() line will be totally left out of your program if X is not defined.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11673
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: How do you use Preprocessor directives for embedded program 2020/02/24 22:05:34 (permalink)
    +2 (2)
    A typical use is to compile the same code for different hardware targets, or brand names, or model numbers.  E.g., I have thermostat code that that uses preprocessor directives to build 58 different hex files based on various features and hardware.
    #3
    LdB_ECM
    Super Member
    • Total Posts : 300
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: How do you use Preprocessor directives for embedded program 2020/02/24 22:23:02 (permalink)
    +1 (1)
    The essential use is to pickup up the COMPILER, TARGET or LIBRARY predefined constants and macros the code may require to know when writing portable code.
     
    The user use like your example although nice sometimes is just a bonus of the essential feature. That is why your example does not show when it is absolutely required and essential and why you would be questioning why it exists.
    #4
    Aussie Susan
    Super Member
    • Total Posts : 3685
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How do you use Preprocessor directives for embedded program 2020/02/25 18:23:00 (permalink)
    +2 (2)
    I use them to add in some debugging code (e.g. toggling an IO port when something happens) that I don't want to have in the final code.
    Also, generally speaking , embedded code does not need 'stdio.h' because you really don't have an OS to handle the terminal/screen interactions for you. (You can set up a UART to do that - perhaps for debugging - but embedded systems generally don't have a lot of user interactions except through (say) an LCD screen and buttons etc..)
    However you DO need to include something like the 'xc.h' at the start so that the compiler knows (from the IDE) which MCU you are using and therefore the registers that it has.
    Susan
    #5
    NorthGuy
    Super Member
    • Total Posts : 5961
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: online
    Re: How do you use Preprocessor directives for embedded program 2020/02/26 08:11:11 (permalink)
    +1 (1)
    If you have to write lots of similar fragments of code, you can define a big macro which expands into several lines of code. This makes for less writing and more readability. And then if you need to change something, you only need to change the macro and everything changes at once.
    #6
    KTrenholm
    Super Member
    • Total Posts : 783
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: How do you use Preprocessor directives for embedded program 2020/02/26 08:37:11 (permalink)
    +1 (1)
    I use them very frequently for Debug/Testing builds.
     
    Like a #ifdef SERIAL_DEBUG to enable/disable blocks of code if you need serial debugging output when developing, but then just undefine SERIAL_DEBUG for release code.
     
    Or I also have systems with smaller PICs where I want periodic serial logging functions for running in a temperature chamber, but in actual release code I need that codespace to fit in a bootloader.  In that case I can put all my serial logging functions and calls into a big #ifdef ENABLE_SERIAL_LOGGING block so it's nice and easy to add/remove.
     
    Or even just for defining constants:
    #ifdef PRODUCTION
    #define FW_TYPE    "PRODUCTION"
    #else
    #define FW_TYPE    "TESTING"
    #endif
     
    set your preprocessor definition to your project configuration and you can just select your config from the MPLAB X dropdown to automatically enable/disable code as appropriate.
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5