Arduino Timer
Arduino Timer
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
Timers in Arduino
The PWM analogWrite(), the tone() and the noTone() function Even the Servo library uses timers.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
Function analogWrite() writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at
various speeds.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
wave of the specied duty cycle until the next call to analogWrite().
What is a timer?
A timer is a piece of hardware built-in the
Arduino controller. time events. registers.
It is like a clock, and can be used to measure The timer can be programmed by some special You can congure the pre-scalar for the timer,
ATmega328. the mode of operation and many other things.
What is a timer?
ATmega328 has 3 timers, called timer0,
timer1 and timer2. is a 16bit timer.
Timer0 and timer2 are 8bit timers, while timer1 More bits means higher timing resolution. Remember, 8bits has 256 different states while
16bit has 65536.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
Timer0
Timer0 is a 8bit timer. In the Wiring libraries, timer0 is used for
the commonly used timer functions.
You know... delay(), millis() and micros(). Warning: If you change the timer0
registers, this may inuence the Arduino timings function. mess up a timer register, just upload a new program.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
Timer1
Timer1 is a 16bit timer. In the Wiring libraries, this timer is used for the servo library.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
Timer2
Timer2 is a 8bit timer. In the Wiring libraries, this timer is
used for the tone() function.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
f = 1 /T For example, if a newborn baby's heart beats at a frequency of Click for a neat frequency animation.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
120 times a minute, its period (the interval between beats) is half a second.
Prescaler or count-divider
A prescaler is an electronic counting circuit used to reduce a high frequency electrical signal to a lower frequency by integer division.
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering
TCCRx - Timer/Counter Control Register. Prescaler is congured here. TCNTx - Timer/Counter Register. The actual timer value is stored here. OCRx - Output Compare Register ICRx - Input Capture Register (only for 16 bit timer) TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable
timer interrupts. interrupt.
register 16MHz/256/2Hz WGM12); CS12); OCIE1A); // // // // CTC mode 256 prescaler enable timer compare interrupt enable all interrupts
CPE 355 - Real Time Embedded Kernels - Spring 12 Nuno Alves (nalves@wne.edu), College of Engineering