Jump to main content

##

# 3.2.22 Timer(TMR)

The Timer1 module found in particular family devices shares many features with a classic Type A timer. There is typically a single Timer1 module implemented for each CPU core present in a device. Timers are useful for generating accurate time-based periodic interrupt events for software applications or real-time operating systems. Other uses include counting external pulses or accurate timing measurement of external events by using the timer’s gate feature. The timers found in some particular family devices include the following features:

• Asynchronous and Synchronous Operation

• Software-Selectable Internal or External Clock Sources

• Programmable Interrupt Generation and Priority

• Gated External Pulse Counter

• Software Prescalers: 1:1, 1:8, 1:64 and 1:256

## Using The Library

Timer mode is used for periodic interrupt generation. 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 and the registered callback function will be called.

Callback method This example demonstrates how to use the timer to generate periodic callback.

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

    int main(void)
    {
        /* Register callback function Timer interrupt */
        TMR1_CallbackRegister(TIMER1_EventHandler,(uintptr_t)NULL);

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

## Library Interface

Peripheral library provides the following interfaces:

Functions

Name | Description
--- | ---
TMRx_Initialize | Initializes given instance of TMR
TMRx_Deinitialize | De-initializes given instance of TMR
TMRx_Start | Starts the given Timer
TMRx_Stop | Stops the given Timer counter
TMRx_PeriodSet | Sets the period value of a given timer
TMRx_PeriodGet | Reads the period value of given timer
TMRx_CounterGet | Reads the timer counter value
TMRx_FrequencyGet | Provides the given timer's counter-increment frequency
TMRx_InterruptEnable | Enable TMR overflow interrupt
TMRx_InterruptDisable | Disable TMR overflow interrupt
TMRx_CallbackRegister | Sets the callback_fn function for an match
TMRx_PeriodHasExpired | Checks period expiration
TMRx_StartTimeOut | Stores current tick count and delay value in the timeout
TMRx_ResetTimeOut | Resets current tick count in the timeout
TMRx_IsTimeoutReached | Checks for timeout
TMRx_GetTickCounter | Returns current tick count

Data types and constants

Name | Type | Description
--- | --- | ---
TMR_CALLBACK | Typedef | Defines the function pointer data type and function signature for the TMRx callback function
TMR_TIMEOUT | Struct | Defines the following parameters for a timeout period.