Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Library writers >> Timer usage in libraries.
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1158344443

Message started by Alan_Kilian on 09/15/06 at 18:20:43

Title: Timer usage in libraries.
Post by Alan_Kilian on 09/15/06 at 18:20:43
Have you guys figured out how we should allocate timers in libraries?

I notice the Servo library uses timer 3 and I'd like to use one for a
library, but I certainly don;t want to conflict with the Servo library.

For now, I'll just use timer 3, but we should try and figure out a set
of routines so that each library can allocate a timer and just get which
ever one is free and use that one instead of hard-coding which timer
is used by a library.

I'll think about it and propose something if it isn't already in the works.

??? Which timers are free to be used by libraries by the way ???

Title: Re: Timer usage in libraries.
Post by zambetti on 09/24/06 at 19:25:46
Very good point. It gets a bit compilcated since not all timers have the same capabilities and each timer has a prescale value that is applied to every channel for that timer. So changing a prescale value could interfere with libraries using the other channels.

Perhaps there could even be a way to list a number of functions (callbacks) to be called for a single timer (if they are not very time dependent).

Looking at WTimer.h, Timer 3 is also used for PWM pins 0, 1, and 2 (channels A, B, and C respectively). I see no problem in using timer 3 for your library, but if you are planning to distribute it, it would be a good idea to put a comment in a visible place that states that it cannot be used in the same sketch as the servo library.

Another timer to be careful with is Timer0 ... looking at WApplet.c, it is used for millis() and delay(). Overriding that one would be a bad idea.

Here is a rough outline of timer usage:

Timer 0:
   Millisecond counting: millis() (core/WApplet.c)
   delay() (core/WApplet.c)

Timer 1 channel A:
   Analog out (PWM) pin 5 (core/WTimer.c)
   Pulse channel 0 (hidden) (core/WPulse.c)

Timer 1 channel B:
   Analog out (PWM) pin 4 (core/WTimer.c)
   Pulse channel 1 (hidden) (core/WPulse.c)

Timer 1 channel C:
   Analog out (PWM) pin 3 (core/WTimer.c)
   Pulse channel 2 (hidden) (core/WPulse.c)

Timer 3 channel A:
   Analog out (PWM) pin 2 (core/WTimer.c)
   Servo Library, Timer3 prescaled to 256 (Servo/Servo.cpp)

Timer 3 channel B:
   Analog out (PWM) pin 1 (core/WTimer.c)

Timer 3 channel C:
   Analog out (PWM) pin 0 (core/WTimer.c)

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.