FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
timer::Timer< NTIMER_ > Class Template Reference

General API to handle an AVR timer. More...

#include <fastarduino/timer.h>

Inheritance diagram for timer::Timer< NTIMER_ >:

Public Types

using TYPE = typename TRAIT::TYPE
 The type of this timer's counter (either uint8_t or uint16_t). More...
 
using CALCULATOR = Calculator< NTIMER >
 The Calculator type for this Timer. More...
 
using PRESCALER = typename PRESCALERS_TRAIT::TYPE
 The enum type listing all available precaler values for this timer. More...
 

Public Member Functions

 Timer (TimerMode timer_mode, PRESCALER prescaler, TimerInterrupt interrupts=TimerInterrupt(0))
 Construct a new Timer handler and initialize its mode. More...
 
void set_interrupts (TimerInterrupt interrupts=TimerInterrupt(0))
 Set the list of interrupts that must be triggered by this timer. More...
 
void enable_interrupts (TimerInterrupt interrupts)
 Add new interrupts to be enabled to the current list of interrupts that must be triggered by this timer. More...
 
void disable_interrupts (TimerInterrupt interrupts)
 Remove interrupts from the current list of enabled interrupts triggered by this timer. More...
 
bool are_interrupts_enabled (TimerInterrupt interrupts) const
 Test if interrupts are currently enabled for this timers. More...
 
void set_input_capture (TimerInputCapture input_capture)
 Set the input capture mode for this timer. More...
 
TimerInputCapture input_capture () const
 Return the current TimerInputCapture used by this timer. More...
 
void set_capture_noise_canceller (bool cancel_noise)
 Set or clear the noise canceller for input capture on this timer. More...
 
bool has_capture_noise_canceller () const
 Tell whether noise canceller for this timer's input capture is active or not. More...
 
void set_timer_mode (TimerMode timer_mode)
 Change timer mode. More...
 
TimerMode get_timer_mode () const
 Get current time mode. More...
 
void set_prescaler (PRESCALER prescaler)
 Change prescaler for this timer. More...
 
void begin (TYPE max=0)
 Start this timer in the currently selected mode, with the provided prescaler value and max value. More...
 
void begin_ (TYPE max=0)
 Start this timer in the currently selected mode, with the provided prescaler value and max value. More...
 
void reset (TYPE ticks=0)
 Reset current counter to ticks . More...
 
void reset_ (TYPE ticks=0)
 Reset current counter to ticks . More...
 
TYPE ticks ()
 Return the current counter value for this timer. More...
 
TYPE ticks_ ()
 Return the current counter value for this timer. More...
 
void suspend_interrupts ()
 Temporarily suspend this timer: the timer does not generate any interrupt any longer. More...
 
void suspend_interrupts_ ()
 Temporarily suspend this timer: the timer does not generate any interrupt any longer. More...
 
void resume_interrupts ()
 Resume this timer if it was previously suspended: the timer's counter is reset and the timer starts generating interrupts again. More...
 
void resume_interrupts_ ()
 Resume this timer if it was previously suspended: the timer's counter is reset and the timer starts generating interrupts again. More...
 
bool is_interrupt_suspended ()
 Check if this timer interrupts are currently suspended, i.e. More...
 
void suspend_timer ()
 Suspend this timer, ie stop this timer counting (ticks() will not change then). More...
 
void suspend_timer_ ()
 Suspend this timer, ie stop this timer counting (ticks() will not change then). More...
 
void resume_timer ()
 Resume this timer, ie restart counting (ticks() will start changing again). More...
 
void resume_timer_ ()
 Resume this timer, ie restart counting (ticks() will start changing again). More...
 
void end ()
 Completely stop this timer: timer interrupts are disabled and counter is stopped. More...
 
void end_ ()
 Completely stop this timer: timer interrupts are disabled and counter is stopped. More...
 
template<uint8_t COM>
void set_output_mode (TimerOutputMode mode)
 Change the output mode for this timer; this enables connection between this timer to one of its associated digital output pins. More...
 
template<uint8_t COM>
void set_max (TYPE max)
 Change the maximum value for this timer, in relation to one of its associated digital output pins. More...
 

Static Public Attributes

static constexpr const board::Timer NTIMER = NTIMER_
 The Board timer used by this Timer. More...
 
static constexpr const TYPE TIMER_MAX = TRAIT::MAX_COUNTER - 1
 The maximum value that can be set to this timer's counter. More...
 
static constexpr const TYPE PWM_MAX = TRAIT::MAX_PWM
 The maximum value that can be set to this timer's counter in PWM mode. More...
 
static constexpr const board::DigitalPin ICP_PIN = TRAIT::ICP_PIN
 The pin that is used for Input Capture feature, if supported by this timer, or board::DigitalPin::NONE if not. More...
 

Detailed Description

template<board::Timer NTIMER_>
class timer::Timer< NTIMER_ >

General API to handle an AVR timer.

Note that many timer usages will require ISR registration with one of the macros defined in this header file.

Template Parameters
NTIMER_the AVR timer to use for this Timer
See also
board::Timer

Definition at line 704 of file timer.h.

Member Typedef Documentation

◆ TYPE

template<board::Timer NTIMER_>
using timer::Timer< NTIMER_ >::TYPE = typename TRAIT::TYPE

The type of this timer's counter (either uint8_t or uint16_t).

Definition at line 725 of file timer.h.

◆ CALCULATOR

template<board::Timer NTIMER_>
using timer::Timer< NTIMER_ >::CALCULATOR = Calculator<NTIMER>

The Calculator type for this Timer.

Definition at line 730 of file timer.h.

◆ PRESCALER

template<board::Timer NTIMER_>
using timer::Timer< NTIMER_ >::PRESCALER = typename PRESCALERS_TRAIT::TYPE

The enum type listing all available precaler values for this timer.

Definition at line 735 of file timer.h.

Constructor & Destructor Documentation

◆ Timer()

template<board::Timer NTIMER_>
timer::Timer< NTIMER_ >::Timer ( TimerMode  timer_mode,
PRESCALER  prescaler,
TimerInterrupt  interrupts = TimerInterrupt(0) 
)
inline

Construct a new Timer handler and initialize its mode.

Note this constructor does not start the timer.

Parameters
timer_modethe mode to initalize this timer with
prescalerthe prescale enum value to use for this timer
interruptsdefault interrupts that will be used when timer is started; note that some interrupts are not supported by all timers, if used here, they will silently be ignored.
See also
begin()
set_timer_mode()
set_interrupts()
set_prescaler()

Definition at line 766 of file timer.h.

Member Function Documentation

◆ set_interrupts()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::set_interrupts ( TimerInterrupt  interrupts = TimerInterrupt(0))
inline

Set the list of interrupts that must be triggered by this timer.

If you do not want any interrupt triggered, then call this method with no argument. To select several interrupts, just "or" them together:

Defines all API to manipulate AVR Timers.
Definition: pulse_timer.h:116
@ OVERFLOW
This interrupt occurs when the counter overflows it maximum value.
@ INPUT_CAPTURE
This interrupt occurs during input capture, i.e.
Parameters
interruptsinterrupts that will be used when timer is started; note that some interrupts are not supported by all timers, if used here, they will silently be ignored.

Definition at line 785 of file timer.h.

◆ enable_interrupts()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::enable_interrupts ( TimerInterrupt  interrupts)
inline

Add new interrupts to be enabled to the current list of interrupts that must be triggered by this timer.

To select several interrupts, just "or" them together:

Parameters
interruptsadditional interrupts that will be used when timer is started; note that some interrupts are not supported by all timers, if used here, they will silently be ignored.

Definition at line 804 of file timer.h.

◆ disable_interrupts()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::disable_interrupts ( TimerInterrupt  interrupts)
inline

Remove interrupts from the current list of enabled interrupts triggered by this timer.

To select several interrupts to be disabled, just "or" them together:

Parameters
interruptsinterrupts that will not be used when timer is started

Definition at line 822 of file timer.h.

◆ are_interrupts_enabled()

template<board::Timer NTIMER_>
bool timer::Timer< NTIMER_ >::are_interrupts_enabled ( TimerInterrupt  interrupts) const
inline

Test if interrupts are currently enabled for this timers.

To select several interrupts to be tested, just "or" them together:

Parameters
interruptsinterrupts to be checked

Definition at line 838 of file timer.h.

◆ set_input_capture()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::set_input_capture ( TimerInputCapture  input_capture)
inline

Set the input capture mode for this timer.

Input Capture will work only if set_interrupts() is called with TimerInterrupt::INPUT_CAPTURE. Note that some timers do not support input capture; in this situation, using this method will generate a compiler error.

Parameters
input_capturenew input capture mode to use

Definition at line 852 of file timer.h.

◆ input_capture()

template<board::Timer NTIMER_>
TimerInputCapture timer::Timer< NTIMER_ >::input_capture ( ) const
inline

Return the current TimerInputCapture used by this timer.

Definition at line 863 of file timer.h.

◆ set_capture_noise_canceller()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::set_capture_noise_canceller ( bool  cancel_noise)
inline

Set or clear the noise canceller for input capture on this timer.

Note that some timers do not support input capture; in this situation, using this method will generate a compiler error.

Parameters
cancel_noiseif true, set noise canceller for this timer's input capture; if false, clear noise canceller.

Definition at line 876 of file timer.h.

◆ has_capture_noise_canceller()

template<board::Timer NTIMER_>
bool timer::Timer< NTIMER_ >::has_capture_noise_canceller ( ) const
inline

Tell whether noise canceller for this timer's input capture is active or not.

Definition at line 891 of file timer.h.

◆ set_timer_mode()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::set_timer_mode ( TimerMode  timer_mode)
inline

Change timer mode.

Parameters
timer_modethe mode to reset this timer to

Definition at line 901 of file timer.h.

◆ get_timer_mode()

template<board::Timer NTIMER_>
TimerMode timer::Timer< NTIMER_ >::get_timer_mode ( ) const
inline

Get current time mode.

This method exists only for the sake of completeness; if not really needed, do not use it, as it will generate extra code with no real added value.

Definition at line 919 of file timer.h.

◆ set_prescaler()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::set_prescaler ( PRESCALER  prescaler)
inline

Change prescaler for this timer.

Parameters
prescalerthe prescale enum value to use for this timer

Definition at line 928 of file timer.h.

◆ begin()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::begin ( TYPE  max = 0)
inline

Start this timer in the currently selected mode, with the provided prescaler value and max value.

This method enables timer interrupts (Compare Match),hence ISR registration is required. Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use begin_() instead.

Parameters
maxthe maximum value to use for this timer's counter
See also
end()
begin_(TYPE)

Definition at line 948 of file timer.h.

◆ begin_()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::begin_ ( TYPE  max = 0)
inline

Start this timer in the currently selected mode, with the provided prescaler value and max value.

This method enables timer interrupts (Compare Match),hence ISR registration is required. Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use begin() instead.

Parameters
maxthe maximum value to use for this timer's counter
See also
end_()
begin(TYPE)

Definition at line 966 of file timer.h.

◆ reset()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::reset ( TYPE  ticks = 0)
inline

Reset current counter to ticks .

This method is synchronized if needed (i.e. if this timer is 16 bits). If you do not need synchronization, then you should better use reset_() instead.

Parameters
ticksnumber of ticks to reset counter to
See also
reset_()

Definition at line 987 of file timer.h.

◆ reset_()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::reset_ ( TYPE  ticks = 0)
inline

Reset current counter to ticks .

Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use reset() instead.

Parameters
ticksnumber of ticks to reset counter to
See also
reset()

Definition at line 1005 of file timer.h.

◆ ticks()

template<board::Timer NTIMER_>
TYPE timer::Timer< NTIMER_ >::ticks ( )
inline

Return the current counter value for this timer.

This method is synchronized if needed (i.e. if this timer is 16 bits). If you do not need synchronization, then you should better use ticks_() instead.

See also
ticks_()

Definition at line 1017 of file timer.h.

◆ ticks_()

template<board::Timer NTIMER_>
TYPE timer::Timer< NTIMER_ >::ticks_ ( )
inline

Return the current counter value for this timer.

Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use ticks() instead.

See also
ticks()

Definition at line 1033 of file timer.h.

◆ suspend_interrupts()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::suspend_interrupts ( )
inline

Temporarily suspend this timer: the timer does not generate any interrupt any longer.

Note that this method is synchronized, i.e. it disables AVR interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use suspend_interrupts_() instead.

See also
resume_interrupts()
suspend_interrupts_()

Definition at line 1048 of file timer.h.

◆ suspend_interrupts_()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::suspend_interrupts_ ( )
inline

Temporarily suspend this timer: the timer does not generate any interrupt any longer.

Note that this method is not synchronized, hence you should ensure it is called only while AVR interrupts are not enabled. If you need synchronization, then you should better use suspend_interrupts() instead.

See also
resume_interrupts_()
suspend_interrupts()

Definition at line 1063 of file timer.h.

◆ resume_interrupts()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::resume_interrupts ( )
inline

Resume this timer if it was previously suspended: the timer's counter is reset and the timer starts generating interrupts again.

Note that this method is synchronized, i.e. it disables AVR interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use resume_interrupts_() instead.

See also
suspend_interrupts()
resume_interrupts_()

Definition at line 1079 of file timer.h.

◆ resume_interrupts_()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::resume_interrupts_ ( )
inline

Resume this timer if it was previously suspended: the timer's counter is reset and the timer starts generating interrupts again.

Note that this method is not synchronized, hence you should ensure it is called only while AVR interrupts are not enabled. If you need synchronization, then you should better use resume_interrupts() instead.

See also
suspend_interrupts_()
resume_interrupts()

Definition at line 1094 of file timer.h.

◆ is_interrupt_suspended()

template<board::Timer NTIMER_>
bool timer::Timer< NTIMER_ >::is_interrupt_suspended ( )
inline

Check if this timer interrupts are currently suspended, i.e.

it does not generate any interrupts.

Return values
truethe timer is currently suspended
falsethe timer is currently active

Definition at line 1108 of file timer.h.

◆ suspend_timer()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::suspend_timer ( )
inline

Suspend this timer, ie stop this timer counting (ticks() will not change then).

Note that this method is synchronized, i.e. it disables AVR interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use suspend_timer_() instead.

See also
resume_timer()
suspend_timer_()

Definition at line 1123 of file timer.h.

◆ suspend_timer_()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::suspend_timer_ ( )
inline

Suspend this timer, ie stop this timer counting (ticks() will not change then).

Note that this method is not synchronized, hence you should ensure it is called only while AVR interrupts are not enabled. If you need synchronization, then you should better use suspend_timer() instead.

See also
resume_timer_()
suspend_timer()

Definition at line 1138 of file timer.h.

◆ resume_timer()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::resume_timer ( )
inline

Resume this timer, ie restart counting (ticks() will start changing again).

Note that this method is synchronized, i.e. it disables AVR interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use resume_timer_() instead.

See also
suspend_timer()
resume_timer_()

Definition at line 1154 of file timer.h.

◆ resume_timer_()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::resume_timer_ ( )
inline

Resume this timer, ie restart counting (ticks() will start changing again).

Note that this method is not synchronized, hence you should ensure it is called only while AVR interrupts are not enabled. If you need synchronization, then you should better use resume_timer() instead.

See also
suspend_timer_()
resume_timer()

Definition at line 1169 of file timer.h.

◆ end()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::end ( )
inline

Completely stop this timer: timer interrupts are disabled and counter is stopped.

Note that this method is synchronized, i.e. it disables interrupts during its call and restores interrupts on return. If you do not need synchronization, then you should better use end_() instead.

See also
begin()
end_()

Definition at line 1185 of file timer.h.

◆ end_()

template<board::Timer NTIMER_>
void timer::Timer< NTIMER_ >::end_ ( )
inline

Completely stop this timer: timer interrupts are disabled and counter is stopped.

Note that this method is not synchronized, hence you should ensure it is called only while interrupts are not enabled. If you need synchronization, then you should better use end() instead.

See also
begin_()
end()

Definition at line 1200 of file timer.h.

◆ set_output_mode()

template<board::Timer NTIMER_>
template<uint8_t COM>
void timer::Timer< NTIMER_ >::set_output_mode ( TimerOutputMode  mode)
inline

Change the output mode for this timer; this enables connection between this timer to one of its associated digital output pins.

Template Parameters
COMthe index of the output pin for this timer; your program will not compile if you use an incorrect value.
Parameters
modethe new output mode for this timer and this pin

Definition at line 1215 of file timer.h.

◆ set_max()

template<board::Timer NTIMER_>
template<uint8_t COM>
void timer::Timer< NTIMER_ >::set_max ( TYPE  max)
inline

Change the maximum value for this timer, in relation to one of its associated digital output pins.

Note that this method is always synchronized, i.e. it disables interrupts during its call and restores interrupts on return.

Template Parameters
COMthe index of the output pin for this timer; your program will not compile if you use an incorrect value.
Parameters
maxthe new maximum value for this timer and this pin

Definition at line 1232 of file timer.h.

Member Data Documentation

◆ NTIMER

template<board::Timer NTIMER_>
constexpr const board::Timer timer::Timer< NTIMER_ >::NTIMER = NTIMER_
staticconstexpr

The Board timer used by this Timer.

Definition at line 708 of file timer.h.

◆ TIMER_MAX

template<board::Timer NTIMER_>
constexpr const TYPE timer::Timer< NTIMER_ >::TIMER_MAX = TRAIT::MAX_COUNTER - 1
staticconstexpr

The maximum value that can be set to this timer's counter.

Definition at line 740 of file timer.h.

◆ PWM_MAX

template<board::Timer NTIMER_>
constexpr const TYPE timer::Timer< NTIMER_ >::PWM_MAX = TRAIT::MAX_PWM
staticconstexpr

The maximum value that can be set to this timer's counter in PWM mode.

Definition at line 745 of file timer.h.

◆ ICP_PIN

template<board::Timer NTIMER_>
constexpr const board::DigitalPin timer::Timer< NTIMER_ >::ICP_PIN = TRAIT::ICP_PIN
staticconstexpr

The pin that is used for Input Capture feature, if supported by this timer, or board::DigitalPin::NONE if not.

Definition at line 751 of file timer.h.


The documentation for this class was generated from the following file: