Home | Products | Teensy | Blog | Forum |
You are here:TeensyTeensyduinoLibrariesTimerOne |
| TimerOne and TimerThree let you use the built-in 16 bit timers.
Hardware RequirementsThese libraries use Timer1 and Timer3.Each timer controls PWM pins. While uses these libraries, analogWrite() to thosepins will not work normally, but you can use the library pwm() function.
Basic UsageThe timer is configured to repetitively measure a period of time, in microseconds. At the end ofeach period, an interrupt function can be run. The PWM pins can also be configured to switchduring a portion of the period.If using TimerThree, replace 'Timer1' with 'Timer3'. ConfigurationTimer1.initialize(microseconds);Begin using the timer. This function must be called first. 'microseconds' is the periodof time the timer takes. Timer1.setPeriod(microseconds);Set a new period after the library is already initialized. Run ControlTimer1.start();Start the timer, beginning a new period. Timer1.stop();Stop the timer. Timer1.restart();Restart the timer, from the beginning of a new period. Timer1.resume();Resume running a stopped timer. A new period is not begun. PWM Signal OutputTimer1.pwm(pin, duty);Configure one of the timer's PWM pins. 'duty' is from 0 to 1023, where 0 makes thepin always LOW and 1023 makes the pin always HIGH. Timer1.setPwmDuty(pin, duty);Set a new PWM, without reconfiguring the pin. This is slightly faster than pwm(), but pwm()must be used at least once to configure the pin. Timer1.disablePwm(pin);Stop using PWM on a pin. The pin reverts to being controlled by digitalWrite(). Interrupt FunctionTimer1.attachInterrupt(function);Run a function each time the timer period finishes. The function is run as aninterrupt, so special care is needed to share any variables beteen the interruptfunction and your main program. Timer1.detachInterrupt();Disable the interrupt, so the function no longer runs. Example ProgramInterrupt Context IssuesSpecial design is necessary to share data between interrupt code and therest of your program.Variables usually need to be 'volatile' types. Volatile tells the compiler toavoid optimizations that assume variable can not spontaneously change. Because yourfunction may change variables while your program is using them, the compiler needsthis hint. But volatile alone is often not enough. When accessing shared variables, usually interrupts must be disabled. Even withvolatile, if the interrupt changes a multi-byte variable between a sequence ofinstructions, it can be read incorrectly. If your data is multiple variables,such as an array and a count, usually interrupts need to be disabled for the entiresequence of your code which accesses the data. DetailsNew development on TimerOne occurs on this Github page:https://github.com/PaulStoffregen/TimerOne https://github.com/PaulStoffregen/TimerThree The original version of these libraries is at: Timer1 page. |
Timer 90 Min
Timer 9 0 3 0
1 min -: 3 min -: 5 min -: 10 min: 15 min -: 20 min -: 30 min -: 1 h. A cool little 9 Minute Timer! Simple to use, no settings, just click start for a countdown timer of 9 Minutes. Try the Fullscreen button in classrooms and meetings:-).