class TimerWiPy – control hardware timers 类TimerWiPy – 控制硬件计时器 ==========================================


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. 这个类是WiPy的非标准计时器实现。 它可以简单地称为’ ‘ machine。定时器’ ‘在WiPy上不过是命名在 下面的文档为’ ‘ machine。TimerWiPy ‘ ‘来区分它和 更普遍的:裁判:“机器。计时器<机器。计时器>”类。

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 behavior (which thus won’t be portable to other boards). 硬件计时器处理周期和事件的计时。计时器是也许 mcu和SoCs中最灵活、最异构的硬件 不同的模型有很大的不同。MicroPython定时器类的 定义在给定周期内执行回调的基线操作 (或在一段时间后),并允许特定的董事会定义更多 非标准行为(因此不能移植到其他板上)。

See discussion of important constraints on Timer callbacks. 参见:ref: ‘ important constraints <machine_callbacks> ‘上的讨论</machine_callbacks> 定时器回调。


Memory can’t be allocated inside irq handlers (an interrupt) and so exceptions raised within a handler don’t give much information. See micropython.alloc_emergency_exception_buf() for how to get around this limitation. 内存不能在irq处理程序(中断)中分配,因此 在处理程序中引发的异常不会提供太多信息。看到 :func:“micropython。alloc_emergency_exception_buf ‘来解决这个问题 限制。

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). 构造一个新的计时器对象,该对象的id为-1 虚拟计时器(如果由一个板支持)。

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: 关键字参数:

  • mode can be one of:

  • mode 可以是以下其中一种:

    • TimerWiPy.ONE_SHOT - The timer runs once until the configured period of the channel expires.

    • TimerWiPy.ONE_SHOT - 计时器运行一次,直到配置完成信道的期限到期。

    • TimerWiPy.PERIODIC - The timer runs periodically at the configured frequency of the channel.

    • TimerWiPy.PERIODIC -计时器定期在配置的位置运行频道的频率。

    • TimerWiPy.PWM - Output a PWM signal on a pin.

    • TimerWiPy.PWM - 输出一个PWM信号在一个针。

  • width must be either 16 or 32 (bits). For really low frequencies < 5Hz (or large periods), 32-bit timers should be used. 32-bit mode is only available for ONE_SHOT AND PERIODIC modes.

  • width``必须是16或32(位)。非常低的频率< 5Hz (或大周期),应该使用32位计时器。32位模式仅可用 用于``ONE_SHOT``PERIODIC``模式。


Deinitialises the timer. Stops the timer, and disables the timer peripheral. Deinitialises计时器。停止计时器,并禁用计时器外设。, \**, 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). 如果只有一个通道标识符通过,那么之前初始化的通道 对象被返回(如果之前没有通道,则返回``None``)。

Otherwise, a TimerChannel object is initialized and returned. 否则,将初始化并返回一个TimerChannel对象。

The operating mode is is the one configured to the Timer object that was used to create the channel. 操作模式是配置到计时器对象的模式创建通道。

  • channel if the width of the timer is 16-bit, then must be either TIMER.A, TIMER.B. If the width is 32-bit then it must be TIMER.A | TIMER.B.

  • channel 如果定时器的宽度是16位,那么必须是``TIMER.A``, TIMER.B

如果宽度是32位,那么它**must be** TIMER.A | TIMER.B

Keyword only arguments: 关键字参数:

  • freq sets the frequency in Hz.

  • freq 设置频率,单位是Hz。

  • period sets the period in microseconds.

  • period 设置以微秒为单位的周期。


Either freq or period must be given, never both. freq``period``必须给出,不能同时给出。

  • polarity this is applicable for PWM, and defines the polarity of the duty cycle

  • duty_cycle only applicable to PWM. 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.

  • polarity``这适用于``PWM,并定义了占空比的极性

  • ``duty_cycle``只适用于``PWM``这是一个百分比(0。000.00 -100。)自从WiPy

不支持浮点数,占空比必须在0-10000范围内指定, 其中10000代表100.00,5050代表50.50,以此类推。


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: 当信道处于PWM模式时,相应的引脚被自动分配,因此 没有必要通过 Pin 类分配pin的备用函数。的别针 支持PWM功能是以下:

  • GP24 on Timer 0 channel A.

  • GP25 on Timer 1 channel A.

  • GP9 on Timer 2 channel B.

  • GP10 on Timer 3 channel A.

  • GP11 on Timer 3 channel B.

class TimerChannel — setup a channel for a timer 类TimerChannel — 为计时器设置一个通道 ==================================================

Timer channels are used to generate/capture a signal using a timer. 定时器通道是用来产生/捕获一个信号使用定时器。

TimerChannel objects are created using the method. TimerChannel对象是使用方法创建的。

Methods 方法 ——-

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

The behavior of this callback is heavily dependent on the operating mode of the timer channel: 此回调的行为严重依赖于操作定时器通道模式:

  • If mode is TimerWiPy.PERIODIC the callback is executed periodically with the configured frequency or period.

  • 如果模式是``TimerWiPy.PERIODIC``这个回调是定期执行的 与配置的频率或周期。

  • If mode is TimerWiPy.ONE_SHOT the callback is executed once when the configured timer expires.

  • 如果模式是``TimerWiPy.ONE_SHOT``回调执行一次 配置的计时器到期。

  • If mode is TimerWiPy.PWM the callback is executed when reaching the duty cycle value.

  • 如果模式是``TimerWiPy.PWM``回调被执行时,达到职责周期值。

The accepted params are: 接受的参数是:

  • priority level of the interrupt. Can take values in the range 1-7. Higher values represent higher priorities.

  • priority 中断的级别。可以取范围为1-7的值。 值越高,优先级越高。

  • handler is an optional function to be called when the interrupt is triggered.

  • handler 是一个可选函数,当中断被触发时将被调用。

  • trigger must be TimerWiPy.TIMEOUT when the operating mode is either TimerWiPy.PERIODIC or TimerWiPy.ONE_SHOT. In the case that mode is TimerWiPy.PWM then trigger must be equal to TimerWiPy.MATCH.

  • trigger 必须是``TimerWiPy.TIMEOUT`` 当操作模式为``TimerWiPy.PERIODIC``或 TimerWiPy.ONE_SHOT。在这种情况下,模式是``TimerWiPy.PWM``然后触发器必须等于 TimerWiPy.MATCH

Returns a callback object. 返回一个回调对象。


Get or set the timer channel frequency (in Hz). 获取或设置定时器通道频率(赫兹)。


Get or set the timer channel period (in microseconds). 获取或设置计时器通道周期(以微秒为单位)。


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. 获取或设置PWM信号的占空比。这是一个百分比(0。000.00 -100。)自从WiPy 不支持浮点数,占空比必须在0-10000范围内指定, 其中10000代表100.00,5050代表50.50,以此类推。

Constants 常量 ———


Timer operating mode. 定时器的操作模式。