Pages: [1]   Go Down
Author Topic: Multiple libraries that use same Timer  (Read 830 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

My understanding of the innards of Timer interrupt handling and programming are somewhat limited, however, my impression is that interrupt handlers can be chained. Firstly, is this true ? And if so, can I expect to be able to have multiple libraries that use the same Timer (say Timer1) to coexist ?

For example, there's a Tone library that uses Timer1 and an ultra-sonic ranging sensor library, that too uses Timer1. By, default, I believe one cannot use both libraries in same Arduino sketch, right ? The idea is to merge the library code to be able to do so.

Any best practices ?

cheers,
f74
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 394
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you know enough about C++ and Atmel timers to merge the libraries successfully, I bow in your presence  smiley-wink

I faced the same problem.  You do not need a sonar library nor a timer assuming we are using the HC-SR04.  The trick is to realize that the start of the Utrasound is always the same.  Only the time of the echo return varies.

I used hardware Interrupt0 on pin 2.  The FALLING edge marks the return echo.  I mark the trigger time with  micros() and note the micros() of the interrupt on echo return. We subtract the values and then subtract the deadtime of 500-600 usec.  We are done! 

I have some sample code if you wish.

Joe
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5666
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For example, there's a Tone library that uses Timer1 and an ultra-sonic ranging sensor library, that too uses Timer1. By, default, I believe one cannot use both libraries in same Arduino sketch, right ? The idea is to merge the library code to be able to do so.

Those two activities are mutually incompatible.

If it was me I'd put one of them on timer2.

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Adding clarification, that this is not exactly on Arduino HW using 328p or the likes, but using attiny85, which has only 2 times (8-bit).

The Tone library is not the standard Arduino tone library, and well, my usecase, does require Tone and Range-finding to work together (range of obstruction determines tone being played). The libraries in their original form, are certainly mutually exclusive, but idea was to find a way to change that.

@joe, looks like you've been there and done that, but I'm afraid that I don't understand the explanation completely, especially the part about subtracting the "500-600us dead time" bit. Some code could certainly help, but elaboration would also be quite okay.

Thanks for the answers folks.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 394
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you trigger the sensor it takes 500usec before the sound is emitted.  Dead time or ping delay time.

I repeat, you do not need a timer for sonar.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 117
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are right @Joe - we do not need timer for sonar.

There's another library that needs timer though, it is the "klik aan klik uit" library for ISM-band RF. The decoder library needs to work directly with AVR timers.
« Last Edit: May 10, 2013, 10:01:22 pm by falcon74 » Logged

Anchorage, AK
Offline Offline
Edison Member
*
Karma: 42
Posts: 1176
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The major hurdle to clear here is whether the timer can be reprogrammed between tasks, or if the timer can be programmed once and its events used by both applications.

For example, if you're using the overflow interrupt to trigger a generic ISR, then you can probably use it for more than one purpose.  Say, for library 1, you need to do something on every overflow, whereas library 2 needs to act on every fifth overflow.  That could be accomplished by incrementing a counter, and triggering library 2 when it's mod 5.

If you're using CtC mode (clear timer on compare match) to fire at a variable frequency, which would be common for tone generators, then the window between events becomes less predictable for the "other" library.  But, if you never need to generate tones while the other library has access to the timer, you can multiplex their use.  You just have to be careful to re-initialize the timer settings every time either library needs to use it.

So yes, it's possible.  It would be typically disastrous to publish such a library, since guaranteeing the state of a non-exclusive timer is inherently troublesome.  But, if it's your own code, or a common use-case where a certain combination of libraries will always be used in tandem, it's absolutely doable.
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5666
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Adding clarification, that this is not exactly on Arduino HW using 328p or the likes, but using attiny85, which has only 2 times (8-bit).

Now you tell us...

The Tiny85 doesn't have a hardware event timer. If the range sensor is one of these (or similar) you might as well just disable interrupts and increment a 'long' until you get the return pulse. No hardware timer needed.

Tone will keep on working in the background if it's on a hardware timer.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Pages: [1]   Go Up
Jump to: