1

There are many software timer libraries available in Arduino.
A library called SimpleTimer http://playground.arduino.cc/Code/SimpleTimer is really good with many functions, but it uses polling.
If anyone is familiar with a really good library that uses Timer ISR of Atmega instead of polling, please suggest me as i am using ultrasonic sensors and i can't afford polling.

  • 1
    If it used the hardware timer then it wouldn't be a software timer library. – Ignacio Vazquez-Abrams Mar 16 '15 at 6:26
  • ultrasonic sensor is using the pulseIn() function. But i need software timer for another purpose. – explorer Mar 16 '15 at 6:29
1

Yeah, I have the same problem. The only solution I found is to rewrite a lot of the code. Still ongoing, though.

For you, Timer1 is probably the best candidate, unless you have already some use for it, like in my case.

A quick hack is to free Timer0: it is currently (ab)used by the wire library, which provides the typical arduino timing. But you can remove/rename the file wiring.cpp to something that will not be identified as source (ex: wiring._cpp) and comment out the init() function call from the main() function in the main.c file.

Normal Arduino stile time functions will not work, of course.

Unfortunatelly Arduino SW really lacks the structured design that would allow it to be useful in more complex cases and most of the libraries make exclusive use of resources that could be shared, timer being the most prominent example.

Port ISRs is another.

Btw, if you happen to use a SW serial library, it's already game over, as it busy loops inbetween bits, in interrupt context (!), rather than using a timer.

| improve this answer | |
  • Correct me if i am wrong: the first line of timer1 library page says : "This library is a collection of routines for configuring the 16 bit hardware timer called Timer1" Can it be used to generate software interrupts? – explorer Mar 16 '15 at 6:47
  • I decided to ignore the library and write my own, as I didn't like the style used by the library. Here's my (GPLv2) work: github.com/igor-stoppa/pem Check github.com/igor-stoppa/pem/tree/master/src/pem_Arduino you can look at hw-adaptation-uno.cpp for a very simple exampel of how to use the timer. – Igor Stoppa Mar 16 '15 at 7:07
  • Thanks! I don't intent to use your entire work, but it will surely help me write my own code. thanks. – explorer Mar 16 '15 at 8:03
  • Yes, I wasn't proposing that :-) But it shows in few lines how to use directly the timer interrupt. For more details, you can refer to the fine manual, but it shouldn't be necessary. The slightly trickier part is to calculate the value to use. – Igor Stoppa Mar 16 '15 at 8:42
  • What you do, is: check your main clock (20/16/8MHz), choose a divider for the prescaler, then calculate the scaled clock (main/prescaler) and multiply it by the period you want to use. The result must be subtracted from the max value that the timer register can assume (256 for 8bit, 65536 for 16bit register). This will cause the Overflow interrupt to trigger once the max value is reached. Then you can rearm or not, depending if it's periodic. Remember to rearm as first thing, so that the period is stable, regardless of what else you do in the isr. – Igor Stoppa Mar 16 '15 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.