0

I writing a program to control several pumps and monitor a switches, and my goal is twofold: first, to reduce the amount of code and second to make it easier to remember and keep track of which pin is which LED/switch.

I have two methods and can't determine which would be better in the long run, ie maintaining the code in the future.

The first is to create function blocks, such as:

void pump_on(void)
{
    PORTC |= _BV(PC5); 
}

The second would be a define:

#define pump_on PORTC |= _BV(PC5)

Is one preferable over the other?

pekasus
  • 626
  • 2
  • 7
  • 20
  • See also [Inline functions vs Preprocessor macros](http://stackoverflow.com/q/1137575/33499) for a c++ related answer – wimh Mar 07 '15 at 19:43
  • Thanks for that! This does answer my question. Functions it is. – pekasus Mar 07 '15 at 19:50
  • I wouldn't have marked it a dupe due to that this is embedded-specific. Good, properly optimizing compilers might simply not exist, so making the function call a mess of assembly code on the micro, both larger in size and in processing time for such small functions. My answer would be to always check the disassembly if either matters. Otherwise you can definitely use the function to have your code easier to debug. – Jubatian Mar 08 '15 at 07:32

1 Answers1

0

I'd definitely go for the function, as it is less error prone (probalby not in your exact example, though) and should have the exact same efficiency on any decent compiler.

However, I'd think about parameterizing it like this:

void set_pump(_Bool enable) //or void set_pump(bool enable) if you are using c++
{
    if (enable) {
        PORTC |= _BV(PC5); 
    } else {
        PORTC &= ~_BV(PC5);
    }
}
MikeMB
  • 20,029
  • 9
  • 57
  • 102
  • Thanks! I used this to make a python script to build a library so I don't have to retype https://github.com/pekasus/AVR-Dev-Tools/blob/master/pinout_builder.py – pekasus Mar 07 '15 at 23:42