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.

Problem using interrupt with SW1 on Tiva Launchpad

Hello,

I'm using Tiva Launchpad EK-TMC123GXL with Code Composer Studio v.6.0.0.00190. My intent is to trigger interrupts with the onboard buttons SW1 & SW2. By pressing SW1 I want the "blue" LED to turn on, and with SW2 the "red" LED to turn on. I have managed to turn on the "red" LED by pressing SW2, but for some reason the same does not happen for SW1. I read the following posts:

http://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/p/332605/1159528.aspx#1159528

http://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/336194.aspx

... but could not identify anything that would help me solve my problem.

My code is as follows:

1 - First I configure the system clock.

    SysCtlClockSet (SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

2 - Then I configure the GPIO port and pins.

    // Enable Peripheral Clocks
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    // Enable pin PF0/4 for GPIOInput
    //First open the lock and select the bits we want to modify in the GPIO commit register.
    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1;

    //Now modify the configuration of the pins that we unlocked.
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4);

    // Enable pin PF1/2/3 for GPIOOutput
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);

    // Turn weak pull-ups on
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4 , GPIO_STRENGTH_2MA,    GPIO_PIN_TYPE_STD_WPU);

3 - I set up the interrupts and the start an infinate loop.
    IntMasterEnable();
    IntEnable(INT_GPIOF);

    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_INT_PIN_0 | GPIO_INT_PIN_4);

    GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_BOTH_EDGES);
    GPIOIntEnable(GPIO_PORTF_BASE, GPIO_INT_PIN_0 | GPIO_INT_PIN_4);

    while(1)
    {    }

4 - My ISR is the following.

void buttonIsr()
{
    if (!GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0))
    {
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0x02);
    }

    if (!GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4))
    {
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x04);
    }

    SysCtlDelay(1000000);

    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2, 0x00);

    GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4);
}

5 - I have added the ISR in the vector table in the "startup" code.

As I have already stated, the above code will result in flashing the "red" LED for a small amount of time (defined in "SysCtlDelay(1000000);") when I press SW2. However the same does not happen for SW1. In addition, when I put my finger on the PF4 pin on the J4 connector, the blue LED will flash. Finally, when I debug the whole thing in the "Memory Browser" I see the following:

PF0 in address 0x40025004 has a value 1 and if I press SW2 it goes to 0, which is what I would expect. However, PF4 in address 0x40025040 is always 0. It will switch to 1 only if I put my finger on pin PF4 of the J4 connector.

What am I missing here? Thank you in advance.

  • Konstantinos Giotopoulos said:
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_INT_PIN_0 | GPIO_INT_PIN_4);

    Mon ami - nicely detailed post - good job.

    Look closely @ the item quoted.  (above)  Did you really intend that?

    Far earlier in your code you:

        //Now modify the configuration of the pins that we unlocked.
        GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4);

    Thus - you've effectively "undone" the good work which your earlier appearing code had performed.

    Your intent was an interrupt set-up/config - which I believe you handle nicely in the 2 lines just below this errant code.  (although I'm no fan of "both edges" triggering your interrupt - if the switch closes to ground - use negative edge)

    Your "unlock" of the (always) dreaded PF0 is bit abbreviated.  More normally a 3rd function call appears: (to terminate that unlock)                          

    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0;

    Use of a, "body part" as test probe (while common, unfortunately) is not best practice - may subject your MCU/other components to ESD!  A DMM read of PF0 & PF4 should reveal the weak pull-up placing each pin at 3V3 when the switch is inactive.  (again - assumes switch closes to ground - reverse if switch closes to 3V3)

    You're almost there.  These MCUs are complex - demand great attention to detail.

  • Oh my God this was it, it finally works. Thank you very much, it took me so many hours trying to find the cause and it was right in front of me.


    Many thanks.

  • Konstantinos Giotopoulos said:
    it took me so many hours

    My friend - thank you - and know that I've stared @ code for days - only to have a colleague "instantly" note my screw-up!

    Added eyes/minds - motivation - so much becomes possible.

    Bon chance...