Tivaware

LM4f120: Using “buttons.h” for inbuilt buttons.

Last few days I spent a lot of time with the LM4F120 board just to get familiarized with the Stellarisware APIs. There are some documents available with the software which gives a good picture on theĀ behaviorĀ of those APIs. Since TI discontinued Stellarisware you can find these documents under Tivaware section in TI.com.

Following is the way to implement ‘button.h’ in your code. Here, I will be using inbuilt buttons to turn on and off the LEDs in the circuit.

Code:

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "drivers/buttons.h"

int main(void) {

unsigned char button_val;
unsigned char Delta;
unsigned char Rawstate;

SysCtlClockSet(SYSCTL_SYSDIV_5| SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE , GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);

ButtonsInit();

while(1){
SysCtlDelay(100000);

button_val = ButtonsPoll(&Delta, &Rawstate);

if(BUTTON_PRESSED(LEFT_BUTTON,button_val,Delta)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0xFF);
}

if(BUTTON_PRESSED(RIGHT_BUTTON,button_val,Delta)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x00);
}
}

}

But unfortunately this code alone will not do the trick. You should strictly adhere to following steps in order to build this code. Otherwise you will receive “Unresolved variable …” errors.
(more…)