Jump to main content

##

# 2.120.1 Basic Timer Counter (TC)

A Timer Counter (TC) module can operate in any one of the below three modes. Timer mode - is used for periodic delay. Compare mode is used to generate waveforms on WO[0]and WO[1] Capture mode is used to measure delay, pulse width and frequency of the input signal. Capture trigger can be provided by input event line.

TIMER MODE

The TC in timer mode is used for periodic interrupt generation. This provides below features:

- Selectable 8/16/32 bit counter

- Interrupts/output events on:

- Counter overflow/underflow

- Compare match

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 (period is controlled by CC0 in MPWM waveform mode). 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(Application do not have to poll continuously)

Polling method

This example demonstrates how to check if timer period is expired by polling the status.

    int main(void)
    {

        /* Start the timer instance 3*/
        TC3_TimerStart();

        while (1)
        {
           while(! TC3_TimerPeriodHasExpired());
           LED_Toggle();
        }

    }

Callback method

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

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

    int main ( void )
    {
        /* Initialize all modules */
        SYS_Initialize ( NULL );

        /* Register callback function for TC3 period interrupt */
        TC3_TimerCallbackRegister(TC3_Callback_InterruptHandler, (uintptr_t)NULL);

        /* Start the timer*/
        TC3_TimerStart();

        while ( true )
        {
            /* Maintain state machines of all polled MPLAB Harmony modules. */
            SYS_Tasks ( );
        }

        /* Execution should not come here during normal operation */

        \return ( EXIT_FAILURE );
    }

COMPARE MODE

The TC channel in compare mode is used for waveform generation. This provides below features:

- Selectable 8/16/32 bit counter

- 2 compare/capture channels (CC)

- Waveform configurations

  - Normal Frequency - Period controlled by MAX (by PER in 8-bit mode)

  - Match Frequency - Period controlled by CC0 to generates 50% duty cycle waveform

  - Normal PWM - Period controlled by MAX (by PER in 8-bit mode) and duty controlled by CCx

  - Match PWM - Period controlled by CC0 and duty controlled by CC1

- Interrupts/output events on:

  - Counter overflow/underflow

  - Compare match

Using The Library

When using the TC and the Compare/Capture Value registers (CCx) for compare operations, the counter value is continuously compared to the values in the CCx registers. Output polarity can be reversed by inverting the output waveforms. This library provides below waveform types:

Name | Operation | TOP | Update | Output on match | Output on update
--- | --- | --- | --- | --- | ---
MFRQ (output on WO[0] ) | Match Frequency | CC0 | TOP/ZERO | Toggle | Stable
MPWM (output on WO[1] ) | Match PWM | CC0 | TOP/ZERO | Toggle | Toggle

In compare mode, overflow interrupt and compare match interrupts can be used to change the frequency or duty cycle of the waveform.

CAPTURE MODE

The TC channel in capture mode is used to measure pulse width, period, timestamp. A capture trigger can be provided by input event line TC_EV. This provides below features:

- Selectable 8/16/32 bit counter

- Input capture

  - Period captured in CC0 and pulse width in CC1

  - Period captured in CC1 and pulse width in CC0

- Interrupts/output events on:

  - Capture

- DMA support

Using The Library

Capture trigger can be provided by

- Input event line TC_EV

  - Only one event input line is available

  - This provides different capture event actions:

    - Pulse width in CC0 and period in CC1

    - Pulse width in CC1 and period in CC0

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 TC in capture mode to measure on time and period. CC0 captures on time and CC1 captures period of the input event. Event action is configured as PWP (Pulse width in CC0 and period in CC1).

    / * This function is called after Capture 0 interrupt  */
    void TC3_CaptureInterruptHandler(TC_CAPTURE_STATUS status, uintptr_t context)
    {
        / * Read Captured values  */
        on_time = TC3_Capture16bitChannel0Get();
        period = TC3_Capture16bitChannel1Get();
    }

    int main(void)
    {

        / * Register callback function for period interrupt  */
        TC3_CaptureCallbackRegister(TC3_CaptureInterruptHandler, (uintptr_t)NULL);

        / * Start the Capture  */
        TC3_CaptureStart();
    }

## Library Interface

Basic Timer Counter peripheral library provides the following interfaces:

Functions

Name | Description
--- | ---
TCx_TimerInitialize | Initializes given instance of TC
TCx_TimerStart | Starts the timer for given TC instance
TCx_TimerStop | Stops the timer for given TC instance
TCx_Timer8bitPeriodSet | Sets the period value of a given timer
TCx_Timer8bitPeriodGet | Reads the period value of given timer
TCx_Timer8bitCounterGet | Reads the timer current count value
TCx_Timer8bitCounterSet | Sets new timer counter value
TCx_Timer8bitCompareSet | Sets the compare value of a given timer
TCx_Timer16bitPeriodSet | Sets the period value of a given timer
TCx_Timer16bitPeriodGet | Reads the period value of given timer
TCx_Timer16bitCounterGet | Reads the timer current count value
TCx_Timer16bitCounterSet | Sets new timer counter value
TCx_Timer16bitCompareSet | Sets the compare value of a given timer
TCx_Timer32bitPeriodSet | Sets the period value of a given timer
TCx_Timer32bitPeriodGet | Reads the period value of given timer
TCx_Timer32bitCounterGet | Reads the timer current count value
TCx_Timer32bitCounterSet | Sets new timer counter value
TCx_Timer32bitCompareSet | Sets the compare value of a given timer
TCx_TimerPeriodHasExpired | Checks whether timer period is elapsed
TCx_TimerFrequencyGet | Provides the given timer's counter-increment frequency
TCx_TimerCallbackRegister | Registers the function to be called from interrupt
TCx_CompareInitialize | Initializes given instance of TC
TCx_CompareStart | Starts the timer for given TC instance
TCx_CompareStop | Stops the timer for given TC instance
TCx_Compare8bitPeriodSet | Sets the period value of a given timer
TCx_Compare8bitPeriodGet | Reads the period value of given timer
TCx_Compare8bitCounterGet | Reads the timer current count value
TCx_Compare8bitCounterSet | Sets new timer counter value
TCx_Compare8bitMatch0Set | Writes compare value of the given compare channel 0
TCx_Compare8bitMatch1Set | Writes compare value of the given compare channel 1
TCx_Compare16bitPeriodSet | Sets the period value of a given timer
TCx_Compare16bitPeriodGet | Reads the period value of given timer
TCx_Compare16bitCounterGet | Reads the timer current count value
TCx_Compare16bitCounterSet | Sets new timer counter value
TCx_Compare16bitMatch0Set | Writes compare value of the given compare channel 0
TCx_Compare16bitMatch1Set | Writes compare value of the given compare channel 1
TCx_Compare32bitPeriodSet | Sets the period value of a given timer
TCx_Compare32bitPeriodGet | Reads the period value of given timer
TCx_Compare32bitCounterGet | Reads the timer current count value
TCx_Compare32bitCounterSet | Sets new timer counter value
TCx_Compare32bitMatch0Set | Writes compare value of the given compare channel 0
TCx_Compare32bitMatch1Set | Writes compare value of the given compare channel 1
TCx_CompareFrequencyGet | Provides the given timer's counter-increment frequency
TCx_CompareCallbackRegister | Registers the function to be called from interrupt
TCx_CompareStatusGet | Reads status of compare operation
TCx_CaptureInitialize | Initializes given instance of TC
TCx_CaptureStart | Starts the timer for given TC instance
TCx_CaptureStop | Stops the timer for given TC instance
TCx_Capture8bitChannel0Get | Reads capture value from channel 0
TCx_Capture16bitChannel0Get | Reads capture value from channel 0
TCx_Capture32bitChannel0Get | Reads capture value from channel 0
TCx_Capture8bitChannel1Get | Reads capture value from channel 1
TCx_Capture16bitChannel1Get | Reads capture value from channel 1
TCx_Capture32bitChannel1Get | Reads capture value from channel 1
TCx_CaptureStatusGet | Reads status capture operation
TCx_CaptureCallbackRegister | Registers the function to be called from interrupt

Data types and constants

Name | Type | Description
--- | --- | ---
TC_TIMER_STATUS | Typedef | Identifies channel interrupt source mask
TC_TIMER_CALLBACK | Typedef | Defines the function pointer data type and function signature for the TC channel callback function
TCx_COMPARE_STATUS | Typedef | Identifies channel interrupt source mask
TCx_COMPARE_CALLBACK | Typedef | Defines the function pointer data type and function signature for the tc channel callback function
TCx_CAPTURE_STATUS | Typedef | Identifies channel interrupt source mask
TCx_CAPTURE_CALLBACK | Typedef | Defines the function pointer data type and function signature for the tc channel callback function

Note: Not all APIs maybe implemented. See the specific device family section for available APIs.