Timer of 168/328 and Arduino/ servo PPM



話說花了一個晚上去看, mega168其實提供3個timers, Timer0, Timer1, Timer2
Timer1是16bits, timer0跟timer2都是8bits....

Timer 是根據system clock(當然clock source 也是可以另外選的),再去用prescale做除頻的動作....
可以config的function很多....

arduino本身在wiring.c裏的initial 對timer0作了規劃,用來做millis()跟micro()的基本....他選了64當prescale
16Mhz/64=250Khz, 也就是說Timer一格是0.000004 s=4us, 這就是為什麼micros()的精確度最多到4us
8bit的timer0,over flow 是:4us*256=1024us=1.024ms,millis()就是用這個去做計算, 但是它有做誤差的補償...

arduino另外對timer1也做了PWM輸出的規劃....拿來做analog.write,不幸得很, 他也是用64當prescale,所以解析度也是4us

解析度4us,其實用來輸出servo PPM時最大誤差會到7-8us,這就會有飄來飄去的問題...
4us對RC servo來說, 勉強而已....一般servo的death band約就在4us....

其實以16Mhz, 16 bits timer,
prescale=1, 解析度:0.0625us , overflow period:4.096ms
prescale=8, 解析度:0.5us , overflow period:32.768ms
Prescale=16, 解析度:1us overfolw period:65.536ms
prescale=32, 解析度:2us overflow period:131.072ms
prescale 選8 很適合需要快速反應的servo application.
mega168在 timer1上還有一個capture mode, 有 noise filter可以選(其實就是要4個訊號都一致才算trigger,所以會有4個system clock的delay)

留言

這個網誌中的熱門文章

Heltec ESP32+OLED+Lora, hardware testing

micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled

Install Network Time Protocol(NTP) on BeagleBone with Angstrom linux and set local time zone