The TimerOne library is available from 86Duino Coding 105 and provides the API of the Arduino TimerOne library to access 86Duino’s 32-bit hardware timers.
The library provides two objects — Timer1 and TimerRTC —
to manipulate different hardware timers as follows.
Timer1
This object includes a collection of routines for configuring the 32-bit internal high-precision timer in 86Duino’s CPU, which can be configured in a variety of ways to achieve different functionality. It provides a way to quickly and easily set the PWM period or frequency and also includes timer interrupt handling and other features.
Note that since the Servo and Servo86 libraries also employ the same internal timer, the one shouldn’t use Timer1 and the Servo library, as well as the Servo86 library, at the same time.
— Documentation —
Simplified descriptions of what the most important routines do
initialize(period)
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.setPeriod(period)
Sets the period in microseconds. The minimum period or highest frequency this library supports is 1 microsecond or 1 MHz. The maximum period is 21474836 microseconds or about 21.47 seconds. Note that setting the period will change the attached interrupt and pwm outputs’ frequencies simultaneously.pwm(pin, duty, period)
Generates a PWM waveform on the specified pin. Note that you have to choose output pins between PWM pins, anything else is ignored; For example, on 86Duino Zero, these are digital pins 3, 5, 6, 9, 10, 11 and 13. The period parameter (in microseconds) isn’t necessary, but you can optionally set it to change the period settled by setPeriod(period).attachInterrupt(function, period)
Calls a specified 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 the period parameter (in microseconds) isn’t necessary, but you can optionally set it to change the period settled by setPeriod(period).setPwmDuty(pin, duty)
A fast shortcut for setting the pwm duty for a given pin if you have already set it up by calling pwm() earlier.detachInterrupt()
Disables the attached interrupt.disablePwm(pin)
Turns PWM off for the specified pin so you can use that pin for something else.read()
Reads the time since last rollover in microseconds.
— 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
/* * Timer1 library example * June 2008 | jesse dot tane at gmail dot com */ #include "TimerOne.h" void setup() { pinMode(10, OUTPUT); Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period Timer1.pwm(9, 512); // setup pwm on pin 9, 50% duty cycle Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt } void callback() { digitalWrite(10, digitalRead(10) ^ 1); } void loop() { // your program here... }
— Other sketches/snippets that use Timer1 —
examples from the Arduino Tutorial
- DirectDriveLEDMatrix
- DirectDrive88884Digit7SegmentDisplay
- ReadReceiver
- ACPhaseControl
- Suzuki V-Strom Motorcycle display
TimerRTC
This object includes a collection of routines to utilize the interrupt function of the RTC timer in 86Duino’s CPU. Unlike Timer1, it provides only timer interrupt handling but no PWM functionality. TimerRTC also has less accuracy than Timer1, but since it employs another timer, there is no conflict between TimerRTC and the Servo library.
On the other hand, since the Servo86 library still employs the RTC timer, the one shouldn’t use TimerRTC and the Servo86 library at the same time.
— Documentation —
Simplified descriptions of what the most important routines do.
initialize(period)
You must call this method first to use any of the other methods. You can optionally specify the RTC timer’s period here (in microseconds), by default it is set at 0.5 second.setPeriod(period)
Sets the period in microseconds. The maximum period is 500000 microseconds or about 0.5 seconds. Note that the time per RTC tick is about 30.5 us, which is larger than 1us, and so the library would actually set an approximate period rather than the specified period; moreover, the minimum period that can be achieved is 122us (even though you set a 1us period).attachInterrupt(function, period)
Calls a specified 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 the period parameter (in microseconds) isn’t necessary, but you can optionally set it to change the period settled by setPeriod(period).detachInterrupt()
Disables the attached interrupt.
— Example —
Attaches an interrupt that toggles digital pin 10 every half second
#include "TimerOne.h" void setup() { pinMode(10, OUTPUT); TimerRTC.initialize(500000); // initialize timer1, and set a 1/2 second period TimerRTC.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt } void callback() { digitalWrite(10, digitalRead(10) ^ 1); } void loop() { // your program here... }
Libraries Reference Home
The text of the 86Duino reference is a modification of the Arduino reference and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.