Turning LEDS (and pins) On and Off

Can someone explain why in the SDK, the data direction registers are changed to turn the LEDs on and off, instead of leaving it as an output and changing the values in the port registers?

From board.h:

#define LED_RED(v)          {((v) ? (P2DIR |= 0x02) : (P2DIR &= ~0x02));}

I need to turn a transistor on and off, so I’m just setting the pin in output mode, then writing a 1 or 0 to a pin in the port register. It seems to work, so what are the advantages/disadvantages of the two methods? Am I leaving the transistor’s base-emitter in an undefined, floating state by not changing the data direction?

Thanks.

–Louie

Hello, Louie.

One of the steps for the user to force the Wixel in to bootloader mode is to set pin P2_2 (the yellow LED line) high. We definitely would not want to drive that line low because then that would cause a short circuit. So we chose to drive the line high when we want to turn the LED on and weakly pull the line low when we want to turn the LED off. To switch between those two states, all we need to do is change the direction bit, because the output value bit has already been set to 1.

The red LED line is pretty close to the yellow LED line and there is reason to drive it low, so we treat it the same as the yellow LED line.

What you’re doing with the transistor sounds fine. If you have more doubts about it, I recommend that you check out the GPIO library in the Wixel SDK.

GPIO API documentation: pololu.github.com/wixel-sdk/gpio_8h.html
GPIO library source: pololu.github.com/wixel-sdk/gpio_8c_source.html

–David