rbwilliams 2 Posted September 6, 2012 Share Posted September 6, 2012 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. Quote Link to post Share on other sites
energia 485 Posted September 6, 2012 Share Posted September 6, 2012 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 1 Quote Link to post Share on other sites
clay_shooter 2 Posted September 6, 2012 Share Posted September 6, 2012 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. Quote Link to post Share on other sites
GeekDoc 226 Posted September 7, 2012 Share Posted September 7, 2012 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). Quote Link to post Share on other sites
larsie 121 Posted September 7, 2012 Share Posted September 7, 2012 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. Quote Link to post Share on other sites
energia 485 Posted September 7, 2012 Share Posted September 7, 2012 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; } oscarl1718, larsie and rohit 3 Quote Link to post Share on other sites
rbwilliams 2 Posted September 7, 2012 Author Share Posted September 7, 2012 Thanks all for the suggestions! I agree with @larsie - is there a code example for interrupts in Energia? Quote Link to post Share on other sites
rbwilliams 2 Posted September 7, 2012 Author Share Posted September 7, 2012 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! Quote Link to post Share on other sites
rbwilliams 2 Posted September 7, 2012 Author Share Posted September 7, 2012 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. Quote Link to post Share on other sites
chamakov 2 Posted May 14, 2013 Share Posted May 14, 2013 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 =) Quote Link to post Share on other sites
energia 485 Posted May 14, 2013 Share Posted May 14, 2013 On the MSP430 you can use any GPIO. All GPIO's on the MSP430 have interrupt capability. Quote Link to post Share on other sites
RobG 1,892 Posted May 14, 2013 Share Posted May 14, 2013 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. Quote Link to post Share on other sites
sutekh137 2 Posted May 14, 2013 Share Posted May 14, 2013 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 Quote Link to post Share on other sites
canibalimao 2 Posted June 26, 2013 Share Posted June 26, 2013 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? Quote Link to post Share on other sites
RobG 1,892 Posted June 26, 2013 Share Posted June 26, 2013 Really? So how do I program something with 3 or 4 buttons?You test flags However, I think you misunderstood my post. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.