ServoDecode library for 20MHz arduino

Hi all,

I have built a 20MHz arduino and sorted out the bootloader etc. My code works nicely with the exception of the ServoDecode library.

The issue is that the timing is done in the hardware timer1 using the clock prescaler. It works nicely at 16MHz with the timer being incremented twice per microsecond. I'm not too good with these timer bits, and was wondering whether someone with experience with this timers could help.

The library in question is here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1228137503/0

If anyone has any ideas I'd be most grateful for the help. I think I can work out how to make the needed changes happen at compile time depending on the clock speed of the target device, but I cannot figure out the changes to make it work at 20MHz.

Thanks in advance for your help!

Ed

I seem to have found a stop-gap solution for using this library.

The clock prescaler is set to /8 and the 20mhz clock will give 2.5 ticks per microsecond. Editing the #define TICKS_PER_uS to 2.5 instead of 2 gives good results.

Can anyone see any issues with this?

It seems to work. I'll test it thoroughly and post the results here.

:slight_smile:

Hi Ed,

Its not a solution I like because it brings in the floating point library which is much slower (not a good thing in an interrupt handler) and uses more RAM.

But if nothing else is being affected and you can spare the RAM then it may be ok. I will be interested to hear how you get on.

Not tested but another approach you can try is to change:
Pulses[++Channel] = ICR1 / TICKS_PER_uS; // store pulse length as microsoeconds
To
Pulses[++Channel] = (ICR1 *4) / 10; // store pulse length as microsoeconds

This scales the value by 2.5 using fixed point instead of floating point division.

Have fun!

Hi Mem,

cheers for your input - it's working ok, but I agree it's not ideal. I'm running this on a 328, and would like to save on ram wherever possible. I'll try your idea and see how it goes...

Do you think that the prescaler would be more helpful if set faster, such as /4? I'm new to the hardware timers, but I'm keen to learn about them. I've worked out that you're using it /8 (correct me if I'm wrong!!) so if using it at /4 would it improve the situation? TICKS_PER_uS would then be 5.

I think this might be a good solution however it'll no-doubt cause other issues when this is changed.

Cheers!
Ed

the timer prescaler is in steps of 8 so the step up is prescale of 1 which is would give a ticks per microsecond of 20 using a 20mhz clock. This tick rate is too high, a frame (20ms) would take 400,000 ticks but the counter only holds 64,000 ticks.