The GPIO functions in Tivaware take a little fiddling with to understand. For beginners, it may not be immediately obvious what’s going on in the output function, especially as there are multiple ways to do the same thing. Let’s take a look at an example of how to use the pins, and at the end we’ll set them up (that’s the boring part).

GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1, GPIO_PIN_1);	// turn on one LED

Tivaware includes the GPIOPinWrite function. As arguments, it takes the base port, the pin(s) and the value to write. So to write to PF1, we need to tell it that we’re in port F, we want pin 1, and then the value. The ‘value’ isn’t a 1 or a zero, however. It needs to be a 1 or a 0 in the position of the pin we want to control, that’s the key. GPIOPIN1 is simply a variable equal to the position of pin 1 in registers, which is actually equal to 2 in decimal notation. You can see the number that these macros are referencing by hovering your mouse over them in Code Composer Studio. Pin 1 = 2, Pin 2 = 4, Pin 3 = 8, and so on.

So we are telling the microcontroller to place a 1 in the 2’s place, which corresponds to pin 1 on port F. How confusing is that?! It’s not too bad, but it can take a little while to wrap your head around if you’re coming straight from Arduino. If you’ve dealt with 8-bit microcontroller programming, this won’t be too bad, since there are only 8 pins on a port here too.

Multiple pins at once

GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 4);	// turn on one LED

To set multiple pins at the same time, we need to do two things. As before, we’re specifying that we’re on port F. First, we’ll OR the pins we need together. The pipe character ”|” means OR. So the micro is going to be able to work with any pins we specify here in a string of OR statements. In that case, it’s pins 1, 2, and 3. It’s not a big deal if we put a pin in here that we don’t end up setting.

Next up, we set the value. Remember how each GPIOPINX is actually a number? Since we’ve placed a 4 here, that corresponds to pin F2. Since pin F2 was specified in our ORing earlier, F4 will go high. Instead of placing a 4, we could’ve also put GPIOPIN2, like in the previous example, which is equal to 4.

So how do we set multiple pins? First, they need to appear in your OR statement. Then, we set the port to equal the addresses of those pins, but added together. Since pin 1 = 2, pin 2 = 4, and pin 3 = 8, we could set the port to 6, 10, 12, or 14. These are all the combinations of adding 2, 4, and 8 to each other. This is how the GPIO bus works, there’s a unique number for any combination of pins being on. Tivaware handles this automatically, so we can just change the pins we want, and the others will keep their states. Awesome!

This numbering system could get confusing since we won’t always remember which number corresponds to each pin. Luckily, we can use the OR statement again. In an application like this, OR is actually serving as addition. If we OR 00000010 with 00000100 (2 and 4 in binary), we get 00000110. See, from our two inputs, we’ve set any bit that input 1 OR input 2 have set. To do this with Tivaware, we can set the last argument of our function as GPIOPIN2|GPIOPIN3, for example. This will turn on pins 1 and 3. We could’ve even set it to 4|8, which is kind of weird, but is what the GPIOPINX variables represent. Here’s an example:

GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_2|GPIO_PIN_3);	// turn on two LEDs

Turning pins off

To turn a pin off, simply put its address in the second argument of GPIOPinWrite and write a zero as the third argument. Actually, anything except the pin’s address will make turn it off. There is an example of this in the complete demo code at the end, where we turn off pin 1 only.

Setting up outputs

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);	// enable port F

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);	// enable outputs on the launchpad LED pins

To set pins as outputs, we just need to enable the port and set some pins as outputs. Like we’ve gone through, we OR together the specific pins we’d like to mess with. There are also includes above this. They’re in the complete demo code below. This program will blink a few of the LEDs on the launchpad. It’s a good example of how to set single and multiple pins, and turn specific pins off.

/*
GPIO Output Demo
*/

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
int main(void)
{
  SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);	// set up the clock
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);	// enable port F
  GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);	// enable outputs on the launchpad LED pins

  while(1)
  {

      GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1, GPIO_PIN_1);	// turn on one LED
      SysCtlDelay(7000000); // wait a while
      GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_1|GPIO_PIN_2);	// turn on two LEDs
      SysCtlDelay(7000000);
      GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1, 0x00);	// turn pin 1 off
      SysCtlDelay(7000000);
  }
}