Accessing AVR GPIOs via bit fields :: 08 Dec 2009


I noticed that the TMS320F2809 examples accessed GPIOs by bits rather than as 32-bit ints. For example, it’s possible to set a GPIO like this on the TMS320F280x:

GpioDataRegs.GPADAT.bit.GPIO12 = 1; // Sets GPIO12 to high.

Normally when working with AVRs, I would set bits like this:

PORTB |= (1<<PB3); // Sets port b, bit 3 to high.

After a bit of tinkering, I came up with this (for AVRs):

portb.bit.b3 = HIGH; // Sets port b, bit 3 to high.

I personally think it looks a bit cleaner, so I’ll probably be using it in the future. If you’re interested in trying it out, you can download a full example here:

Side note: It is also possible to access GPIOs as 32-bit ints on the TMS320F280x like this:

GpioDataRegs.GPADAT.all |= (1<<12); // Sets GPIO12 to high.

Edit: Looks like I’m not alone.