class Timer – control internal timers¶
Timers can be used for a great variety of tasks, calling a function periodically, counting events, and generating a PWM signal are among the most common use cases. Each timer consists of 2 16-bit channels and this channels can be tied together to form 1 32-bit timer. The operating mode needs to be configured per timer, but then the period (or the frequency) can be independently configured on each channel. By using the callback method, the timer event can call a Python function.
Example usage to toggle an LED at a fixed frequency:
tim = pyb.Timer(4) # create a timer object using timer 4 tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode tim_ch = tim.channel(Timer.A, freq=2) # configure channel A at a frequency of 2Hz tim_ch.callback(handler=lambda t:led.toggle()) # toggle a LED on every cycle of the timer
Example using named function for the callback:
tim = Timer(1, mode=Timer.PERIODIC) tim_a = tim.channel(Timer.A, freq=1000) led = Pin('GPIO2', af=0, mode=Pin.OUT) def tick(timer): # we will receive the timer object when being called print(timer.time()) # show current timer's time value (is microseconds) led.toggle() # toggle the LED tim_a.callback(handler=tick)
tim1 = pyb.Timer(2, mode=Timer.EVENT_COUNT) # initialize it capture mode tim2 = pyb.Timer(1, mode=Timer.PWM) # initialize it in PWM mode tim_ch = tim1.channel(Timer.A, freq=1, polarity=Timer.POSITIVE) # start the event counter with a frequency of 1Hz and triggered by positive edges tim_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=50) # start the PWM on channel B with a 50% duty cycle tim_ch.time() # get the current time in usec (can also be set) tim_ch.freq(20) # set the frequency (can also get) tim_ch.duty_cycle(30) # set the duty cycle to 30% (can also get) tim_ch.duty_cycle(30, Timer.NEGATIVE) # set the duty cycle to 30% and change the polarity to negative tim_ch.event_count() # get the number of captured events tim_ch.event_time() # get the the time of the last captured event tim_ch.period(2000000) # change the period to 2 seconds
Note: Memory can’t be allocated during a callback (an interrupt) and so
exceptions raised within a callback don’t give much information. See
micropython.alloc_emergency_exception_buf() for how to get around this
Construct a new timer object of the given id. If additional arguments are given, then the timer is initialised by
idcan be 1 to 4.
init(mode, *, width=16)
Initialise the timer. Example:
tim.init(Timer.PERIODIC) # periodic 16-bit timer tim.init(Timer.ONE_SHOT, width=32) # one shot 32-bit timer
modecan be one of:
Timer.ONE_SHOT- The timer runs once until the configured period of the channel expires.
Timer.PERIODIC- The timer runs periodically at the configured frequency of the channel.
Timer.EDGE_TIME- Meaure the time pin level changes.
Timer.EDGE_COUNT- Count the number of pin level changes.
widthmust be either 16 or 32 (bits). For really low frequencies <= ~1Hz (or large periods), 32-bit timers should be used. 32-bit mode is only available for
Deinitialises the timer.
Disables any channel callbacks (and the associated irq). Stops the timer, and disables the timer peripheral.
channel(channel, **, freq, period, polarity=Timer.POSITIVE, duty_cycle=0)
If only a channel identifier passed, then a previously initialized channel object is returned (or
Noneif there is no previous channel).
Othwerwise, a TimerChannel object is initialized and returned.
The operating mode is is the one configured to the Timer object that was used to create the channel.
channelif the width of the timer is 16-bit, then must be either
TIMER.B. If the width is 32-bit then it must be
TIMER.A | TIMER.B.
Keyword only arguments:
freqsets the frequency in Hz.
periodsets the period in microseconds.
periodmust be given, never both.
polaritythis is applicable for:
PWM, defines the polarity of the duty cycle
EDGE_COUNT, defines the polarity of the pin level change to detect. To detect both rising and falling edges, make
polarity=Timer.POSITIVE | Timer.NEGATIVE.
duty_cycleonly applicable to
PWM. It’s a percentage (0-100)
class TimerChannel — setup a channel for a timer¶
Timer channels are used to generate/capture a signal using a timer.
TimerChannel objects are created using the Timer.channel() method.
callback(**, value, priority=1, handler=None)
The behavior of this callback is heaviliy dependent on the operating mode of the timer channel:
- If mode is
Timer.PERIODICthe callback is executed periodically with the configured frequency or period.
- If mode is
Timer.ONE_SHOTthe callback is executed once when the configured timer expires.
- If mode is
Timer.EDGE_COUNTthe callback is executed when reaching the configured number of events (see
- If mode is
Timer.PWMthe callback is executed when reaching the duty cycle value.
The accepted params are:
prioritylevel of the interrupt. Can take values in the range 1-7. Higher values represent higher priorities.
handleris an optional function to be called when the interrupt is triggered.
valueis only valid when in
Timer.EDGE_COUNTmode and is used to set the number of edge events that will trigger the interrupt.
Returns a callback object.
- If mode is
Get or set the timer channel frequency (in Hz).
Get or set the timer channel period (in microseconds).
Get or set the timer channel current time value (in microseconds).
Get the number of edge events counted.
Get the time of ocurrance of the last event.
Get or set the duty cycle of the PWM signal (in the range of 0-100).