Jump to main content

##

# 3.31.23 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

Name | Description
--- | ---
TCx_CHy_TimerInitialize | Initializes given instance of TC channel
TCx_CHy_TimerStart | Starts the given TC channel counter
TCx_CHy_TimerStop | Stops the given TC channel counter
TCx_CHy_TimerCompareSet | Sets the compare value of a given timer channel
TCx_CHy_TimerPeriodSet | Sets the period value of a given timer channel
TCx_CHy_TimerPeriodGet | Reads the period value of given timer channel
TCx_CHy_TimerCounterGet | Reads the timer channel counter value
TCx_CHy_TimerFrequencyGet | Provides the given timer's counter-increment frequency
TCx_CHy_TimerPeriodHasExpired | Checks whether timer period is elapsed
TCx_CHy_TimerCallbackRegister | Registers the function to be called from interrupt

Data types and constants

Name | Type | Description
--- | --- | ---
TC_TIMER_STATUS | Enum | Identifies channel interrupt source mask
TC_TIMER_CALLBACK | Typedef | Defines 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

Name | Description
--- | ---
TCx_CHy_CompareInitialize | Initializes given TC channel in compare mode
TCx_CHy_CompareStart | Starts the given TC channel compare counter
TCx_CHy_CompareStop | Stops the given TC channel counter in compare mode
TCx_CHy_ComparePeriodSet | Sets the period value of a given TC channel in compare mode
TCx_CHy_ComparePeriodGet | Gets the period value of given timer channel in compare mode
TCx_CHy_CompareFrequencyGet | Provides the given timer's counter-increment frequency in compare mode
TCx_CHy_CompareCallbackRegister | Registers the function to be called from interrupt
TCx_CHy_CompareASet | Sets the Compare-A value of the given timer channel in compare mode
TCx_CHy_CompareBSet | Sets the Compare-B value of the given timer channel in compare mode
TCx_CHy_CompareStatusGet | Identifies status of the compare events

Data types and constants

Name | Type | Description
--- | --- | ---
TC_COMPARE_STATUS | Enum | Identifies channel interrupt source mask
TC_COMPARE_CALLBACK | Typedef | Defines 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

Name | Description
--- | ---
TCx_CHy_CaptureInitialize | Initializes given instance of TC channel to capture mode
TCx_CHy_CaptureStart | Starts the given TC channel capture counter
TCx_CHy_CaptureStop | Stops the given TC channel counter
TCx_CHy_CaptureCallbackRegister | Registers the function to be called from the capture-event
TCx_CHy_CaptureAGet | Returns the Capture-A value
TCx_CHy_CaptureBGet | Returns the Capture-B value
TCx_CHy_CaptureStatusGet | Identifies status of the capture events

Data types and constants

Name | Type | Description
--- | --- | ---
TC_CAPTURE_STATUS | Enum | Identifies channel interrupt source mask
TC_CAPTURE_CALLBACK | Typedef | Defines 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

Name | Description
--- | ---
TCx_QuadratureInitialize | Initializes given instance of TC channel in quadrature mode
TCx_QuadratureStart | Starts the given TC channel counter in quadrature mode
TCx_QuadratureStop | Stops the given TC channel counter in quadrature mode
TCx_QuadraturePositionGet | Reads the angular position from the quadrature encoder
TCx_QuadratureRevolutionsGet | Reads the number of revolutions from the quadrature encoder
TCx_QuadratureSpeedGet | Reads the quadrature index change speed
TCx_CHy_QuadratureStatusGet | Identifies status of the quadrature events
TCx_QuadratureCallbackRegister | Registers the function to be called from interrupt

Data types and constants

Name | Type | Description
--- | --- | ---
TC_QUADRATURE_STATUS | Enum | Identifies channel interrupt source mask
TC_QUADRATURE_CALLBACK | Typedef | Defines the function pointer data type and function signature for the tc channel callback function