0

I'm programming in C on an MSP432 using Code Composer Studio v8.2.0.

Now I am writing an interrupt to use 4 different buttons to increment and decrement a variable. The code below cuts out two of those buttons and is simply trying to increment or decrement a variable (TEST) by a value of 2 whenever one of these two buttons are pressed.

I've written some interrupts for some rotary encoders that work perfectly. As far as I can tell I have used the same exact code (other than obvious changes to make it for buttons instead of encoders) but this isn't working.

Buttons are on P1.1 (edit: said 1.2) and P1.4. This code throws up no errors but the interrupt never gets triggered by either of the buttons and therefor, the variable doesn't change values at all.

I've lost track as to what I've tried at this point. Its been hounding me for about 5 hours now.

#include "msp.h"
#include "driverlib.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "grlib.h"
#include "button.h"
#include "imageButton.h"
#include "radioButton.h"
#include "checkbox.h"
#include <LcdDriver/kitronix320x240x16_ssd2119_spi.h>
#include "images/images.h"

int TEST = 0;

int main(void)
{
    WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;     // stop watchdog timer

        boardInit();
        clockInit();
        initializeOptionsMenuButtons();
        __enable_interrupt();

        //Navigation Testing IO
        __disable_irq();

        P1->SEL1 &= ~0x12;  // select io function: P1.2 & P1.4
        P1->SEL0 &= ~0x12;
        P1->DIR &= ~0x12;  // set inputs
        P1->REN |= 0x12;  // enable pull resistors for P1.2 & P1.4
        P1->OUT &= ~0x12;  // need to set P2.3-P2.6 to low so PULLDOWN resistor will be selected
        P1->IES &= ~0x12;  // select low to high transition for interrupt
        P1->IFG = 0;  // clear interrupt register
        P1->IE  |= 0x12;  // enable interrupt for  P1.2 & P1.4

        NVIC_SetPriority(PORT1_IRQn,3);
        NVIC_EnableIRQ(PORT1_IRQn);
        __enable_irq();

        for(;;){
        }
}

void PORT1_IRQHandler(void) {
    if( P1->IFG & 0x02 ) { // UP (2.3) triggers interrupt
        if ( 2 <= TEST <= 11 ) {
            TEST = TEST - 2;
        }
    }

    if( P1->IFG & 0x10 ) { // DOWN (2.4) triggers interrupt
        if ( TEST <= 9 ) {
            TEST = TEST + 2;
        }
    }
    P1->IFG &= ~0x12;
}

Depending on which button is pushed, the variable called TEST should increment or decrement by 2. As I said before, this does not happen because my interrupt that they are housed inside of does not trigger.

Any help is greatly appreciated. I'm out of ideas.

LightMan
  • 1
  • 1
  • Shouldn’t you need any compiler specific directives for PORT1_IRQHandler to tell it that it’s an interrupt handler? Right now it just looks like a regular function. – Fredrik Apr 11 '19 at 03:25
  • This ”if ( 2 <= TEST <= 11 ) {” is wrong, it doesn’t test what you think it does. – Fredrik Apr 11 '19 at 03:27
  • @Fredrik thank you for pointing out that problem with the if statement. I have fixed that. That still doesnt explain why the interrupt wouldnt trigger though. – LightMan Apr 11 '19 at 20:38
  • @Fredrik as far as the PORT1_IRQ handler goes, is that not what the NVIC_EnableIRQ(PORT1_IRQn) is doing? Saying that this function is an interrupt handler? – LightMan Apr 11 '19 at 20:40
  • Go back to your working code and look at the differences. Maybe try that code with your buttons - put breakpoints when the interrupt is triggered by the botton - prove that your interrupt enabling works, then move incrementally form there, testing at each small step. – DisappointedByUnaccountableMod Apr 11 '19 at 21:10
  • You don’t show the code which specifies that your interrupt handler function is installed as the interrupt handler for the interrupt - where is that done? – DisappointedByUnaccountableMod Apr 11 '19 at 21:13

2 Answers2

0

You are using the bitmask 0x12 over and over again, and it doesn't appear to be correct for the bits that your comments refer to.

You have a good debugger at your disposal. Step through your code and verify that you have the correct values for all of your registers.

Run the code, press the buttons, and then pause the debugger to see if you end up in a fault handler.

Elliot Alderson
  • 638
  • 4
  • 8
  • I wrote it wrong in my comment. Its 1.1 not 1.2. I have a breakpoint set at the inside of the interrupt and it never reaches the breakpoint. – LightMan Apr 11 '19 at 20:34
  • I run the prorgam, press a button, pause the debugger, and Im just stuck in the for loop in the main – LightMan Apr 11 '19 at 20:49
0

I solved the issue and I believe I know what happened. Ports now have to be declared in a completely different manner. I'm pretty sure I have an updated version of the SDK and that's where this change happened. Thanks everyone for your help.

    MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN1 | GPIO_PIN4);
    MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN1 | GPIO_PIN4);
    MAP_GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN1 | GPIO_PIN4);
    MAP_Interrupt_enableInterrupt(INT_PORT1);

It now looks like this.

LightMan
  • 1
  • 1