Libraries

Timer2

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.

For advanced users: it is possible to set TCNT2 directly in the user defined function to do things in fractions of a determined prescaler. For more information about this check the source code for the library which is included with the Wiring distribution.

Timer2
Wiring I/O board internal Timer2.

set()
Set the Timer2 parameters.

start()
Starts Timer2.

stop()
Stops Timer2 and releases resources allocated for it.