Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpLibrary writers › Timer usage in libraries.

Page Index Toggle Pages: 1
Timer usage in libraries. (Read 5315 times)
09/15/06 at 18:20:43

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
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 ???
IP Logged
Reply #1 - 09/24/06 at 19:25:46

zambetti   Offline
YaBB Newbies

Posts: 21
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)
IP Logged
Page Index Toggle Pages: 1