Reference for Wiring version 0027+ If you have a previous version, use the reference includede with your software. If see any errors or have any comments, let us know.
Class | Timer2 |
||||||
---|---|---|---|---|---|---|---|
Name | set() |
||||||
Examples | #include "HardwareTimer2.h"; int ledPin = 48; // onboard LED on the Wiring I/O board (pin 48) byte value = HIGH; void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output // sets Timer2 to execute function ledFlash() every 100ms Timer2.set(100, ledFlash); // sets Timer2 to execute function ledFlash() using timer prescaler 1024 // F_CPU / 1024 in a 16Mhz will be 15625 // 15625 / 255 = 61.27 overflows (full countings to 255) // 1000ms / 61.27 = 16.32ms, so function ledFlash() will be executed every 16.32ms //Timer2.set(ledFlash, CLOCK_DIV1024); // start timer Timer2.start(); } void loop() { } void ledFlash() { digitalWrite(ledPin, value); if(value == HIGH) value = LOW; else value = HIGH; } |
||||||
Description | Timer2 is an 8-bit hardware timer present in many Atmel microcontrollers. A hardware timer might be used among other things to clock events. The Timer2 library allows for flexible manipulation of this hardware timer. The timer can be set to execute a function every certain milliseconds, a difference with setting up a software timer is the precision. Timer2 clock source is the main CPU clock which runs at 16Mhz F_CPU = 16000000, this frequency might be too fast for many uses, (1/16000000) of a second, so a prescaler is used for the clock source. A prescaler is the cpu speed divided by some power of 2. There are some prescalers defined for Timer2: CLOCK_DIV1, CLOCK_DIV8, CLOCK_DIV32, CLOCK_DIV64, CLOCK_DIV128, CLOCK_DIV256 and CLOCK_DIV1024, which are respectively F_CPU/1, F_CPU/8, F_CPU/32, F_CPU/128, F_CPU/256 and F_CPU/1024. For flexibility, Timer2 can be set in two ways, one to execute a function every certain milliseconds and the second to execute a function at a frequency defined by a prescaler. Depending on the hardware, some prescalers for Timer2 might not be available, for example for the atmega128 microcontroller the prescalers CLOCK_DIV32 and CLOCK_DIV128 are not defined, but they are defined for Timer2 on atmega1281 and atmega2561. Since Timer2 is an 8-bit timer internally it overflows when its counter TCNT2 reaches the value 255. In the set() method, one way sets the time (in milliseconds) and the function name to execute (the best prescaler is selected automatically), the second way sets the function to execute and the prescaler to use, time between function calls varies according to the prescaler specified. | ||||||
Syntax | Timer2.set(time, function) |
||||||
Parameters |
|
||||||
Returns | None | ||||||
Usage | Application |