Adafruit ZeroTimer Library
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Adafruit_ZeroTimer Class Reference

Class that stores state and functions for interacting with SAMD21 or SAMD51 Timer Counter. More...

#include <Adafruit_ZeroTimer.h>

Classes

struct  counter_16_bit
 Helper struct to hold state for 16-bit configured TC. More...
 
struct  counter_32_bit
 Helper struct to hold state for 32-bit configured TC. More...
 
struct  counter_8_bit
 Helper struct to hold state for 8-bit configured TC. More...
 
struct  pwm_channel
 Helper struct to hold state for a PWM output channel. More...
 

Public Member Functions

 Adafruit_ZeroTimer (uint8_t tn)
 Instantiate a ZeroTimer class. More...
 
boolean PWMout (boolean pwmout, uint8_t channum, uint8_t pin)
 Use the TC to output a PWM signal on a given pin. Check datasheet to verify what pins can be connected to which TC output and what channel to use. More...
 
void setPeriodMatch (uint32_t period, uint32_t match, uint8_t channum=1)
 Set up channel 0 of the timer for the 'top' or period setting value, and channel 1 for the 'compare' value. More...
 
void enable (boolean en)
 Enable or disable the timer. Won't do anything if the status is what we desire already. More...
 
void configure (tc_clock_prescaler prescale, tc_counter_size countersize, tc_wave_generation wavegen, tc_count_direction countdir=TC_COUNT_DIRECTION_UP)
 Configure how we want to use the timer, based on ASF constants. More...
 
void setCompare (uint8_t channum, uint32_t compare)
 Set the timer counter's channel compare register to a value. More...
 
void invertWave (uint8_t invert)
 Whether or not to invert the output PWM. More...
 
void setCallback (boolean enable, tc_callback cb_type, void(*callback_func)(void)=NULL)
 Have a function called whenever the timer interrupt goes off. More...
 

Static Public Member Functions

static void timerHandler (uint8_t timerNum)
 The function we call from within the IRQ function defined in the sketch. We can't have all the IRQ functions in this library because then no other library can use them. So we have a handler that the usercode must call. More...
 

Protected Member Functions

bool tc_init ()
 Initializer for timer counter object. More...
 

Protected Attributes

uint8_t _timernum
 Which TC this is, 3 for TC3, 4 for TC4, etc.
 
Tc * _hw
 Pointer to the timer we're wrappering.
 
tc_clock_prescaler _clock_prescaler
 Prescale divider from timer clock source.
 
tc_counter_size _counter_size
 8, 16 or 32 bit counter size?
 
tc_wave_generation _wave_generation
 What sort of waveform we'll be creating.
 
uint8_t _waveform_invert_output
 Should we invert the output?
 
tc_count_direction _count_direction
 Which way the counter goes, up or down.
 
counter_8_bit _counter_8_bit
 
counter_16_bit _counter_16_bit
 
counter_32_bit _counter_32_bit
 
pwm_channel _pwm_channel [NUM_PWM_CHANNELS]
 

Detailed Description

Class that stores state and functions for interacting with SAMD21 or SAMD51 Timer Counter.

Constructor & Destructor Documentation

◆ Adafruit_ZeroTimer()

Adafruit_ZeroTimer::Adafruit_ZeroTimer ( uint8_t  timernum)

Instantiate a ZeroTimer class.

Parameters
timernumThe timer we are wrapping, 3 for TC3, 4 for TC4, etc!

Member Function Documentation

◆ PWMout()

boolean Adafruit_ZeroTimer::PWMout ( boolean  pwmout,
uint8_t  channum,
uint8_t  pin 
)

Use the TC to output a PWM signal on a given pin. Check datasheet to verify what pins can be connected to which TC output and what channel to use.

Parameters
pwmoutTrue to enable the output, False to disable
channumWhich channel, 0 or 1, we want to output on.
pinThe Arduino pin name we want to have the PWM output on
Returns
True on success

◆ setPeriodMatch()

void Adafruit_ZeroTimer::setPeriodMatch ( uint32_t  period,
uint32_t  match,
uint8_t  channum = 1 
)

Set up channel 0 of the timer for the 'top' or period setting value, and channel 1 for the 'compare' value.

Parameters
periodThe period register value to use (you'll need to calculate this from the frequency desired)
matchThe compare value, should be less than the period, determines the duty cycle
channumOnly for 8-bit counter mod, we can have the period set and two* channel outputs

◆ enable()

void Adafruit_ZeroTimer::enable ( boolean  en)

Enable or disable the timer. Won't do anything if the status is what we desire already.

Parameters
enTrue to enable, False to disable

◆ configure()

void Adafruit_ZeroTimer::configure ( tc_clock_prescaler  prescale,
tc_counter_size  countersize,
tc_wave_generation  wavegen,
tc_count_direction  countdir = TC_COUNT_DIRECTION_UP 
)

Configure how we want to use the timer, based on ASF constants.

Parameters
prescaleWhat divider we want, e.g. TC_CLOCK_PRESCALER_DIV16 see https://asf.microchip.com/docs/latest/thirdparty.wireless.avr2130_lwmesh.apps.wsndemo.saml21_xpro_b_rf233/html/group__asfdoc__sam0__tc__group.html#ga98aed17b995157e67b9322a45f0ed5f4
countersizeCan be TC_COUNTER_SIZE_8BIT, TC_COUNTER_SIZE_16BIT or TC_COUNTER_SIZE_32BIT
wavegenCan be TC_WAVE_WAVEGEN_NFRQ, TC_WAVE_WAVEGEN_MFRQ, TC_WAVE_WAVEGEN_NPWM or TC_WAVE_WAVEGEN_MPWM
countdirCan be TC_COUNT_DIRECTION_UP or TC_COUNT_DIRECTION_DOWN

◆ setCompare()

void Adafruit_ZeroTimer::setCompare ( uint8_t  channum,
uint32_t  compare 
)

Set the timer counter's channel compare register to a value.

Parameters
channumWhich channel to use, can be 0 or 1
compareThe compare value to set the channel compare value to, will be cast to whatever size the timer is setup for (8/16/32 bit)

◆ invertWave()

void Adafruit_ZeroTimer::invertWave ( uint8_t  invert)

Whether or not to invert the output PWM.

Parameters
invertTrue to invert, False to use default

◆ setCallback()

void Adafruit_ZeroTimer::setCallback ( boolean  enable,
tc_callback  cb_type,
void(*)(void)  callback_func = NULL 
)

Have a function called whenever the timer interrupt goes off.

Parameters
enableTrue to enable the callback, False to disable
cb_typeWhich channel to use, can be TC_CALLBACK_CC_CHANNEL0 or TC_CALLBACK_CC_CHANNEL1
callback_funcA function with no params or return, that will be called

◆ timerHandler()

void Adafruit_ZeroTimer::timerHandler ( uint8_t  timerNum)
static

The function we call from within the IRQ function defined in the sketch. We can't have all the IRQ functions in this library because then no other library can use them. So we have a handler that the usercode must call.

Parameters
timerNumThe timer we just got an IRQ for, 3 for TC3, 4 for TC4, etc!

◆ tc_init()

bool Adafruit_ZeroTimer::tc_init ( )
protected

Initializer for timer counter object.

Returns
True if we were able to init the timer, False on any failure

Member Data Documentation

◆ _counter_8_bit

counter_8_bit Adafruit_ZeroTimer::_counter_8_bit
protected

Stats for when we have the counter configed for 8 bit operation

◆ _counter_16_bit

counter_16_bit Adafruit_ZeroTimer::_counter_16_bit
protected

Stats for when we have the counter configed for 16 bit operation

◆ _counter_32_bit

counter_32_bit Adafruit_ZeroTimer::_counter_32_bit
protected

Stats for when we have the counter configed for 32 bit operation

◆ _pwm_channel

pwm_channel Adafruit_ZeroTimer::_pwm_channel[NUM_PWM_CHANNELS]
protected

status of the 2 PWM channels per timer


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