Jump to content
tw1ns

How to using attachInterrupt()?

Recommended Posts

Energia or rather the MSP430 only supports FALLING and RISING, not CHANGE. I guess CHANGE could be emulated but will introduce some overhead in the ISR. It might be worth looking into this for the next release.

Share this post


Link to post
Share on other sites

You may try to use 2 pins with different modes of interrupts.

 - signal goes to the two pins at the same time;

 - interrupts in FALLING and RISING modes

    (FALLING for when the pin goes from high to low; RISING to trigger when the pin goes from low to high); 

 - attachInterrupt() for different pins points to the same function

attachInterrupt(5, func_interrupt_change, FALLING);
attachInterrupt(6, func_interrupt_change, RISING); 

if I correctly understood CHANGE mode in Arduino 

 

for buttons: http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_PushButton (Fig. 2:  Switch after debouncing circuit)

Share this post


Link to post
Share on other sites

I'm not an Energia user myself, but I guess what you could do is add some code similar to this to the end of your interrupt function:

if (readPin (5)==1)
  attachInterrupt(5, func_interrupt_change, FALLING);
else
  attachInterrupt(5, func_interrupt_change, RISING);

Share this post


Link to post
Share on other sites

While looking at this I discovered a serious bug in the interrupt code. Basically RISING would never work. Patch for this fix is pretty simple:

 

diff --git a/hardware/msp430/cores/msp430/Energia.h b/hardware/msp430/cores/msp430/Energia.h
index facfd06..7ce8034 100644
--- a/hardware/msp430/cores/msp430/Energia.h
+++ b/hardware/msp430/cores/msp430/Energia.h
@@ -22,9 +22,8 @@ extern "C"{
 #define LSBFIRST 0
 #define MSBFIRST 1
 
-#define CHANGE 1
-#define FALLING 2
-#define RISING 3
+#define RISING 0
+#define FALLING 1
 
 #define INPUT 0x0
 #define OUTPUT 0x1

 

 

And this is the Sketch that I used to test it. This Sketch also demonstrates how to get an interrupt on CHANGE just like in Arduino. It's only a couple more lines of code and a volatile var.

 

uint8_t volatile edge;

void setup() {                
  pinMode(RED_LED, OUTPUT);     
  digitalWrite(RED_LED, LOW);

  attachInterrupt(PUSH2, func, FALLING);
  edge = FALLING;  
}

void loop() {
}

void func() {
  if(edge == FALLING) {
    attachInterrupt(PUSH2, func, RISING); 
    edge = RISING;
  } else {
    attachInterrupt(PUSH2, func, FALLING);
    edge = FALLING;
  }
  
  P1OUT ^= 0x01;
  
}

 

 

Share this post


Link to post
Share on other sites

 

While looking at this I discovered a serious bug in the interrupt code. Basically RISING would never work. Patch for this fix is pretty simple:

diff --git a/hardware/msp430/cores/msp430/Energia.h b/hardware/msp430/cores/msp430/Energia.h
index facfd06..7ce8034 100644
--- a/hardware/msp430/cores/msp430/Energia.h
+++ b/hardware/msp430/cores/msp430/Energia.h
@@ -22,9 +22,8 @@ extern "C"{
 #define LSBFIRST 0
 #define MSBFIRST 1
 
-#define CHANGE 1
-#define FALLING 2
-#define RISING 3
+#define RISING 0
+#define FALLING 1
 
 #define INPUT 0x0
 #define OUTPUT 0x1

Where do I go to make this fix? (working on a mac). 

Thanks in advance,

Dan

Share this post


Link to post
Share on other sites

The MSP430 does not have a CHANGE interrupt. The fix is so that FALLING and RISING have the correct value.


You can apply this fix by doing the following: 


 


1: control click Energia and select "Show Package Content". This will open the Energia folder.


2: go to Contents/Resources/Java/hardware/msp430/cores


3: In that folder locate Energia.h and open it in a TextEdit


4: Replace:


#define CHANGE 1


#define FALLING 2


#define RISING 3


with


#define FALLING 1


#define RISING 2


5: Save and close TextEdit


 


The trick now is to emulate the CHANGE interrupt. The example Sketch I posted above shows how to do this. The Sketch demonstrates that if you push button 2 the LED goes on and when you release it it goes off.


 


The idea is that when you enter the interrupt handler in this case func() you flip the edge of the interrupt. So if it was triggering on FALLING you reattach the interrupt on the RISING edge and vise versa.


Make sure that the variable that keeps track of the edge is of type volatile in this case volatile uint8_t edge.


 


Robert


Share this post


Link to post
Share on other sites

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