GPIOs and LED Blinking (TivaWare)
The TM4C123GH6PM microcontroller has a lot of wonderful features built into its GPIOs. Some of these features are:
- Any GPIO can be an interrupt:
- Edge-triggered on rising, falling or both
- Level-sensitive on high or low values
- Can directly initiate an ADC sample sequence or μDMA transfer
- Toggle rate up to the CPU clock speed on the Advanced High-Performance Bus. 1/2 CPU clock speed on the Standard Bus.
- 5V tolerant in input configuration
- Programmable Drive Strength (2, 4, 8mA or 8mA with slew rate control)
- Programmable weak pull-up, pull-down, and open drain
- Pin state can be retained during Hibernation mode
In this post we would talk about how to blink an LED using the TM4C123GH6PM microcontroller on the LaunchPad using both TivaWare library as well as directly accessing the hardware registers on the controller.
Before we delve into the code there are some concepts about the LaunchPad that one needs to know as we would be using the LaunchPad's onboard LEDs. The LEDs are connected to GPIO portF's pins 1,2 and 3 as shown in the figure below:
The LED on your LaunchPad is not a single LED but a package of RGB LEDs with a common cathode configuration. As can be seen from the above figure:
PF1 = LED_R (Red)
PF2 = LED_B (Blue)
PF3 = LED_G (Green)
We would be blinking the LED_R here. The code for doing this using TivaWare library is given below. Also the compiler installation and configuration could be found in the very well constructed tutorial by TI for the LaunchPad as I had mentioned in the introduction page. You can also find it HERE.
#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)
{
// Set the system clock
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
// Enable the GPIO PortF
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Set the GPIO PortF Pin2 as output
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
while(1)
{
// Turn on the LED
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 2);
// Delay for a bit
SysCtlDelay(2000000);
// Turn off the LED
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
// Delay for a bit
SysCtlDelay(2000000);
}
}
The code is duly commented and can be easily understood. If doubts remain please refer to the TivaWare tutorial on the webpage mentioned above. If doubts persist please put them in the comments. I would be happy to help you.
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 2);
ReplyDeletethis part of the code is wrong because your are initiating pin 2 while writing dat on pin 1. use 4 instead of 2 as a data