Blinky with TivaWare

In this tutorial i will show how to blink a LED with the TM4C123 launchpad. The LED you will be using it's the red LED in PF1 from the RGB LED present on the launchpad. This example was made using IAR Workbech free version with 32Kb code limit.Well let's get's started:

First we have to of course setup the system clock:

This will set it up to 80Mhz.

SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

Then we need to enable the clk in the peripheral

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); SysCtlDelay(3);

Normally the clock is disabled in any peripheral to save power. Leave a delay after it. I'ts advised to wait at least 3 clock cycles before configuring the peripheral you just enabled.

  • SysCtlPeripheralEnable(uint32_t ui32Peripheral) is what you always use to turn on a peripheral. It's what enables the clock for it. The parameter is the peripheral you want to enable. For GPIOs it's SYSCTL_PERIPH_GPIOx, being "x" the letter of the peripheral you want to enable.

  • SysCtlDelay(uint32_t ui32Count) is a delay. It stops the code for a certain number of clock cycles. Each value delays for 3 clock cycles so in our case with the value 3, we delay 9 clock cycles. How do you know what time that is? Well at 80Mhz each clock cycle is 12,5nS, that times 9, makes 112,5nS of delay. You can use this for rude delays, it's not very precise but it works for stuff like this. I found out it's not needed for all peripherals, consult Note - Common errors

Setting the pins to outputs

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);

This part is simple, you use GPIOPinTypeGPIOOutput(uint32_t ui32Port,uint8_t ui8Pins) to set it to a output. ui32Port is the GPIO base, in this case GPIO_PORTF_BASE, and ui8Pins is the pin you want to be output, in this case GPIO_PIN_1. In the launchpad this is the red LED, PF1.

Let's make it blink then

Now to turn on and off the LED you use GPIOPinWrite(uint32_t ui32Port,uint8_t ui8Pins,uint8_t ui8Val). The first and second parameter you alredy know what they are, the third it's to chose the state of the pin. You use 0 to turn it off and to turn the GPIO on, to 1, you need to use the value GPIO_PIN_x that you used in ui8Pins, like so for PF1:

/* This code was made using TivaWare 2.1.0.12573. TivaWare rights to it are all own byTexas Instruments This code shows how to blink the RED LED of the TM4C123 Launchpad using Tivaware. Luís Afonso*/#include <stdint.h>#include <stdbool.h>#include "inc/hw_types.h"#include "inc/hw_gpio.h"#include "driverlib/pin_map.h"#include "driverlib/sysctl.c"#include "driverlib/sysctl.h"#include "driverlib/gpio.c"#include "driverlib/gpio.h"/* This are to help if you want to change the pin you want to use so you don't have to change it in the entire code*/#define LED_PERIPH SYSCTL_PERIPH_GPIOF#define LED_BASE GPIO_PORTF_BASE#define RED_LED GPIO_PIN_1 int main(){ //Set the system clock to 80Mhz SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); /* Enables the clock on the GPIO, or "turns on" Also, delays for a bit since it's advised in the datasheet, i add problems when i didn't have that delay. */ SysCtlPeripheralEnable(LED_PERIPH); SysCtlDelay(3); //Set the pin of your choise to output GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); /* Here it blinks the LED with a 0.5 seconds interval. To set a GPIO to the state HIGH you need in the third parameter to be the name of the pin, ex: GPIO_PIN_1. To set to LOW just write 0. SysCtlDelay math: 1/80Mhz = 12.6nS, 12,5*3= 37,5nS. 37,5*13333333=0.5 seconds */ while(1){ GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1, GPIO_PIN_1); SysCtlDelay(13333333); GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1, 0); SysCtlDelay(13333333); } }