Jump to content
43oh

Using interrupts


Recommended Posts

The documentation for the attachInterrupt function states:

 

 

Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost.

 

What about micros(), does it work?

 

I can understand these limitations for the lower end MSP430s but what about the 5529, Tiva-C and C2000?  Do they have the same limitations?  Any chance the interrupt handling could be enhanced on the higher end MCUs?

 

 

Also, any chance at timer interrupts in Energia or will that always need to be done something like this http://forum.stellarisiti.com/topic/1754-interrupt-handler-in-energia/

 

Thanks,

Link to post
Share on other sites

micros()/millis() do work, because you're just getting some data. But be sure not to "block" the flow during an interrupt. In particular, this code does not work and in addition is very bad design

void functionToBeAttached()
{
  int start = micros();
  // some code
  while(micros() - start < 20); // wait 20 microseconds
  // some other code
}

micros()/millis() do give you information, but they do not get updated while you're in your function. This kind of coding can (though will not always when using micros()) thus result in an infinite loop.

 

If you do need to wait for a set time, instead use the flag polling strategy:

bool buttonPressed = false;

void loop()
{
  if (buttonPressed)
  {
    // some code
    delay(20);
    buttonPressed = false;
    // some other code
  }
}

void funcionToBeAttached()
{
  buttonPressed = true;
}

This behaviour is resulting from the way Energia keeps track of millis(), which is also on an interrupt. All of the named controllers have just one CPU, and thus can handle only one interrupt simultaneously.

Link to post
Share on other sites

It is important to remember that interrupt routines should be kept as simple (and short) as possible. When they are being executed no other code is running and (typically) interrupts are disabled. If you're blocking on one interrupt (say a push button) you may lose data coming in over UART!

 

Note: The handler is simply a callback function that is called from the interrupt routine defined at the interrupt vector.

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