machine.Timer function
Hardware timer can be used to trigger tasks or process tasks at a fixed time. When the set time is reached, it can trigger an interrupt (call a callback function). It has higher accuracy than software timer. It should be noted that the timer may behave differently in different hardware.
The MicroPython Timer class defines the basic operation of executing a callback at a given time period (or executing a callback after a delay), and allows for more non-standard behavior to be defined for specific hardware (and therefore not portable to other boards).
There are 3 timers in total, each timer has 4 channels available.
Parameters
id: Timer ID, [0~2] (Timer.TIMER0~TIMER2)
channel: Timer channel, [Timer.CHANNEL0~Timer.CHANNEL3]
mode: Timer mode, MODE_ONE_SHOT or MODE_PERIODIC or MODE_PWM
period: Timer period, after starting the timer, the callback function will be called, (0,~)
unit: Set the unit of the period, the default is milliseconds (ms), Timer.UNIT_S or Timer.UNIT_MS or Timer.UNIT_US or Timer.UNIT_NS
callback: Timer callback function, two parameters are defined, one is the timer object Timer, the second is the parameter arg that you want to pass in the definition object, please see the arg parameter explanation for more information
Note: The callback function is called in the interrupt, so please do not take up too much time in the callback function and do actions such as dynamically allocating switch interrupts, etc.
arg: the parameter you want to pass to the callback function, as the second parameter of the callback function
start: whether to start the timer immediately after the object is successfully constructed, True: start immediately, False: do not start immediately, you need to call the start() function to start the timer
priority: hardware timer interrupt priority, related to the specific CPU, in K210, the value range is [1,7], the smaller the value, the higher the priority
div: hardware timer divider, the value range is [0,255], the default is 0, clk_timer (timer clock frequency) = clk_pll0 (phase-locked loop 0 frequency)/2^(div+1)
clk_timer*period(unit:s) should be < 2^32 and >=1
|