1.15.17 Timer(TMR)

Depending on the specific variant, the PIC32M device family offers several 16-bit timers. These timers are designated as Timer1, Timer2, Timer3, ..., etc. 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. With certain exceptions, all of the timers have the same functional circuitry.

The PIC32 has two kinds of timers:

Timer1

All timers can run when the Central Processing Unit (CPU) is in idle mode, but only Timer1's asynchronous external clock mode enables it to run while the CPU is in sleep. This, combined with its ability to be driven by a secondary oscillator, allows the timer to function as a real-time clock (RTC).

Timer2, 3, 4, etc ...

This peripheral library covers timers of this category.

Each timer has its own 16-bit counter and period registers. A 32-bit timer can be created by combining two timers (e.g. Timer2 & Timer3). In this case, the 32-bit timer is controlled by the even number timer control registers (e.g. Timer2). When an interrupt event occurs, the odd number timer generates the event (e.g. Timer3). The timer supports the following features. 16-bit and 32-bit modes Synchronous Internal Timer Synchronous Internal Gated Timer Synchronous External Timer

Timer Operation (non-gate mode) When a timer is not in gate mode, it will generate an interrupt event when the value in the counter matches the value in the period register. When this match occurs, the counter is reset to zero, and the timer starts counting again. Note that if interrupts are disabled, the timer interrupt flag, TxIF, will still be set.

Timer Operation (Gate mode) You can use the timer's gate feature to accurately measure the time for an external event. When the timers are in gate mode, the counters (driven by an internal clock) only increment when the TxCK pin is high. When this pin goes low, the timer stops incrementing and the timer interrupt flag, TxIF, is set. Note that in this mode, an interrupt event is not generated when the timer value matches the period register.

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 TIMER2_EventHandler(uint32_t status, uintptr_t context)
{
    /* Toggle LED */
    LED_Toggle();
}

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

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

Library Interface

peripheral library provides the following interfaces:

Functions

Name Description
void
TMRx_Initialize 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

Data types and constants

Name Type Description
TMR_CALLBACK Typedef Defines the function pointer data type and function signature for the TMRx callback function