rbwilliams

Interrupts for a Newbie

22 posts in this topic

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.

Share this post


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.

tripwire likes this

Share this post


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.

Share this post


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).

Share this post


Link to post
Share on other sites

Sorry for hijacking the thread, but is there a quick example code snippet somewhere showing the use of interrupts for buttons in Energia? Is this done exactly the same way as Arduino? Basically I'd like an interrupt routine being called when I press a button.

Share this post


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;
}

larsie, rohit and oscarl1718 like this

Share this post


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!

Share this post


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.

Share this post


Link to post
Share on other sites

HI! somebody can tellme in which pin its the interrupt for this routine?

because in arduino the switch can be attached to digital pin 2 and 3,

in the msp430 launchpad which pin is?

 

Sorri for my bad english, i need to practice more =)

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


Link to post
Share on other sites

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

 

Really? So how do I program something with 3 or 4 buttons?

Share this post


Link to post
Share on other sites

Really? So how do I program something with 3 or 4 buttons?

You test flags

However, I think you misunderstood my post.

Share this post


Link to post
Share on other sites

Oh I'm so stupid... I misunderstood everithing. I thought that P1 and P2 were "P1.1 and P1.2" -_-

 

Thank you booth for the explanation :smile:

Share this post


Link to post
Share on other sites

Hi,

is it possible to wake up from LPM4 mode by SPI bus (to be precise - from CC110L radio)?

/just found that CC110L seems to have dedicated out pin for interrup ucontroller, so it should work throuhg GPIO interrupt/ 

Share this post


Link to post
Share on other sites

Hi,

is it possible to wake up from LPM4 mode by SPI bus (to be precise - from CC110L radio)?

/just found that CC110L seems to have dedicated out pin for interrup ucontroller, so it should work throuhg GPIO interrupt/ 

Essentially, yes, you can wake an SPI slave MSP430. That is, any real SPI slave will have a /CS input, which is driven from the master. This is in most cases a software controlled interrupt GPIO line, so all is fine there.

You cannot however wake an SPI master from the SPI slave. In those cases, the SPI slave will have a separate interrupt line going out to the master.

Share this post


Link to post
Share on other sites

I don't think the CC110l is itself capable of waking the host MCU, though, since it's a simple transceiver.

Share this post


Link to post
Share on other sites

Hi,

 

Just seeing this now .. the CC110L generates interrupts when it is receiving. That means your MCU can sleep or do other things. In the energia for the CC110 the interrupt (on GDO0) is set when you start the Radio. Unfortunately its not available to the user. In my code I have disabled the attachinterrupt in the original library to allow my own attachinterupt routine to take over and signal an incoming message.

 

regards

Cor

Share this post


Link to post
Share on other sites

The code that is avaliable on the link: 

Quote

file:///D:/Softwares/energia-0101E0012/reference/AttachInterrupt.html

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); // Interrupt is fired whenever button is pressed
}

void loop()
{
  digitalWrite(GREEN_LED, state); //LED starts ON
  if(flag) {
    count++;
    Serial.println(count);
    flag = LOW;
  }
}

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

 

In this code, I am trying to press the pushbutton P1_3. But it is not showing any status on Serial Monitor.

 

Please need a suggestion from Energia Forum..

Waiting for the response!!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now