Please explain this code to me

I found this piece of code that is used to provide power to a Wiichuck (converterboard for use with Wii Nunchuck)

static void nunchuck_setpowerpins()
{
#define pwrpin PC3
#define gndpin PC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);
    delay(100);  // wait for things to stabilize        
}

Can someone explain what it exactly does?

It's doing the equivalent of

#define PWRPIN 17 // 3 + 14 == Analog Pin 3
#define GNDPIN 16 // 2 + 14 == Analog Pin 2
pinMode(PWRPIN, OUTPUT);
pinMode(GNDPIN, OUTPUT);
digitalWrite(PWRPIN, HIGH);
digitalWrite(GNDPIN, LOW);
delay(100);

But it's using direct port manipulation to do it.

That is easier to read and understand for me.

What is the advantage to use Arduino code or port manipulation?

While the port manipulation routines happen about 20x~40x faster than the digitalWrite() routines, it's ridiculous to use such low-level nonsense before a 100ms delay. As you just demonstrated, it doesn't make the code more readable or maintainable.

ok :wink:

thank you very much for explaining

What is the advantage to use Arduino code or port manipulation?

I suspect that the original author was not aware that the "analog pins" of the arduino could be manipulated with pinMode() and digitalWrite() (using the n+14 as pin number), and so dropped into the direct mode access that they knew would work...