以下將列出如何計算與設定
Example:
MCU: PIC18F452
Crystal: 4MHz
System clock: 4MHz/4 = 1MHz (1us)
Timer0有一組prescale可以設定,意義為累計多次"drivid by N"再增加一的動作
然而N就是輸入的clock cycle, 取得輸出cycle out 由prescale決定
Timer0 prescale 可以設定從1(no divid at all), 2, 4, 8...直到256
例如
Prescale 設定256,必須取得256個system clock才能取得一次cycle out,
因此Timer0變成 1us (system clock) * 256(Prescale setting) = 256 us/count.
假設設定Timer0 為8bit模式(PIC18F452T Timer0 有8或16bits可選),prescale設定為1
每經過256會發生中斷(overflow),相同得設定16 bit模式須經過65536 count
完整設定 timer 0 為 8 bit模式, 來源使用 system clock並且搭配prescale 256你設定方式為:
Code: |
set_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_256); |
8 bit mode:
timer 0 overflow(interrupt) 為 256 us/count * 256 counts/overflow = 65536 us = 65.536 ms.
Formula: |
period = 1/Hz = crystal frequence/(4*256-X)*prescale; How to got X value: X = 256-crystal frequence*period/(4*prescale) |
例如:
a) 4,000,000/(4*256*256) = 15.2587890625 Hz
b) f=1/Hz => 1/15.2587890625 = 0.065536 sec = 65.536ms
c) if preset X value = 6 (set time value)
4,000,000/(4*(256-6)*256) = 15.625Hz => 1/15.625Hz=64ms(duty cycle)
例如:
a) 預取得15.625Hz => 1/15.625Hz = 0.064sec (duty cycle)
b) Time0 設定值 X = 256 - 4000000*0.064/(4*256) = 6
Code: |
set_timer0(6); |
16 bit mode:
timer 0 overflow(interrupt) 為 256 us/count * 65536 counts/overflow = 16,777,216 us = 16.77216 sec.
因此最終結論
a) clock source
b) 選擇 8 or 16 bit counter
c) prescale value
沒有留言:
張貼留言