class TimerWiPy – control hardware timers — MicroPython latest documentation (original) (raw)

This is the documentation for the latest development branch of MicroPython and may refer to features that are not available in released versions.

If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

Note

This class is a non-standard Timer implementation for the WiPy. It is available simply as machine.Timer on the WiPy but is named in the documentation below as machine.TimerWiPy to distinguish it from the more general machine.Timer class.

Hardware timers deal with timing of periods and events. Timers are perhaps the most flexible and heterogeneous kind of hardware in MCUs and SoCs, differently greatly from a model to a model. MicroPython’s Timer class defines a baseline operation of executing a callback with a given period (or once after some delay), and allow specific boards to define more non-standard behaviour (which thus won’t be portable to other boards).

See discussion of important constraints on Timer callbacks.

Note

Memory can’t be allocated inside irq handlers (an interrupt) and so exceptions raised within a handler don’t give much information. Seemicropython.alloc_emergency_exception_buf() for how to get around this limitation.

Constructors

class machine.TimerWiPy(id, ...)

Construct a new timer object of the given id. Id of -1 constructs a virtual timer (if supported by a board).

Methods

TimerWiPy.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

Keyword arguments:

TimerWiPy.deinit()

Deinitialises the timer. Stops the timer, and disables the timer peripheral.

TimerWiPy.channel(channel, **, freq, period, polarity=TimerWiPy.POSITIVE, duty_cycle=0)

If only a channel identifier passed, then a previously initialized channel object is returned (or None if there is no previous channel).

Otherwise, a TimerChannel object is initialized and returned.

The operating mode is the one configured to the Timer object that was used to create the channel.

Keyword only arguments:

Note

Either freq or period must be given, never both.

Note

When the channel is in PWM mode, the corresponding pin is assigned automatically, therefore there’s no need to assign the alternate function of the pin via the Pin class. The pins which support PWM functionality are the following:

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.

Methods

timerchannel.irq(*, trigger, priority=1, handler=None)

The behaviour of this callback is heavily dependent on the operating mode of the timer channel:

The accepted params are:

Returns a callback object.

timerchannel.freq([_value_])

Get or set the timer channel frequency (in Hz).

timerchannel.period([_value_])

Get or set the timer channel period (in microseconds).

timerchannel.duty_cycle([_value_])

Get or set the duty cycle of the PWM signal. It’s a percentage (0.00-100.00). Since the WiPy doesn’t support floating point numbers the duty cycle must be specified in the range 0-10000, where 10000 would represent 100.00, 5050 represents 50.50, and so on.

Constants

TimerWiPy.ONE_SHOT

TimerWiPy.PERIODIC

Timer operating mode.