Thread: Please explain what this macro is doing

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    17

    Please explain what this macro is doing

    Hello,

    I am working with PIC32CM controller using Harmony3 framework. Can someone explain the following use of the language:

    //Macro defined:
    #define LED_Set() (PORT_REGS->GROUP[0].PORT_OUTSET = ((uint32_t)1U << 24U))

    I understand it is a function-like macro, but when it is called in main.c, another macro sets it this way

    #define LED_Off LED_Set

    Why parenthesis are missing from LED_Set?

    and finally it is called LED_Off(); in a function inside main().

    All these uses/not-use of parenthesis are not making sense. Please help.

    Thanks,

    BTW: If someone is experienced with PIC32CM1216MC000xx or similar microcontroller, I am willing to pay for their time to answer my questions any reasonable price. Though I am not an expert, I understand C-language but having difficulty decoding statements like (PORT_REGS->GROUP[0].PORT_OUTSET = ((uint32_t)1U << 24U)). Please contact me via email.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by boomeral
    I understand it is a function-like macro, but when it is called in main.c, another macro sets it this way

    #define LED_Off LED_Set
    Remember, the first identifier is the macro name, and the rest is what the macro consists of. So, this doesn't set the LED_Set macro; this sets the LED_Off macro to LED_Set. This way, you can use the LED_Off macro exactly as you would have used the LED_Set macro, except that if you want to substitute those uses for another macro, you just change this #define to set LED_Off to something else that is compatible.

    Quote Originally Posted by boomeral
    Why parenthesis are missing from LED_Set?
    So that where LED_Off is used, it will be used such that it looks like a function call. If you put the parentheses for LED_Set in the #define, you would then have to either leave out the parentheses when using LED_Off (which presumably looks odd as it is supposed to be function-style), or you would have to add it in for the #define:
    Code:
    #define LED_Off() LED_Set()
    which may seem like unnecessary complication.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    LED_Off();
    becomes...
    Code:
    LED_Set();
    which becomes
    Code:
    (PORT_REGS->GROUP[0].PORT_OUTSET = ((uint32_t)1U << 24U));
    Sort of like a copy and paste thing

    This...
    Code:
    ((uint32_t)1U << 24U));
    is saying that the unsigned 32 bit number 1 is shifted to the left 24 times.
    i.e.
    Code:
    0000 0000 0000 0000 0000 0000 0000 0001 << 24
    =
    0000 0001 0000 0000 0000 0000 0000 0000
    It is the way of setting an individual bit in a portable way.

    The 'U' is saying to the compiler that this number is an unsigned int

    PORT_REGS is a pointer, that's why you need to use ->

    You appear to have different output groups, and they are organised into different array elements. Each one of those elements has a member called PORT_OUTSET, which would be related to the output ports of your device.

  4. #4
    Registered User
    Join Date
    Mar 2021
    Posts
    17
    Wow! Laserlight. Very well explained.

    The compiler behaves exactly as you have suggested for the alternate uses of parenthesis:

    #define LED_Off LED_Set() --- but now have to leave out the parenthesis when using the LED_Off in the main code.
    or
    #define LED_Off() LED_Set() --- use parenthesis with both the name and the declaration and also use parenthesis when using the LED_Off() in the main code.

    Thanks for your great help!

  5. #5
    Registered User
    Join Date
    Mar 2021
    Posts
    17

    Please explain what this macro is doing

    Hi Click_here,

    Thanks for explaining the statement with example. I appreciate your help.
    For some reason, when I click on reply, the cart-wheel appears but page does not transition - must be some bug.

    I understand the bit-shifting statement, but having difficulty with statements prior to bit-sit statement. I search in the Harmony3 generated code but cannot find what I am looking for.

    Thanks again for your help!

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Think of it like cut and paste.

    If I type...
    Code:
    LED_Off();
    ... it will "cut" the "LED_Off" leaving the "();"
    Code:
    <snip>();
    ... and replace it with "LED_Set"
    Code:
    LED_Set();
    The next one will also snip the "()"

    Code:
    LED_Set();
    ...
    <snip>;
    ...
    (PORT_REGS->GROUP[0].PORT_OUTSET = ((uint32_t)1U << 24U));

  7. #7
    Registered User
    Join Date
    Mar 2021
    Posts
    17

    Please explain what this macro is doing

    Thanks Click_here,

    Another great explanation.

    Boomer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking a macro's sign in order to define other macro
    By Egalestrenge in forum C Programming
    Replies: 5
    Last Post: 05-23-2016, 09:39 PM
  2. Is this possible for c Macro?
    By Wener in forum C Programming
    Replies: 2
    Last Post: 08-05-2012, 02:30 AM
  3. macro use.
    By gourav87 in forum C Programming
    Replies: 8
    Last Post: 02-15-2012, 11:04 AM
  4. Can someone explain the following macro in detail?
    By lilzz in forum C Programming
    Replies: 5
    Last Post: 02-23-2011, 01:42 PM
  5. help with macro
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 01:55 PM

Tags for this Thread