• AVR Freaks

Hot!Question regarding Pin Manager generated code

Author
eko
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2015/07/22 03:48:49
  • Location: 0
  • Status: offline
2016/01/28 03:58:42 (permalink)
0

Question regarding Pin Manager generated code

Hey guys!
 
When I look at the generated "pin_manager.h" file I see stuff like this everywhere:
 
#define P2B_SetHigh() do { LATC0 = 1; } while(0)
 
So why this single run do-while-loop? Where is the advantage of
 
do { LATC0 = 1; } while(0)
 
over simply
 
LATC0 = 1?
 
It would be nice if someone could enlighten me, though I know it makes no difference in the compiled code (I hope at least).
 
TIA!
 
#1

3 Replies Related Threads

    IonutB
    Moderator
    • Total Posts : 115
    • Reward points : 0
    • Joined: 2008/08/04 09:43:55
    • Location: 0
    • Status: offline
    Re: Question regarding Pin Manager generated code 2016/01/29 09:41:39 (permalink)
    4 (1)
    Hi eko,
     
    The macros provided by MCC in the Pin Manager header file help you customize and maintain your generated code easily. Sure you can use direct register/bit access statements instead, but you might have trouble maintaining your code later on. MCC offers you the possibility of defining a custom name for each selected pin. Based on this custom name, several macros are generated in the Pin Manager header file, so that you won't need to figure out which register/bit corresponds to what pin. You can simply select one of the macros for each pin function you need to configure.
     
    The "do { ... } while (0)" macros are a common practice to ensure the content of a macro executes in the way it was intended to. As you know, unlike ordinary functions, macros lack type checking and they are expanded by the preprocessor exactly as defined in the code. This can easily result in errors by using the macro in ways it was not designed for. One of these cases are multiple statements within the same macro.
     
    Take this case for example:
     
    #define WRITE_MESSAGE (m) { write ("start: "); write (m); write ("end") }

     
    This macro will result in build error if used incorrectly, such as:
     
    if (condition1)
         WRITE_MESSAGE (message1);
    else if (condition2)
         WRITE_MESSAGE (message2);
     
     
    This piece of code is expanded as:
     
     
    if (condition1)
         write ("start: "); write (message1); write ("end");
    else if (condition2)
          write ("start: "); write (message2); write ("end");

     
    which will obviously break away the else branch from the if statement and determine a build error. This behavior is often referred to as 'swallowing the semicolon'.
     
    If we rewrite the macro as:
     
    #define WRITE_MESSAGE (m) do { write ("start: "); write (m); write ("end") } while(0) 

     
    the whole contents of the do-while loop will be executed when either condition1 or condition2 are true, thus allowing the code to build and function as intended.
     
     
    #2
    bconklin
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2013/06/24 17:12:35
    • Location: 0
    • Status: offline
    Re: Question regarding Pin Manager generated code 2019/07/26 09:08:35 (permalink)
    5 (1)
    The generated code only uses a single statement, making your whole argument invalid.
    #3
    mbrowning
    Just a Member
    • Total Posts : 1500
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: Question regarding Pin Manager generated code 2019/07/26 10:29:46 (permalink)
    5 (3)
    You’ve been holding that back for 3 years but just couldn’t keep silent any longer, right ? :)

    Oh well - there's always next year
    #4
    Jump to:
    © 2019 APG vNext Commercial Version 4.5