FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
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).
 
using CALCULATOR = Calculator< NTIMER >
 The Calculator type for this Timer.
 
using PRESCALER = typename PRESCALERS_TRAIT::TYPE
 The enum type listing all available precaler values for this timer.
 

Public Member Functions

 Timer (const Timer< NTIMER_ > &)=delete
 
Timer< NTIMER_ > & operator= (const Timer< NTIMER_ > &)=delete
 
 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.
 
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.
 
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.
 
static constexpr const TYPE TIMER_MAX = TRAIT::MAX_COUNTER - 1
 The maximum value that can be set to this timer's counter.
 
static constexpr const TYPE PWM_MAX = TRAIT::MAX_PWM
 The maximum value that can be set to this timer's counter in PWM mode.
 
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.
 

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.

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 764 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:

timer.set_interrupts(TimerInterrupt::OVERFLOW | TimerInterrupt::INPUT_CAPTURE);
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 783 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:

timer.enable_interrupts(TimerInterrupt::OVERFLOW | TimerInterrupt::INPUT_CAPTURE);
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 802 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:

timer.disable_interrupts(TimerInterrupt::OVERFLOW | TimerInterrupt::INPUT_CAPTURE);
Parameters
interruptsinterrupts that will not be used when timer is started

Definition at line 820 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:

if (timer.are_interrupts_enabled(TimerInterrupt::OVERFLOW | TimerInterrupt::INPUT_CAPTURE)) ...
Parameters
interruptsinterrupts to be checked

Definition at line 836 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 850 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 874 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 899 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 917 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 926 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 946 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 964 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 985 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 1003 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 1015 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 1031 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 1046 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 1061 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 1077 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 1092 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 1106 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 1121 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 1136 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 1152 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 1167 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 1183 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 1198 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 1213 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 1230 of file timer.h.


The documentation for this class was generated from the following file:
timer
Defines all API to manipulate AVR Timers.
Definition: pulse_timer.h:116
timer::TimerInterrupt::OVERFLOW
@ OVERFLOW
This interrupt occurs when the counter overflows it maximum value.