en-us_commands:reference:itimerset
This is an old revision of the document!
Table of Contents
ItimerSet()
ItimerSet() sets the interval and callback function, or the hardware timer to use.
bool ItimerSet(unsigned long intervalMs, void (*callback)())
bool ItimerSet(uint8_t timerNumber, unsigned long intervalMs, void (*callback)())
Commands
| Command | Arguments | Return Value | Operation |
|---|---|---|---|
ItimerSet(intervalMs, callback) | unsigned long intervalMs — Callback interval in ms. 0 is not allowedvoid (*callback)() — Function in void functionName() form, called at each interval | bool | Sets the interval and callback function, or the hardware timer to use. |
Example
volatile bool tick = false; void onTimer() { tick = true; } void setup() { pinMode(13, OUTPUT); ItimerSet(1000, onTimer); // Set the interval and callback function, or select the hardware timer to use. ItimerStart(); // Start the configured callback timer. } void loop() { if (tick) { tick = false; digitalWrite(13, !digitalRead(13)); } }
Precautions
- Timer interrupt callbacks run inside an ISR. For safety, change only flags in the callback and perform actual control processing in
loop(). - Do not use slow or interrupt-dependent code such as
delay(),Serial, I2C or SPI in callbacks. - Normally, call
ImodbusRTU()repeatedly inloop(). Call it from a timer interrupt with a sufficient interval only when longdelay()calls or debug output slow downloop(). - Do not use the default
ItimerSet()together withtone()using Timer2 oranalogWrite()on Timer2 PWM pins. - Explicitly using Timer1, Timer3, Timer4 or Timer5 may conflict with PWM, analog output or high-speed counter functions using that timer.
- Configure only one
Itimercallback per hardware timer. - Defining an ISR for the same timer directly in the sketch may conflict with
Itimeroperation. ItimerStopAll()stops only hardware interrupt timers. It does not reset PLC-style timer states such asIton,ItoforItmr.
en-us_commands/reference/itimerset.1788791735.txt.gz · Last modified: by 127.0.0.1
