:: Timer1 ::
This library is a collection of routines for configuring the 16 bit hardware timer called Timer1 on the ATmega168/328. There are 3 hardware timers available on the chip, and they can be configured in a variety of ways to achieve different functionality. The development of this library began with the need for a way to quickly and easily set the PWM period or frequency, but has grown to include timer overflow interrupt handling and other features. It could easily be expanded upon or ported to work with the other timers.
The accuracy of the timer depends on your processor speed and the frequency. Timer1's clock speed is defined by setting the prescaler, or divisor. This prescale can be set to 1, 8, 64, 256 or 1024.
|Prescale||Time per counter tick||Max Period|
|1||0.0625 uS||8.192 mS|
|8||0.5 uS||65.536 mS|
|64||4 uS||524.288 mS|
|256||16 uS||2097.152 mS|
Download -> TimerOne Google Code download
To install, simply unzip and put the files in Arduino/hardware/libraries/Timer1/
:: Timer3 ::
Note that timer1 can be used on a Mega but does not support all three output pins OCR1A, OCR1B & OCR1C. Only A & B are supported. OCR1A is connected to pin 11 of the Mega and OCR1B to pin 12. Using one of the three calls that specify a pin, 1 will map to pin 11 on the Mega and 2 will map to pin 12. Timer3 has only been tested on the Mega.
License: Creative Commons
Download -> TimerThree.zip
To install, simply unzip and put the files in Arduino/hardware/libraries/Timer3/
Documentation - Simplified descriptions of what the most important routines do
You must call this method first to use any of the other methods. You can optionally specify the timer's period here (in microseconds), by default it is set at 1 second. Note that this breaks analogWrite() for digital pins 9 and 10 on Arduino.
Sets the period in microseconds. The minimum period or highest frequency this library supports is 1 microsecond or 1 MHz. The maximum period is 8388480 microseconds or about 8.3 seconds. Note that setting the period will change the attached interrupt and both pwm outputs' frequencies and duty cycles simultaneously.
pwm(pin, duty, period)
Generates a PWM waveform on the specified pin. Output pins for Timer1 are PORTB pins 1 and 2, so you have to choose between these two, anything else is ignored. On Arduino, these are digital pins 9 and 10, so those aliases also work. Output pins for Timer3 are from PORTE and correspond to 2,3 & 5 on the Arduino Mega. The duty cycle is specified as a 10 bit value, so anything between 0 and 1023. Note that you can optionally set the period with this function if you include a value in microseconds as the last parameter when you call it.
Calls a function at the specified interval in microseconds. Be careful about trying to execute too complicated of an interrupt at too high of a frequency, or the CPU may never enter the main loop and your program will 'lock up'. Note that you can optionally set the period with this function if you include a value in microseconds as the last parameter when you call it.
A fast shortcut for setting the pwm duty for a given pin if you have already set it up by calling pwm() earlier. This avoids the overhead of enabling pwm mode for the pin, setting the data direction register, checking for optional period adjustments etc. that are mandatory when you call pwm().
Disables the attached interrupt.
Turns PWM off for the specified pin so you can use that pin for something else.
Reads the time since last rollover in microseconds.
Method Detail - calling conventions for all public methods
void initialize(long microseconds=1000000);
unsigned long read();
void setPeriod(long microseconds);
void pwm(char pin, int duty, long microseconds=-1);
void setPwmDuty(char pin, int duty);
void disablePwm(char pin);
void attachInterrupt(void (*isr)(), long microseconds=-1);
Example - Sets up PWM output on pin 9 with a 50% duty cycle, and attaches an interrupt that toggles digital pin 10 every half second.