This page describes how to control the built-in timers. It does not describe how the timers work on your board. For more information on that, the timers reference.
Warning
The timer interface is still taking shape, and is expected to change significantly between releases. Because of that, the functionality described in this page shouldn’t be considered stable.
If you want a timer API that will be consistent between releases of the Maple IDE, your best bet for now is to use the low-level support in timer.h.
Contents
You’ll first need to define a HardwareTimer variable, which you’ll use to control the timer. Do this by putting the line “HardwareTimer timer(number);” with your variables, where number is the timer’s number.
Here’s an example (we’ll fill in setup() and loop() later):
// Use timer 1
HardwareTimer timer(1);
void setup() {
// Your setup code
}
void loop() {
// ...
}
After defining your timer variable, you’ll probably want to configure how fast your timer’s counter changes (using the prescaler) and when it gets reset to zero (using the overflow value). You can do that with the setPrescaleFactor() and setOverflow() functions.
Set the timer’s prescale factor.
The new value won’t take effect until the next time the counter overflows. You can force the counter to reset using HardwareTimer::refresh().
Parameters: |
|
---|---|
See: |
Set the timer overflow (or “reload”) value.
The new value won’t take effect until the next time the counter overflows. You can force the counter to reset using HardwareTimer::refresh().
Parameters: |
|
---|---|
See: |
For example:
// Use timer 1
HardwareTimer timer(1);
void setup() {
timer.setPrescaleFactor(5);
timer.setOverflow(255);
}
void loop() {
// ...
}
You may also find the setPeriod() function useful:
Set the timer’s period in microseconds.
Configures the prescaler and overflow values to generate a timer reload with a period as close to the given number of microseconds as possible.
Parameters: |
|
---|---|
Return: | The new overflow value. |
For example:
// Use timer 1
HardwareTimer timer(1);
void setup() {
// Have the timer repeat every 20 milliseconds
int microseconds_per_millisecond = 1000;
timer.setPeriod(20 * microseconds_per_millisecond);
}
void loop() {
// ...
}
In order to use timer interrupts, we recommend the following sequence:
Here are two complete examples.
LED blink: This example blinks the built-in LED without doing anything in loop().
#define LED_RATE 500000 // in microseconds; should give 0.5Hz toggles
// We'll use timer 2
HardwareTimer timer(2);
void setup() {
// Set up the LED to blink
pinMode(BOARD_LED_PIN, OUTPUT);
// Pause the timer while we're configuring it
timer.pause();
// Set up period
timer.setPeriod(LED_RATE); // in microseconds
// Set up an interrupt on channel 1
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
timer.attachCompare1Interrupt(handler_led);
// Refresh the timer's count, prescale, and overflow
timer.refresh();
// Start the timer counting
timer.resume();
}
void loop() {
// Nothing! It's all in the handler_led() interrupt:
}
void handler_led(void) {
toggleLED();
}
Racing Counters: This example shows how to use multiple timers at the same time.
int count3 = 0;
int count4 = 0;
// We'll use timers 3 and 4
HardwareTimer timer3(3);
HardwareTimer timer4(4);
void setup() {
// Set up the button for input
pinMode(BOARD_BUTTON_PIN, INPUT_PULLUP);
// Set up timers to add 1 to their counts each time
// their interrupts fire.
timer3.setMode(TIMER_CH1, TIMER_OUTPUT_COMPARE);
timer4.setMode(TIMER_CH1, TIMER_OUTPUT_COMPARE);
timer3.pause();
timer4.pause();
timer3.setCount(0);
timer4.setCount(0);
timer3.setOverflow(30000);
timer4.setOverflow(30000);
timer3.setCompare(TIMER_CH1, 1000); // somewhere in the middle
timer4.setCompare(TIMER_CH1, 1000);
timer3.attachCompare1Interrupt(handler3);
timer4.attachCompare1Interrupt(handler4);
timer3.refresh();
timer4.refresh();
timer3.resume();
timer4.resume();
}
void loop() {
// Display the running counts
SerialUSB.print("Count 3: ");
SerialUSB.print(count3);
SerialUSB.print("\t\tCount 4: ");
SerialUSB.println(count4);
// While the button is held down, pause timer 4
for (int i = 0; i < 1000; i++) {
if (digitalRead(BOARD_BUTTON_PIN)) {
timer4.pause();
} else {
timer4.resume();
}
delay(1);
}
}
void handler3(void) {
count3++;
}
void handler4(void) {
count4++;
}
This section gives a full listing of the capabilities of a HardwareTimer.
Interface to one of the 16-bit timer peripherals.
Public Functions
HardwareTimer(uint8 timerNum)Construct a new HardwareTimer instance.
Parameters:
- timerNum -
number of the timer to control.
void pause(void)Stop the counter, without affecting its configuration.
See: HardwareTimer::resume() void resume(void)Resume a paused timer, without affecting its configuration.
The timer will resume counting and firing interrupts as appropriate.
Note that there is some function call overhead associated with using this method, so using it in concert with HardwareTimer::pause() is not a robust way to align multiple timers to the same count value.
See: HardwareTimer::pause() uint32 getPrescaleFactor()Get the timer’s prescale factor.
Return: Timer prescaler, from 1 to 65,536. See: HardwareTimer::setPrescaleFactor() void setPrescaleFactor(uint32 factor)Set the timer’s prescale factor.
The new value won’t take effect until the next time the counter overflows. You can force the counter to reset using HardwareTimer::refresh().
Parameters:
- factor -
The new prescale value to set, from 1 to 65,536.
See: uint16 getOverflow()Get the timer overflow value.
See: HardwareTimer::setOverflow() void setOverflow(uint16 val)Set the timer overflow (or “reload”) value.
The new value won’t take effect until the next time the counter overflows. You can force the counter to reset using HardwareTimer::refresh().
Parameters:
- val -
The new overflow value to set
See: uint16 getCount(void)Get the current timer count.
Return: The timer’s current count value void setCount(uint16 val)Set the current timer count.
Parameters:
- val -
The new count value to set. If this value exceeds the timer’s overflow value, it is truncated to the overflow value.
uint16 setPeriod(uint32 microseconds)Set the timer’s period in microseconds.
Configures the prescaler and overflow values to generate a timer reload with a period as close to the given number of microseconds as possible.
Parameters:
- microseconds -
The desired period of the timer. This must be greater than zero.
Return: The new overflow value.
void setMode(int channel, timer_mode mode)Configure a timer channel’s mode.
Parameters:
- channel -
Timer channel, from 1 to 4
- mode -
Mode to set
uint16 getCompare(int channel)Get the compare value for the given channel.
See: HardwareTimer::setCompare() void setCompare(int channel, uint16 compare)Set the compare value for the given channel.
Parameters:
- channel -
the channel whose compare to set, from 1 to 4.
- compare -
The compare value to set. If greater than this timer’s overflow value, it will be truncated to the overflow value.
See: timer_mode
void attachInterrupt(int channel, voidFuncPtr handler)Attach an interrupt handler to the given channel.
This interrupt handler will be called when the timer’s counter reaches the given channel compare value.
Parameters:
- channel -
the channel to attach the ISR to, from 1 to 4.
- handler -
The ISR to attach to the given channel.
See: voidFuncPtr
void detachInterrupt(int channel)Remove the interrupt handler attached to the given channel, if any.
The handler will no longer be called by this timer.
Parameters:
- channel -
the channel whose interrupt to detach, from 1 to 4.
See: void refresh(void)Reset the counter, and update the prescaler and overflow values.
This will reset the counter to 0 in upcounting mode (the default). It will also update the timer’s prescaler and overflow, if you have set them up to be changed using HardwareTimer::setPrescaleFactor() or HardwareTimer::setOverflow().
See:
Used to configure the behavior of a timer channel.
Be careful: not all timers can be configured in every mode.
Values:
The timer stops counting, channel interrupts are detached, and no state changes are output.
PWM output.
The timer counts from 0 to its reload value repeatedly; every time the counter value reaches one of the channel compare values, the corresponding interrupt is fired.
The following functionality exists for now, but it has been deprecated, and will be removed in a future Maple IDE release. You shouldn’t use it in new programs, and you should change any of your programs which do use them to use the up-to-date features described above.
The TimerMode type from previous releases has been renamed timer_mode. The mode TIMER_OUTPUTCOMPARE is still present, but will be removed in a future release. Use TIMER_OUTPUT_COMPARE instead.
Use attachInterrupt(1, handler) instead.
Use attachInterrupt(2, handler) instead.
Use attachInterrupt(3, handler) instead.
Use attachInterrupt(4, handler) instead.
Use setMode(channel, mode) instead.
In previous releases, to interact with a particular timers, you would use one of the predefined HardwareTimer instances Timer1, Timer2, Timer3, and Timer4. These are still available for now, but they are also deprecated, and will be removed in a future release. As detailed in Getting Started, you should define your own HardwareTimer variables.