Timer Module API Manual#
Overview#
The K230 integrates 6 hardware Timer modules internally, with a minimum timing period of 1 millisecond (ms).
API Introduction#
The Timer class is located in the machine module.
Example Code#
from machine import Timer
import time
# Instantiate a software timer
tim = Timer(-1)
# Configure the timer, one-shot mode, period 100 ms, callback function prints 1
tim.init(period=100, mode=Timer.ONE_SHOT, callback=lambda t: print(1))
time.sleep(0.2)
# Configure the timer, periodic mode, period 1000 ms, callback function prints 2
tim.init(freq=1, mode=Timer.PERIODIC, callback=lambda t: print(2))
time.sleep(2)
# Release timer resources
tim.deinit()
Constructor#
timer = Timer(index, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None)
Parameters
index: Timer module number, the value range is [-1, 5], where -1 indicates a software timer.mode: Timer running mode, can be one-shot or periodic mode (optional parameter).freq: Timer running frequency, supports floating-point numbers, in Hertz (Hz), this parameter has higher priority thanperiod(optional parameter).period: Timer running period, in milliseconds (ms) (optional parameter).callback: Timeout callback function, must be set and should take one parameter.
init Method#
Timer.init(mode=Timer.PERIODIC, freq=-1, period=-1, callback=None)
Initialize timer parameters.
Parameters
mode: Timer running mode, can be one-shot or periodic mode (optional parameter).freq: Timer running frequency, supports floating-point numbers, in Hertz (Hz), this parameter has higher priority thanperiod(optional parameter).period: Timer running period, in milliseconds (ms) (optional parameter).callback: Timeout callback function, must be set and should take one parameter.
Return Value
None
deinit Method#
Timer.deinit()
Release timer resources.
Parameters
None
Return Value
None
