I understand there are 3 timers on the Arduino UNO. What specific registers do I need to use to access them?
My goal is to use these timers to poll different sensors I am using with a ms period. Are there any pre-made libraries out there to do this?
-
Have you had a chance to look at the datasheet yet?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年12月28日 00:50:14 +00:00Commented Dec 28, 2014 at 0:50
-
The datasheet for the ATMEL SOC, or the Arduino breakout board itself?Sean– Sean2014年12月28日 00:57:47 +00:00Commented Dec 28, 2014 at 0:57
-
For the MCU (not SoC).Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年12月28日 00:58:36 +00:00Commented Dec 28, 2014 at 0:58
2 Answers 2
For the UNO, use Timer1.
An example of the way you can use it is as follows:
Download the TimerOne library: https://code.google.com/p/arduino-timerone/downloads/list
/*
* Timer1 library example
*/
#include "TimerOne.h"
void setup()
{
pinMode(10, OUTPUT);
Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period
Timer1.pwm(9, 512); // setup pwm on pin 9, 50% duty cycle
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
}
void callback()
{
digitalWrite(10, digitalRead(10) ^ 1);
}
void loop()
{
// your program here...
}
Timer 0 is use'd by the arduino core libraries for functions like millis(). It is best left alone. Leaving timer1 and 3 are available for general use. Both 1 and 3 have well established libraries, for simple implementation, without the need to know the mechanics under the hood, about the registers and such.
You can find links and general explanations at timer1. I always recommend the github versions.
And if you are still wanting to know the details the source is quite readible.
Note you don't need 3 timers to read 3 sensors. One timer on a multiple of the the 3, should suffice which can then implement a statemachine to sample at the correct periods.
-
1I think you mean timer2, not timer3.Gerben– Gerben2014年12月28日 18:37:24 +00:00Commented Dec 28, 2014 at 18:37