2.113.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:

NameOperationTOPUpdateOutput on matchOutput on update
MFRQ (output on WO[0] )Match FrequencyCC0TOP/ZEROToggleStable
MPWM (output on WO[1] )Match PWMCC0TOP/ZEROToggleToggle

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

NameDescription
TCx_TimerInitializeInitializes given instance of TC
TCx_TimerStartStarts the timer for given TC instance
TCx_TimerStopStops the timer for given TC instance
TCx_Timer8bitPeriodSetSets the period value of a given timer
TCx_Timer8bitPeriodGetReads the period value of given timer
TCx_Timer8bitCounterGetReads the timer current count value
TCx_Timer8bitCounterSetSets new timer counter value
TCx_Timer8bitCompareSetSets the compare value of a given timer
TCx_Timer16bitPeriodSetSets the period value of a given timer
TCx_Timer16bitPeriodGetReads the period value of given timer
TCx_Timer16bitCounterGetReads the timer current count value
TCx_Timer16bitCounterSetSets new timer counter value
TCx_Timer16bitCompareSetSets the compare value of a given timer
TCx_Timer32bitPeriodSetSets the period value of a given timer
TCx_Timer32bitPeriodGetReads the period value of given timer
TCx_Timer32bitCounterGetReads the timer current count value
TCx_Timer32bitCounterSetSets new timer counter value
TCx_Timer32bitCompareSetSets the compare value of a given timer
TCx_TimerPeriodHasExpiredChecks whether timer period is elapsed
TCx_TimerFrequencyGetProvides the given timer's counter-increment frequency
TCx_TimerCallbackRegisterRegisters the function to be called from interrupt
TCx_CompareInitializeInitializes given instance of TC
TCx_CompareStartStarts the timer for given TC instance
TCx_CompareStopStops the timer for given TC instance
TCx_Compare8bitPeriodSetSets the period value of a given timer
TCx_Compare8bitPeriodGetReads the period value of given timer
TCx_Compare8bitCounterGetReads the timer current count value
TCx_Compare8bitCounterSetSets new timer counter value
TCx_Compare8bitMatch0SetWrites compare value of the given compare channel 0
TCx_Compare8bitMatch1SetWrites compare value of the given compare channel 1
TCx_Compare16bitPeriodSetSets the period value of a given timer
TCx_Compare16bitPeriodGetReads the period value of given timer
TCx_Compare16bitCounterGetReads the timer current count value
TCx_Compare16bitCounterSetSets new timer counter value
TCx_Compare16bitMatch0SetWrites compare value of the given compare channel 0
TCx_Compare16bitMatch1SetWrites compare value of the given compare channel 1
TCx_Compare32bitPeriodSetSets the period value of a given timer
TCx_Compare32bitPeriodGetReads the period value of given timer
TCx_Compare32bitCounterGetReads the timer current count value
TCx_Compare32bitCounterSetSets new timer counter value
TCx_Compare32bitMatch0SetWrites compare value of the given compare channel 0
TCx_Compare32bitMatch1SetWrites compare value of the given compare channel 1
TCx_CompareFrequencyGetProvides the given timer's counter-increment frequency
TCx_CompareCallbackRegisterRegisters the function to be called from interrupt
TCx_CompareStatusGetReads status of compare operation
TCx_CaptureInitializeInitializes given instance of TC
TCx_CaptureStartStarts the timer for given TC instance
TCx_CaptureStopStops the timer for given TC instance
TCx_Capture8bitChannel0GetReads capture value from channel 0
TCx_Capture16bitChannel0GetReads capture value from channel 0
TCx_Capture32bitChannel0GetReads capture value from channel 0
TCx_Capture8bitChannel1GetReads capture value from channel 1
TCx_Capture16bitChannel1GetReads capture value from channel 1
TCx_Capture32bitChannel1GetReads capture value from channel 1
TCx_CaptureStatusGetReads status capture operation
TCx_CaptureCallbackRegisterRegisters the function to be called from interrupt

Data types and constants

NameTypeDescription
TC_TIMER_STATUSTypedefIdentifies channel interrupt source mask
TC_TIMER_CALLBACKTypedefDefines the function pointer data type and function signature for the TC channel callback function
TCx_COMPARE_STATUSTypedefIdentifies channel interrupt source mask
TCx_COMPARE_CALLBACKTypedefDefines the function pointer data type and function signature for the tc channel callback function
TCx_CAPTURE_STATUSTypedefIdentifies channel interrupt source mask
TCx_CAPTURE_CALLBACKTypedefDefines 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.