Arduino/AVR timers2
شنبه, ۷ مرداد ۱۳۹۶، ۱۲:۱۶ ق.ظ
To summarize:
Timer0: Timer0 is a 8bit timer. In the Arduino world Timer0 is been used for the timer functions, like delay(), millis() and micros(). If you change Timer0 registers, this may influence the Arduino timer function. So you should know what you are doing. Timer1: Timer1 is a 16bit timer. In the Arduino world the Servo library uses Timer1 on Arduino Uno (Timer5 on Arduino Mega). Timer2: Timer2 is a 8bit timer like Timer0. In the Arduino work the tone() function uses Timer2. Timer3, Timer4, Timer5: Timer 3,4,5 are only available on Arduino Mega boards. These timers are all 16bit timers. Timer Register You can change the Timer behaviour through the timer register. The most important timer registers are:
TCCRx - Timer/Counter Control Register. The pre-scaler can be configured here.
TCNTx - Timer/Counter Register. The actual timer value is stored here.
OCRx - Output Compare Register
ICRx - Input Capture Register (only for 16bit timer)
TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.
TIFRx - Timer/Counter Interrupt Flag Register. Indicates a pending timer interrupt.
Clock select and timer frequency
Different clock sources can be selected for each timer independently. To calculate the timer frequency (for example 2Hz using Timer1) you will need:
1. CPU frequency 16Mhz for Arduino
2. maximum timer counter value (256 for 8bit, 65536 for 16bit timer)
3. Divide CPU frequency through the chosen pre-scaler (16000000 / 256 = 62500)
4. Divide result through the desired frequency (62500 / 2Hz = 31250)
5. Verify the result against the maximum timer counter value (31250 < 65536 success) if fail, choose bigger pre-scaler.
ref : https://oscarliang.com/arduino-timer-and-interrupt-tutorial/
- ۹۶/۰۵/۰۷