Timer0 .. 4
Although the embedded object names are Timer0..4
, this page refers to them as TimerX
.
Methods
setup()
void setup()
Initializes the timer. This call allocates the required memory area.
begin()
void begin(uint16_t u16Hz, bool b_sw_int = true, bool b_pwm_out = false)
Starts the timer. The first parameter specifies the timer frequency in Hz. If the second parameter is set to true
, software interrupts are enabled. If the third parameter is set to true
, PWM output is enabled.
You can change the frequency with change_hz()
. This method allows more precise control than begin()
.
You can modify the PWM duty cycle using change_duty()
.
To describe the interrupt handler process, you need to define Application Behavior.
end()
void end()
Stops the timer operation.
available()
inline bool available()
Becomes true
in the loop()
immediately after a timer interrupt occurs, and turns false
after the loop()
finishes.
change_duty()
void change_duty(uint16_t duty, uint16_t duty_max = 1024)
Sets the duty cycle. The first parameter specifies the duty value (a smaller value brings the average waveform level closer to GND, while a larger value brings it closer to Vcc). The second parameter specifies the maximum duty value.
It is recommended to set duty_max
to one of 1024
, 4096
, or 16384
.
Although internal count value calculations involve division, these three values allow bit-shift operations instead. Other values result in heavier division calculations.
change_hz()
void change_hz(uint16_t hz, uint16_t mil = 0)
Sets the timer frequency. The second parameter specifies the fractional part (three decimal places) of the frequency as an integer. For example, to set 10.4 Hz, specify hz=10, mil=400
.