Jump to content

Interrupts using variables?

Recommended Posts

Hello All,


I am using LM4F120 Stellaris Launchpad.


Though this question is not related strictly to this only controller, I want to ask to solve my query which has been born while using this controller!


I am programming my logic and I need a strategy to active a software interrupt when the value of the particular variable is get exceeded by predefined threshold value.

Is it possible?? And when the value is get lower down to that threshold value the interrupt should be disable.


For ex. Variable name is flag

Threshold value Th = 200;

Whenever Flag is more than 200 the interrupt should be active (for ex a timer interrupt have a load value of 1 ms)

& whenever Flag gets lesser then 200 the interrupt should be disable.


Please guide me to develop such logic.


Thanks a lot for  your help.

Link to post
Share on other sites

Two obvious ways to do it:


1) Interrupt active all the time; service routine changes behavior depending on value of "flag"




void timer_int_handler(void)
  if flag <= Th
     do what needs to be done



2) Enable/disable the interrupt appropriately every time you update "flag"




static int flag;

updateflag(int newflag)
  flag = newflag;
   if flag > Th then



// Obviously could make this more efficient so it doesn't re-enable or re-disable interrupts, if that matters


This just an example - define update/access routine(s) to modify flag in whatever way makes sense - e.g. could add a variable or fixed value to flag, etc.

Encapsulating flag with the access routines helps to make sure that nothing can accidentally modify flag without appropriately adjusting interrupts.

Edited by bluehash
Please use code tags.
Link to post
Share on other sites

What you are asking doesn't sound like "standard" practice.  Can you give us a bigger picture of why you want this behavior?


Instead of us trying to help implement a single possible solution, if you first help us understand the bigger problem you want to solve chances are good that someone has seen this before and may have a different approach that you had not considered which is likely more elegant.


If you really want to implement this particular solution, i'd suggest using a general purpose timer.  Set it to interrupt on the threshold.  Do not enable the timer to count automatically.  Then use the timer value register to hold the variable "flag" that you want to test.





Link to post
Share on other sites

Thank you all... Thanks for your guidance. I'll implement all suggestions. Actually in code I want to make a very quick responsive method for the stability of the device using IMU (Inertial Measurement Unit) and my task is not to waste even a 1 micro second while gaining that stability... I need to create a strict lines for the variables which are irregular with respect to time... I think Class or Structures can be the option (using the public and private section properly)... Or if any other option which can make the device quick will be better to foll

Link to post
Share on other sites

Assume you have looked at other examples of IMU code (to see how others have solved whatever problem you are dealing with).


Couple thoughts -

if really concerned about time, then want to consider interrupt overhead ( 12 cycles ), 

if need really precise and repeatable timing, then again interrupts may not be your friend (not clear whether you need that or not)


If it happens that flag is a conversion of an analog sensor reading - then have you considered the comparator?


On some machines comparing against 0 (or determining sign + or -) is faster than comparing against an arbitrary value, so if it is tested a lot you can save an instruction or two if you can arrange for threshold to be 0 (e.g. start with flag = -Th, then test whether flag is positive, rather than comparing flag to Th ).  But that depends on how flag is generated.


However, "premature optimization is the root of all evil" -- Donald Knuth

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.

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