This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Connecting an external push button to tiva

Hi,

I am trying to connect a push button to GPIO PIN A2.After pressing the push button for four times it should blink the LED connected to PIN2 of portF.

In the code I have enabled interrupt on pin2 of PORTA,.

Following is my code..

/* Defines the base address of the memories and peripherals */
#include "inc/hw_memmap.h"

/* Defines the common types and macros */
#include "inc/hw_types.h"

/* Defines and Macros for GPIO API */
#include "driverlib/gpio.h"

/* Prototypes for the system control driver */
#include "driverlib/sysctl.h"

/* Prototypes for the NVIC Interrupt Controller Driver. */
#include "driverlib/interrupt.h"

/*  Macros that define the interrupt assignment on Stellaris. */
#include "inc/hw_ints.h"

/* Driver for the SysTick timer in NVIC. */
#include "driverlib/systick.h"

volatile unsigned long g_ulcount = 0;
unsigned long i;
void Pin_Int(void)
    {
    /*Clear interrupt register*/
    GPIOPinIntClear(GPIO_PORTA_BASE, GPIO_PIN_2);
    //Check for debouncing of switch
    for(i=0;i<=2000;i++)
        {
        }
    if(GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_2)==GPIO_PIN_2)
        {
        g_ulcount++;
        }

    /*Switch on LED at Pin2 of PORT F at every 5th time switch is pressed*/
    if(g_ulcount==5)
        {
        g_ulcount=0;
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
        SysCtlDelay(SysCtlClockGet() / 12);
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
        SysCtlDelay(SysCtlClockGet() / 12);
        }
    }

int main(void)
{
    /* Set the clocking to directly run from the crystal at 16MHz */
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    /* Set the clock for the GPIO Port F */

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    /* Set the type of the GPIO Pin of PORTF */
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2 );
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2);

    /* GPIO Pin2 on PORT F initialized to 0 */
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2 , 0 );

    /*Register Interrupt to call Interrupt Handler*/
    GPIOPortIntRegister(GPIO_PORTA_BASE, Pin_Int);

    /*Clear interrupt register*/
    GPIOPinIntClear(GPIO_PORTA_BASE, GPIO_PIN_2);

    /*Configure GPIO pad with internal pull-up enabled*/
    //GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);

    /*Set interrupt triggering sequence*/
    GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_2,GPIO_FALLING_EDGE);

    /*Enable interrupts on selected pin*/
    GPIOPinIntEnable(GPIO_PORTA_BASE, GPIO_PIN_2);

    /*Enable interrupts on selected port*/
    IntEnable(INT_GPIOA);

    /*Enable global interrupts*/
    IntMasterEnable();

    while(1)
    {

    }

}

  • Hello Mahavir,

    Not sure what the issue is but the Push Button on PA2 has to be pressed 6 times. Remember you are setting the g_ulcount to 0 and counting to 5 to blink the led.

    Secondly when you press the key you are checking for the pin to be 1 to increment the count. Based on the delay loop of 2000, it would be 2000*3/16MHz = 375 us for the user to release the button before sampling. I would suggest checking for the Pin to be 0 or better still put a while loop till pin reads 0 and then check for 1.

    Regards
    Amit