3.41.21 Timer Counter (TC)

A Timer Counter (TC) module includes three identical TC channels. All channels of the Timer Counter are independent and identical in operation except when the quadrature encode mode is enabled. Timer counter module can be configured to operate in one of the following modes:

  • Timer mode used for periodic interrupt generation

  • Compare mode used for generating PWM signals

  • Capture mode used to perform measurements such as pulse timing, frequency, period, duty cycle and phase

  • Quadrature encoder mode used to measure the position and speed of the motor

Timer mode

The TC channel in timer mode is used for periodic interrupt generation.

Using The Library

When the timer is enabled, it increments by one on every rising edge of the input clock, and generates an interrupt on a period match (Timer expiry period). Timer mode is used for periodic interrupt generation.

It provides both polling and callback methods to indicate period match has occurred.

  • With polling, the application will need to continuously poll to check if the timer has expired.

  • With callback, the registered callback function will be called when the timer expires. This means the application do not have to poll continuously.

Callback method

This example demonstrates how to use TC channel in timer mode to generate periodic callback.

/* This function is called after period expires */
void TC0_CH0_TimerInterruptHandler(TC_TIMER_STATUS status, uintptr_t context)
{
    /* Toggle LED */
    LED_Toggle();
}

int main(void)
{

    /* Register callback function for CH0 period interrupt */
    TC0_CH0_TimerCallbackRegister(TC0_CH0_TimerInterruptHandler, (uintptr_t)NULL);

    /* Start the timer channel 0*/
    TC0_CH0_TimerStart();
}

Library Interface

Timer Counter peripheral library operating in timer mode provides the following interfaces:

Functions

NameDescription
TCx_CHy_TimerInitializeInitializes given instance of TC channel
TCx_CHy_TimerStartStarts the given TC channel counter
TCx_CHy_TimerStopStops the given TC channel counter
TCx_CHy_TimerCompareSetSets the compare value of a given timer channel
TCx_CHy_TimerPeriodSetSets the period value of a given timer channel
TCx_CHy_TimerPeriodGetReads the period value of given timer channel
TCx_CHy_TimerCounterGetReads the timer channel counter value
TCx_CHy_TimerFrequencyGetProvides the given timer's counter-increment frequency
TCx_CHy_TimerPeriodHasExpiredChecks whether timer period is elapsed
TCx_CHy_TimerCallbackRegisterRegisters the function to be called from interrupt

Data types and constants

NameTypeDescription
TC_TIMER_STATUSEnumIdentifies channel interrupt source mask
TC_TIMER_CALLBACKTypedefDefines the function pointer data type and function signature for the tc channel callback function

Compare mode

The TC channel in compare mode generates one or two PWM signals with the same frequency and independently programmable duty cycles, or generates different types of one-shot or repetitive pulses.

Using The Library

In this mode, TIOAx is configured as an output. TIOBx is configured as an output, if it is not used as an external event.

Functions

NameDescription
TCx_CHy_CompareInitializeInitializes given TC channel in compare mode
TCx_CHy_CompareStartStarts the given TC channel compare counter
TCx_CHy_CompareStopStops the given TC channel counter in compare mode
TCx_CHy_ComparePeriodSetSets the period value of a given TC channel in compare mode
TCx_CHy_ComparePeriodGetGets the period value of given timer channel in compare mode
TCx_CHy_CompareFrequencyGetProvides the given timer's counter-increment frequency in compare mode
TCx_CHy_CompareCallbackRegisterRegisters the function to be called from interrupt
TCx_CHy_CompareASetSets the Compare-A value of the given timer channel in compare mode
TCx_CHy_CompareBSetSets the Compare-B value of the given timer channel in compare mode
TCx_CHy_CompareStatusGetIdentifies status of the compare events

Data types and constants

NameTypeDescription
TC_COMPARE_STATUSEnumIdentifies channel interrupt source mask
TC_COMPARE_CALLBACKTypedefDefines the function pointer data type and function signature for the tc channel callback function

Capture mode

The TC channel in capture mode is used to measure pulse time, frequency, period and duty cycle.

Using The Library

In Capture mode, TIOAx and TIOBx are configured as inputs. The counter value is captured in the buffer on the selected input edge and it is used as a timestamp for the event. The user can select up to two input events and the respective timestamp is captured in Capture A and Capture B buffers, it can be used to measure pulse, period, frequency and duty cycle of the external input.

It provides both polling and callback methods to indicate capture event has occurred.

  • With polling, the application will need to continuously poll to check if the capture event has occurred

  • With callback, the registered callback function will be called when the capture event occurs. This means the application do not have to poll continuously

Callback method

This example demonstrates how to use the TC channel in capture mode to measure off time and period. The rising edge is captured in Capture A and Falling edge is captured in Capture B register, and then the timer is reset.

/* This function is called after Capture B */
void TC0_CH0_CaptureInterruptHandler(TC_CAPTURE_STATUS status, uintptr_t context)
{
    /* Read Captured values */
    off_time = TC0_CH0_CaptureAGet();
    period = TC0_CH0_CaptureBGet();
}

int main(void)
{

    /* Register callback function for CH0 period interrupt */
    TC0_CH0_CaptureCallbackRegister(TC0_CH0_CaptureInterruptHandler, (uintptr_t)NULL);

    /* Start the Capture channel 0*/
    TC0_CH0_CaptureStart();
}

Functions

NameDescription
TCx_CHy_CaptureInitializeInitializes given instance of TC channel to capture mode
TCx_CHy_CaptureStartStarts the given TC channel capture counter
TCx_CHy_CaptureStopStops the given TC channel counter
TCx_CHy_CaptureCallbackRegisterRegisters the function to be called from the capture-event
TCx_CHy_CaptureAGetReturns the Capture-A value
TCx_CHy_CaptureBGetReturns the Capture-B value
TCx_CHy_CaptureStatusGetIdentifies status of the capture events

Data types and constants

NameTypeDescription
TC_CAPTURE_STATUSEnumIdentifies channel interrupt source mask
TC_CAPTURE_CALLBACKTypedefDefines the function pointer data type and function signature for the tc channel callback function

Quadrature decoder mode

The quadrature decoder (QDEC) mode is used to measure the position and speed of the motor

Using The Library

The TC embeds a Quadrature Decoder (QDEC) connected in front of the timers and driven by TIOA0, TIOB0 and TIOB1 inputs. When enabled, the QDEC performs the input lines filtering, decoding of quadrature signals and connects to the timers and counters in order to read the position and speed of the motor through the user interface.

Functions

NameDescription
TCx_QuadratureInitializeInitializes given instance of TC channel in quadrature mode
TCx_QuadratureStartStarts the given TC channel counter in quadrature mode
TCx_QuadratureStopStops the given TC channel counter in quadrature mode
TCx_QuadraturePositionGetReads the angular position from the quadrature encoder
TCx_QuadratureRevolutionsGetReads the number of revolutions from the quadrature encoder
TCx_QuadratureSpeedGetReads the quadrature index change speed
TCx_CHy_QuadratureStatusGetIdentifies status of the quadrature events
TCx_QuadratureCallbackRegisterRegisters the function to be called from interrupt

Data types and constants

NameTypeDescription
TC_QUADRATURE_STATUSEnumIdentifies channel interrupt source mask
TC_QUADRATURE_CALLBACKTypedefDefines the function pointer data type and function signature for the tc channel callback function