Sunday, February 12, 2017

Advance MACRO Function in Embedded C Programming.

In Embedded system C programing MACRO functions are very powerful, it can do the things which traditional C functions can't. As an embedded programmer its very helpful, if you can use these advantages of MACRO functions. You can even create a macro functions as template of function and simply use it, depending on input argument you can even change the operation of that particular function.

Macros can also accept parameters and return values. Macros that do so are called macro functions. To create a macro function, simply define a macro with a parameter that has whatever name you like, such as my_val. For example, one macro defined in the standard libraries is abs, which returns the absolute value of its parameter. Let us define our own version, ABS, below. (Note that we are defining it in uppercase not only to avoid conflicting with abs, but also because all macros should be defined in uppercase, in the GNU coding style.) 

#define ABS(my_val) ((my_val) < 0) ? -(my_val) : (my_val)

-From Examples of GNU

Whenever we think about MACRO function, most of the time we are assuming the functions (ABS) mentioned in above example?? , If we think little bit out of box, we can found a huge advantages of MACRO function, which even change the way we use to write Embedded C programs. The use of macro functions are very simple, you just keep in mind that wherever you use macro function it just replace  function call with function definition. Syntax of macro function is as mentioned below.


#define MACRO_Function_Name ( Argument_1, Argument_2... Argument_n )  \
 { \
/* Function Definitions */  \
}


The main Advantages of MACRO functions are,

To explain this in more detail, consider following example of  MACRO function to calculate Checksum with supports any Data type and different operation to calculate Checksum.

1. Argument type, For a MACRO function you can pass any C keyword, operators, variable names, Identifiers.. etc. as the input argument of MACRO functions. It gives a huge flexibility in writing General functions.

Eg:- To calculate the check sum of  input argument with different types such as  char and long, you can use the


2. Can change the definition of a Macro function in certain C files, where you like to function it differently.
Eg:- If the name of the MACRO function to calculate check sun is GET_CHECK_SUM, and if you like to do a simple addition in a particular C file or in a function , then just redefine at the top of  file or in the function. Because C is a Top to bottom function.

#undef GET_CHECK_SUM
#define GET_CHECK_SUM ( a, b)  a+b


  The main things you need to remember while creating MACRO functions are,
  • Please put back slash / Line break ( \ ) at the end of each line of macro function, not required on last line of the function.
  • In most of the IDE using for embedded development its difficult to debug a macro function, So my suggestion is to convert this macro function to traditional function and once issue was debugged change same to macro function. If your IDE supports then its ok.
  • If you are declaring any variable in MACRO function, Make sure that its name is unique, else it will considered it as a local variable.
  • Make sure to put semicolon (;) after each statements.
  • do not do any assignment  operation on input arguments if they are constants. You can do any assignment  operation on input arguments if you passing local or global variable on MACRO function call.

No comments:

Post a Comment

Please give your suggestion and follow us if you are interested, which encourage us to create new topics for you. And Thankyou for your support.