1.16.24 1.18.20 1.19.20 1.20.24 1.21.25 1.23.26 Timer1(TMR1)

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).

This peripheral library covers timers of this category.*

Timer2, 3, 4, etc ...

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 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
TMR1_Initialize Initializes given instance of TMR
TMR1_Start Starts the given Timer
TMR1_Stop Stops the given Timer counter
TMR1_PeriodSet Sets the period value of a given timer
TMR1_PeriodGet Reads the period value of given timer
TMR1_CounterGet Reads the timer counter value
TMR1_FrequencyGet Provides the given timer's counter-increment frequency
TMR1_InterruptEnable Enable TMR interrupts
TMR1_InterruptDisable Disable TMR interrupts
TMR1_CallbackRegister Sets the callback_fn function for an match

Data types and constants

Name Type Description
TMR1_CALLBACK Typedef Defines the function pointer data type and function signature for the TMR1 callback function