class TimerWiPy -- control hardware timers¶
注釈
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.
ハードウェアタイマーは、一定周期とイベントのタイミングで処理します。タイマーはおそらく MCU や SoC で最も柔軟で様々な種類のあるハードウェアであり、モデルごとに大きく異なります。MicroPython の Timer クラスは、指定した周期で(または少し経過後に1回だけ)コールバックを実行するベースライン操作を定義します。特定のボードがより標準的でない動作を定義できます(したがって他のボードへの移植性はありません)。
Timer のコールバックに関する 重要な制約 の議論を見てください。
注釈
irq ハンドラ(割り込み)内でメモリを割り当てることはできず、そのためにハンドラ内で発生した例外には多くの情報を持ちません。この制限を回避する方法については micropython.alloc_emergency_exception_buf() を参照してください。
コンストラクタ¶
-
class
machine.TimerWiPy(id, ...)¶ 指定した id の新しいタイマーオブジェクトを構築します。id が -1 の場合は、(ボードでサポートしていれば)仮想タイマーを構築します。
メソッド¶
-
TimerWiPy.init(mode, *, width=16)¶ タイマーを初期化します。たとえば次のように使います:
tim.init(Timer.PERIODIC) # periodic 16-bit timer tim.init(Timer.ONE_SHOT, width=32) # one shot 32-bit timer
キーワード引数:
modeは次のいずれかになります。TimerWiPy.ONE_SHOT- The timer runs once until the configured period of the channel expires.TimerWiPy.PERIODIC- The timer runs periodically at the configured frequency of the channel.TimerWiPy.PWM- Output a PWM signal on a pin.
widthmust 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 forONE_SHOTANDPERIODICmodes.
-
TimerWiPy.deinit()¶ タイマーを初期化解除します。タイマーを停止し、タイマーのペリフェラルを無効にします。
-
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
Noneif there is no previous channel).Otherwise, 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 eitherTIMER.A,TIMER.B. If the width is 32-bit then it must beTIMER.A | TIMER.B.
Keyword only arguments:
freqsets the frequency in Hz.periodsets the period in microseconds.
注釈
Either
freqorperiodmust be given, never both.polaritythis is applicable forPWM, and defines the polarity of the duty cycleduty_cycleonly applicable toPWM. 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.
注釈
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
Pinclass. The pins which support PWM functionality are the following:GP24on Timer 0 channel A.GP25on Timer 1 channel A.GP9on Timer 2 channel B.GP10on Timer 3 channel A.GP11on Timer 3 channel B.
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.
メソッド¶
-
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.PERIODICthe callback is executed periodically with the configured frequency or period. - If mode is
TimerWiPy.ONE_SHOTthe callback is executed once when the configured timer expires. - If mode is
TimerWiPy.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.triggermust beTimerWiPy.TIMEOUTwhen the operating mode is eitherTimerWiPy.PERIODICorTimerWiPy.ONE_SHOT. In the case that mode isTimerWiPy.PWMthen trigger must be equal toTimerWiPy.MATCH.
Returns a callback object.
- If mode is
-
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.