![]() It is quite easy as you have a working example, an ATmega datasheet and a lot of tutorials on timers, interrupts and even PWM. I'm not writing code as you just have to open Arduino's library and look at what they do, and replicate it by yourself. You may implement attachInterrupt by yourself so you implement the ISR directly (one fewer function call, and function calls are relatively expensive as you have to work with the stack and registers).Īlso don't use micros(), which does a lot of things (read the Timer0 actual count, and calculate count to µs, and is a function call) just read the timer register directly and do the difference with that value, and do the translation of count to µs into loop(). I would suggest to eliminate the function call in the interrupt: unsigned long tmpTime Īnd even attachInterrupt call an ISR that call your function. It will break millis() and micros(), and you can use Timer1 or Timer2, but Timer0 is the only 16-bit timer, so it depends on you), so this is your real precision killer. or this was to have Timer0 to overflow every second? Take a look at the timer prescaler you can change it if it does not break the Servo lib. Arduino 2009, with using a crystal for 16 MHz, has a drift of many seconds at day. How much would this ruin your lecture Not very much. Also remember that a micros() call cost you 4 µs (in fact you will see it growing 4 by 4. 1 Answer Sorted by: 3 Even tachometer () and the function that updates millis () are called from an interrupt, so even it would 'break' your code. So, unless you are doing a lot of calculation in the interrupt routine, you are fine. Arduino Uno, which uses a less precise oscillator, would drift a lot more. Arduino 2009, with using a crystal for 16 MHz, has a drift of many seconds at day. ![]() How much would this ruin your lecture? Not very much. * Determine time between rotations of engine (pulses from tachometer) */Įven tachometer() and the function that updates millis() are called from an interrupt, so even it would "break" your code. Throttle.writeMicroseconds(throttle_idle) // set servo positions Throttle.attach(throttlePin) // attach servo objects to pins volatile unsigned long period ĪttachInterrupt(0, tachometer, RISING) // set external interrupt The tachometer may have to sense a 10,000 RPM speed so about 167 Hz.Įventually I will implement PID control using servos and tachometer. ![]() I am concerned that the Servo library will cause drift of the millis() and micros() counters and make the speed inaccurate. It determines the time between pulses using micros(). The external interrupt routine in my code is for a tachometer. I am wondering what the impact is on the micros() count since it does not increment during an interrupt. From my understanding the Servo library uses a Timer1 interrupt to send pulses to the servo to maintain position. I have a program that utilizes the Servo library and an external interrupt routine.
0 Comments
Leave a Reply. |