Timer0 .. 4
Timers, PWM (mwx::periph_timer)
The timer has two functions: to generate a software interrupt at a specified period, and to output a PWM at a specified period. 5 timers in total are available in the TWELITE radio module, from 0...4.
The name of the built-in object is Timer0..4
, but will be referred to as TimerX
on this page.
Methods
setup()
Initializes the timer. This call allocates the necessary memory space.
begin()
Starts a timer; the first parameter is the period of the timer in Hz; setting the second parameter to true
enables software interrupts; setting the third parameter to true
true` enables PWM output.
You can change the frequency with change_hz()
. The change_hz()
allows more detailed specification than the begin()
specification.
The duty ratio of PWM output can be changed with change_duty()
.
To describe the processing of the interrupt handler, specify it in the application behavior definition.
end()
Stops the timer operation.
available()
It becomes true
at loop()
immediately after a timer interrupt occurs, and becomes false
when loop()
ends.
change_duty()
Set the duty ratio. the first parameter specifies the duty ratio (a small value makes the average of the waveform closer to the GND level, a large value makes it closer to the Vcc level). the second parameter specifies the maximum duty ratio value of the duty ratio.
It is recommended that duty_max
be one of 1024,4096,16384
.
The internal calculation of the count value involves division, but only for these three values is bit shifting used, while for other values, a computationally expensive division process is performed.
change_hz()
Sets the frequency of the timer; the second parameter is an integer with three decimal places for the frequency. For example, to set the frequency to 10.4 Hz, specify hz=10, mil=400
.
最終更新