Go Down

Topic: How to pass parameters to timer interrupt callback methods (Read 1 time) previous topic - next topic

tommacpherson

When using a library like Timer1 to call a method after a certain amount of time, how do you give that callback method parameters?

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

WizenedEE


When using a library like Timer1 to call a method after a certain amount of time, how do you give that callback method parameters?

You cannot with that library.

However, I think it is possible to figure out what triggered the interrupt with "flag registers," such as TIFR0. Check out the atmega328 datasheet for more information.

Frisky

Why not use Timer 1 directly? Set up the registers and add a interrupt handler.
It's pretty easy to do are you looking to use output compare ? what kind of time delay do you need?
Don

tommacpherson

Code: [Select]
code]
Timer1.initialize(1500000);
Timer1.attachInterrupt(callBackMethod);

...


void callBackMethod(int someInt, int someOtherInt)
{
  ...
}



I want to do something like this, but I don't know how to pass the arguments to the callback method. The Timer1 will be assigned new interrupts/callback methods/arguments periodically.

PeterH

I think your only option would be to put the parameter values in volatile global variables. You can't pass them in as arguments as you're trying to do, since the interrupt mechanism doesn't support that.

If you wanted to hide the mechanics of that and didn't mind the overhead, you could write an interrupt handler which called some other function with the global variables as arguments.
I only provide help via the forum - please do not contact me for private consultancy.

tommacpherson

ok thanks. can you been using Timer1 for two interrupts (counts) at the same time? do Timer2 and 3 do the same thing as Timer1?

Go Up