Jump to content
43oh

Interrupts for a Newbie


Recommended Posts

Hello all,

Maybe this has been addressed already, but I could not find the answer.

I am new to Energia, and relatively new to Arduino. I understand that in the Arduino IDE , one cannot use a delay function inside an interrupt because interrupts and the delay function use the same timer in the Atmel chips. So, is this true for the MSP430 as well? Is there a clever library that uses another MSP430 timer for delays inside interrupts?

I would really like to be able to debounce a switch inside an interrupt routine!

Thanks.

Link to post
Share on other sites

You can not use delay inside the interrupt function because the interrupt function is executed in the interrupt context. This means that no other interrupts will be handled until your interrupt function exits. Hence executing something that depends on interrupts occurring, e.g. delay(), will hang the MSP430.

 

If you need to debounce then I suggest using delayMicroseconds(). This function just does a bunch of NOPS.

Link to post
Share on other sites

You're not going to want to pause/delay inside an interrupt handler. They really need to process and exit as quickly as possible to re-enable other interrupts.

 

For button debounce (I/O interrupt), you could accept the button and do the work on the first interrupt and then record millis() in a variable. Then any interrupt after that checks the recorded millis() to see if enough time has passed for this interrupt to be a new button press. Otherwise you would ignore the state if enough time hasn't passed. You can do the same thing if you are polling the button in a timer interrupt.

 

You could set a flag in the interrupt handler and then have the main loop process after some time has passed possibly even using a delay() in the main loop.

 

Others probably have better ideas but you really want your interrupt handlers to be as tight and quick as possible so that other work can be done. Delays in interrupts can freeze out the watchdog timers causing some systems to restart.

Link to post
Share on other sites

I'm not using Energia, but got caught in the same situation on my Elvis display controller. Setting a flag in the interrupt to trigger in the main loop (polling the flag) worked out great for me. I'm not using power saving modes though, so you might need to consider that if power usage is a concern (I'm powering from mains supply).

Link to post
Share on other sites

Same as in Arduino.

 

volatile int state = HIGH;
volatile int flag = HIGH;
int count = 0;

void setup()
{
 Serial.begin(9600);

 pinMode(GREEN_LED, OUTPUT);
 digitalWrite(GREEN_LED, state);

 attachInterrupt(PUSH2, blink, FALLING);
}

void loop()
{
 digitalWrite(pin, state);

 if(flag) {
   count++;
   Serial.println(count);
   flag = LOW;
 }

}

void blink()
{
 state = !state;
 flag = HIGH;
}

Link to post
Share on other sites
You can not use delay inside the interrupt function because the interrupt function is executed in the interrupt context. This means that no other interrupts will be handled until your interrupt function exits. Hence executing something that depends on interrupts occurring, e.g. delay(), will hang the MSP430.

 

If you need to debounce then I suggest using delayMicroseconds(). This function just does a bunch of NOPS.

 

I kind of understand this. But, I am ok with nothing else happening until the interrupt exists. You can do this in PBASIC - Button triggers interrupt. You can loop the interrupt routine, even with a delay, until button is released. Interrupt exits. Maybe for some this is not what they want, but for me, that is EXACTLY what I want to happen.

 

BTW,

Thanks for the code, energia!

Link to post
Share on other sites

I guess I spoke too soon! I checked back in my old PICAXE code experiments. I did, at one point, put a delay inside the interrupt. But, later on, I put delays in a separate delay subroutine (function)- based on a flag I set in the interrupt subroutine(function)!

I think its the C++ syntax that is making me think sideways. I am still getting used to it!

Thanks again everyone for your kind patience and constructive suggestions.

Link to post
Share on other sites
  • 8 months later...

To be more precise, only P1 and P2 (on any MSP430) have interrupt capability.

 

Also, you can nest interrupts if you have something time consuming in one of your ISRs.

Enabling nested interrupts is as easy as setting GIE flag inside ISR.

Link to post
Share on other sites

To be more precise, only P1 and P2 (on any MSP430) have interrupt capability.

 

 

/me imagines putting a FALLING interrupt on GND and a RISING interrupt on Vcc...or would that be the other way around? Hm, I guess it wouldn't ever trigger, anyway, would it?

 

I know all the P<n> pins work for invoking ISRs because I use them all on my 13-key tone keyboard (plus 2 octave changers and audio out) to come back from LPM4 when the keyboard has gone idle. I attach and detach interrupts like a crazy person!

 

Thanks,

sutekh137

Link to post
Share on other sites
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...