hi,I 'm new with AVRs & Arduino
I want to use the timer to count risning edges from an encoder (2 actually)
I looked for it in the datasheet,and I got lost ;D
can anyone summarize timer features for me? (not all of them of course ;))
and wich pins are used for it? (arduino Uno)
thanks in advance
I want to use the timer to count risning edges from an encoder (2 actually)
This doesn't make sense. How do you use a timer to count pulses if the pulses are not based on the timer?
The usual way to count pulses is to use an interrupt, generated by the encoder, on a RISING edge, and have an interrupt service routine increment a counter.
Then, periodically, in loop, use millis() to determine how long the pulses have been happening, to determine speed, and then reset the counter.
it makes sense.
You use timers to count pulses by using the pulse source as a clock for the timers. each pulse == one count.
Timer 2 can be clocked asynchronously by an external source. The unfortunate part is that these pins are already in use on the uno (for the 16mhz oscillator) and NOT CONNECTED on the MEGA (my second biggest pet peeve with the arduino).
Pins T0 and T1 ( the uno's digital pins 4 and 5 respectively) can /synchronously/ clock timers 0 and 1 respectively. so as long as you are getting pulses slower than say 1/4 Tio (=4MHz ) that would work well for you. The arduino API does use timer 0 for delay and millis so you probably want to use timer 1. using timer 1 would conflict with some of the PWM outputs, but thats life.
"The usual way to count pulses is to use an interrupt"
I don't think I could use Interrupts ,there's actually 2 encoders.
:-X
What do you suggest ? isn't there anyway that the pulses from both encoders will be counted INDEPENDENTLY ?
you sad that API uses timers for its routines, doen't these routines put back the concerned registers the way they were before ? :o
If you are talking about a two phase quadrature encoder, then you really only need to fire the interrupt on one of the phase lines. In the interrupt handler you can read the other phase line to see if you are counting up or down.
You should be able to find lots of code and wiring examples with Google.
you sad that API uses timers for its routines, doen't these routines put back the concerned registers the way they were before ?
no. The API tries hard to abstract out all the hardware details. Also most of the API functions that need the timers use them for long periods of time eg millis (always), delay, pwm(as long as you are generating the PWM).
That said, if you are not using those features then you are free to do what you want with the timers.
If you want to use the timers to count pulses (a nice feature requiring no CPU time) then you can use T0 and T1 to clock timers 0 and 1 for each encoder.
Or you can setup pin interrupts on INT1 and INT0 (digital pins 3 and 2)
I don't think I could use Interrupts ,there's actually 2 encoders.
Search term of interest: "pin change interrupt"