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.

MSP432P401R: msp432 Port Interrupt Problem

Part Number: MSP432P401R


Hello everyone,

I'm having trouble using port interrupt for the Buttons in P1.1 and P1.4. It says I am not using Port 1at all. I just can see the red  and blue led turned on. With the code I want to use both buttons, each of them controls one led and each led blink at different speed. Each button changes the behavior of the led it controls. Also I am not activating the timers at all. Any idea where can be the problem ?

Below is my code. Thank you in advance,

Alda

/* DriverLib Includes */
#include "driverlib.h"

/* Standard Includes */
#include <stdint.h>

#include <stdbool.h>

/* Application Defines  */
#define TIMER_PERIOD    0x2DC6

/* Timer_A0 UpMode Configuration Parameter */
Timer_A_UpModeConfig upConfig0 = {
TIMER_A_CLOCKSOURCE_SMCLK,                   // SMCLK Clock Source
        TIMER_A_CLOCKSOURCE_DIVIDER_64,      // SMCLK/1 = 3MHz
        TIMER_PERIOD,                        // 5000 tick period
        TIMER_A_TAIE_INTERRUPT_DISABLE,      // Disable Timer interrupt
        TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE,  // Enable CCR0 interrupt
        TIMER_A_DO_CLEAR                     // Clear value
        };

/* Timer_A1 UpMode Configuration Parameter */
Timer_A_UpModeConfig upConfig1 = {
TIMER_A_CLOCKSOURCE_SMCLK,                   // SMCLK Clock Source
        TIMER_A_CLOCKSOURCE_DIVIDER_64,      // SMCLK/1 = 3MHz
        TIMER_PERIOD,                        // 5000 tick period
        TIMER_A_TAIE_INTERRUPT_DISABLE,      // Disable Timer interrupt
        TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE,  // Enable CCR0 interrupt
        TIMER_A_DO_CLEAR                     // Clear value
        };

volatile bool buttonPressed, buttonPressed1, buttonPressed2;
volatile int sts, highSpeed;
volatile bool isLedOn1, isLedOn2;
float multiplier, multiplier1;
int period, period1, periodOn, periodOff, periodOn1, periodOff1;

int main(void)
{
    /* Halting the Watchdog */
    MAP_WDT_A_holdTimer();

    /* Configuring P1.0 as output and P1.1 (switch) as input */
    MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
    MAP_GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN2);
    MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
    MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN2);

    /* Configuring P1.1 as an input and enabling interrupts */
    MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN1);
    MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN4);
    MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN1);
    MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN4);
    MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN1);
    MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN4);
    MAP_Interrupt_enableInterrupt(INT_PORT1);

    /* Configuring Timer_A0 andTimer_A1 for Up Mode */
    MAP_Timer_A_configureUpMode(TIMER_A0_BASE, &upConfig0);
    MAP_Timer_A_configureUpMode(TIMER_A1_BASE, &upConfig1);

    /*Enabling the interrupts and starting the timer*/
    MAP_Interrupt_enableInterrupt(INT_TA1_0);
    MAP_Interrupt_enableInterrupt(INT_TA0_0);

    MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
    MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);

    /* Enabling MASTER interrupts */
    MAP_Interrupt_enableMaster();

    /* Enabling SRAM Bank Retention */
    MAP_SysCtl_enableSRAMBankRetention(SYSCTL_SRAM_BANK1);

    MAP_Interrupt_disableSleepOnIsrExit();

    sts = 0;
    buttonPressed = false;
    buttonPressed1 = false;
    buttonPressed2 = false;
    isLedOn1 = false;
    isLedOn2 = false;
    /* Going to LPM3 */
    while (1)
    {
        if (buttonPressed1 == true)
        {

            if (sts == 0)
            {
                multiplier = 0.01;
            }
            else if (sts == 1)
            {
                multiplier = 0.25;
            }

            else if (sts == 2)
            {
                multiplier = 0.5;
            }

            period = 0x2DC6;
            periodOn = period * multiplier;
            periodOff = period - periodOn;
            isLedOn1 = true;

            /* Configuring Timer_A1 for Up Mode */
            MAP_Timer_A_configureUpMode(TIMER_A1_BASE, &upConfig1);

            /* Enabling interrupts and starting the timer */
            MAP_Interrupt_enableInterrupt(INT_TA1_0);

            MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);

        }

        if (buttonPressed2 == true)
        {

            if (sts == 0)
            {
                multiplier1 = 0.02;
            }
            else if (sts == 1)
            {
                multiplier1 = 0.35;
            }

            else if (sts == 2)
            {
                multiplier1 = 0.45;
            }

            period1 = 0x54D6;
            periodOn1 = period1 * multiplier1;
            periodOff1 = period1 - periodOn1;

            isLedOn2 = true;
            /* Configuring Timer_A1 for Up Mode */
            MAP_Timer_A_configureUpMode(TIMER_A0_BASE, &upConfig0);

            /* Enabling interrupts and starting the timer */
            MAP_Interrupt_enableInterrupt(INT_TA0_0);

            MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);
        }

        while (buttonPressed == false || buttonPressed1 == false || buttonPressed2 == false)
        {
            MAP_PCM_gotoLPM0();
        }

        buttonPressed = false;
        buttonPressed1 = false;
        buttonPressed2 = false;
    }
}

/* GPIO ISR */
void PORT1_IRQHandler(void)
{
    uint32_t status;

    MAP_Timer_A_stopTimer(TIMER_A1_BASE);
    MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0);

    MAP_Timer_A_stopTimer(TIMER_A0_BASE);
    MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0);

    MAP_Interrupt_enableInterrupt(INT_TA1_0);
    MAP_Interrupt_enableInterrupt(INT_TA0_0);
    MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
    MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);

    status = GPIO_getEnabledInterruptStatus(GPIO_PORT_P1);
    MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, status);

    if (status & GPIO_PIN1)
    {
        buttonPressed1 = true;
    }

    if (status & GPIO_PIN4)
    {
        buttonPressed2 = true;
    }

    highSpeed++;
    if (highSpeed % 2 == 0)
    {
        sts = 0;
    }
    else if (highSpeed % 2 == 1)
    {
        sts = 1;
    }

    if (highSpeed % 3 == 2)
    {
        sts = 2;
    }
    buttonPressed = true;
    buttonPressed1 = true;
    buttonPressed2 = true;

}

void TA1_0_IRQHandler(void)
{
    MAP_Timer_A_stopTimer(TIMER_A1_BASE);
    MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);

    if (isLedOn1 == true)
    {
        upConfig1.timerPeriod = periodOff;
        MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
    }
    else
    {
        upConfig1.timerPeriod = periodOn;
        MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
    }
    isLedOn1 = !isLedOn1;

    /* Configuring Timer_A1 for Up Mode */
    MAP_Timer_A_configureUpMode(TIMER_A1_BASE, &upConfig1);

    /* Enabling interrupts and starting the timer */
    MAP_Interrupt_enableInterrupt(INT_TA1_0);

    MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
}

void TA0_0_IRQHandler(void)
{
    MAP_Timer_A_stopTimer(TIMER_A0_BASE);
    MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A0_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);

    if (isLedOn2 == true)
    {
        upConfig0.timerPeriod = periodOff1;
        MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN2);
    }
    else
    {
        upConfig0.timerPeriod = periodOn1;
        MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN2);
    }
    isLedOn2 = !isLedOn2;

    /* Configuring Timer_A1 for Up Mode */
    MAP_Timer_A_configureUpMode(TIMER_A0_BASE, &upConfig0);

    /* Enabling interrupts and starting the timer */
    MAP_Interrupt_enableInterrupt(INT_TA0_0);

    MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);
}

  • Some of the comments are wrong. This makes it extremely hard to reverse engineer the code; when you're changing code, either adjust the comments, or delete them.

    When inserting code into a post, use the "Insert Code" button ("</>") so that it is properly formatted.

    Without calling GPIO_interruptEdgeSelect(), it is undefined whether you get an interrupt on the rising or the falling edge.

    At the end of the main loop, you are clearing all buttonPressedX variables, so the checks at the beginning of the loop will not work.
  • Hi,
    Thanks for the reply and feedback. You are right for buttonPressed. Still my timers are not activated and therefore the pins can not be toggled. Maybe is the problem of GPIO_interruptEdgeSelect() that I am not using. I haven't solved the problem yet.
    Best,
    Alda
  • For everyone who need to realize something similar, the problem was in reorganizing better the lines of each function and fixing the state of buttonPressedX.

**Attention** This is a public forum