3.24.3.2 TMR2 with One-Shot Operation

Timer2 with One-Shot Operation

3.24.3.2.1 Introduction

The Timer2 (TMR2) module is a 8-bit timer that operates by a matching the value written to a configurable 8-bit period register.

3.24.3.2.2 Supported Device Families

PIC12/16F161xPIC16F152xxPIC16F153xx
PIC16F171xPIC16F171xxPIC16F176x
PIC16F177xPIC16F180xxPIC16F181xx
PIC16F184xPIC16F184xxPIC16F188xx
PIC16F191xxPIC18F-K40PIC18F-K42
PIC18F-K83PIC18F-Q10PIC18F-Q20
PIC18F-Q40 PIC18F-Q41PIC18F-Q43
PIC18F-Q71PIC18F-Q83PIC18F-Q84

3.24.3.2.3 Required header files:

#include "mcc_generated_files/timer/tmr2.h"

3.24.3.2.4 How to use the Timer 2 PLIB

Click the links to view the code snippets associated with each example:

3.24.3.2.5 Specific MISRA C:2012 Deviations

Global TMR2_MAX_COUNT

Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

Global TMR2_Read

Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

3.24.3.2.6 Module Documentation

TMR2

This file contains API prototypes and other data types for the TMR2 driver.

Module description

This file contains API prototypes and other data types for the TMR2 driver.

This file contains deprecated macros or functions for the TMR2 driver.

Version: Driver Version 4.0.0 Package Version 5.0.0
Definitions
Enumerations
Functions

Definition Documentation

TIMER2_CLOCK_FREQ

#define TIMER2_CLOCK_FREQ TMR2_CLOCK_FREQ

Defines the Custom Name for the TMR2_CLOCK_FREQ.

Timer2_CounterGet

#define Timer2_CounterGet TMR2_CounterGet

Defines the Custom Name for the TMR2_CounterGet API.

Timer2_CounterSet

#define Timer2_CounterSet TMR2_CounterSet

Defines the Custom Name for the TMR2_CounterSet API.

Timer2_Deinitialize

#define Timer2_Deinitialize TMR2_Deinitialize

Defines the Custom Name for the TMR2_Deinitialize API.

Timer2_ExtResetSourceSet

#define Timer2_ExtResetSourceSet TMR2_ExtResetSourceSet

Defines the Custom Name for the TMR2_ExtResetSourceSet API.

Timer2_Initialize

#define Timer2_Initialize TMR2_Initialize

Defines the Custom Name for the TMR2_Initialize API.

Timer2_ISR

#define Timer2_ISR TMR2_ISR

Defines the Custom Name for the TMR2_ISR API.

TIMER2_MAX_COUNT

#define TIMER2_MAX_COUNT TMR2_MAX_COUNT

Defines the Custom Name for the TMR2_MAX_COUNT.

Timer2_MaxCountGet

#define Timer2_MaxCountGet TMR2_MaxCountGet

Defines the Custom Name for the TMR2_MaxCountGet API.

Timer2_ModeSet

#define Timer2_ModeSet TMR2_ModeSet

Defines the Custom Name for the TMR2_ModeSet API.

Timer2_OverflowCallbackRegister

#define Timer2_OverflowCallbackRegister TMR2_PeriodMatchCallbackRegister

Defines the Custom Name for the TMR2_PeriodMatchCallbackRegister API. The Timer2_OverflowCallbackRegister will be deprecated in the future release. Use Timer2_PeriodMatchCallbackRegister instead.

Timer2_PeriodCountSet

#define Timer2_PeriodCountSet TMR2_PeriodSet

Defines the Custom Name for the TMR2_PeriodSet API. The Timer2_PeriodCountSet will be deprecated in the future release. Use Timer2_PeriodSet instead.

Timer2_PeriodGet

#define Timer2_PeriodGet TMR2_PeriodGet

Defines the Custom Name for the TMR2_PeriodGet API.

Timer2_PeriodMatchCallbackRegister

#define Timer2_PeriodMatchCallbackRegister TMR2_PeriodMatchCallbackRegister

Defines the Custom Name for the TMR2_PeriodMatchCallbackRegister API.

Timer2_PeriodMatchStatusClear

#define Timer2_PeriodMatchStatusClear TMR2_PeriodMatchStatusClear

Defines the Custom Name for the TMR2_PeriodMatchStatusClear API.

Timer2_PeriodMatchStatusGet

#define Timer2_PeriodMatchStatusGet TMR2_PeriodMatchStatusGet

Defines the Custom Name for the TMR2_PeriodMatchStatusGet API.

Timer2_PeriodSet

#define Timer2_PeriodSet TMR2_PeriodSet

Defines the Custom Name for the TMR2_PeriodSet API.

Timer2_Read

#define Timer2_Read TMR2_CounterGet

Defines the Custom Name for the TMR2_CounterGet API. The Timer2_Read will be deprecated in the future release. Use Timer2_CounterGet instead.

Timer2_Start

#define Timer2_Start TMR2_Start

Defines the Custom Name for the TMR2_Start API.

Timer2_Stop

#define Timer2_Stop TMR2_Stop

Defines the Custom Name for the TMR2_Stop API.

Timer2_Tasks

#define Timer2_Tasks TMR2_Tasks

Defines the Custom Name for the TMR2_Tasks API.

Timer2_TMRInterruptDisable

#define Timer2_TMRInterruptDisable TMR2_TMRInterruptDisable

Defines the Custom Name for the TMR2_TMRInterruptDisable API.

Timer2_TMRInterruptEnable

#define Timer2_TMRInterruptEnable TMR2_TMRInterruptEnable

Defines the Custom Name for the TMR2_TMRInterruptEnable API.

Timer2_Write

#define Timer2_Write TMR2_CounterSet

Defines the Custom Name for the TMR2_CounterSet API. The Timer2_Write will be deprecated in the future release. Use Timer2_CounterSet instead.

TMR2_CLOCK_FREQ

#define TMR2_CLOCK_FREQ (16000000UL)

Defines the TMR2 prescaled clock frequency in hertz.

TMR2_MAX_COUNT

#define TMR2_MAX_COUNT (255U)

Defines the TMR2 maximum count value.

MISRA C:2012 Deviation Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

TMR2_OverflowCallbackRegister

#define TMR2_OverflowCallbackRegister TMR2_PeriodMatchCallbackRegister

Defines the Custom Name for the TMR2_PeriodMatchCallbackRegister API. The TMR2_OverflowCallbackRegister will be deprecated in the future release. Use TMR2_PeriodMatchCallbackRegister instead.

TMR2_PeriodCountSet

#define TMR2_PeriodCountSet TMR2_PeriodSet

Defines the Custom Name for the TMR2_PeriodSet API. The TMR2_PeriodCountSet will be deprecated in the future release. Use TMR2_PeriodSet instead.

TMR2_Read

#define TMR2_Read TMR2_CounterGet

Defines the Custom Name for the TMR2_CounterGet API. The TMR2_Read will be deprecated in the future release. Use TMR2_CounterGet instead.

MISRA C:2012 Deviation Advisory: misra-c2012-2.5  

Justification: MCC Melody drivers provide macros that can be added to an application. It depends on the application whether a macro is used or not.

TMR2_Write

#define TMR2_Write TMR2_CounterSet

Defines the Custom Name for the TMR2_CounterSet API. The TMR2_Write will be deprecated in the future release. Use TMR2_CounterSet instead.

Function Documentation

TMR2_CounterGet()

uint8_t TMR2_CounterGet (void )

Returns the current counter value.

Precondition:

Initialize TMR2 with TMR2_Initialize() before calling this API.

Parameters:
None.
Returns:

Current counter value

TMR2_CounterSet()

void TMR2_CounterSet (uint8_t count)

Sets the counter value for the TMR2 timer.

Precondition:

Initialize TMR2 with TMR2_Initialize() before calling this API.

Parameters:
count

- Counter value to be written to the T2TMR register

Returns:

None.

TMR2_Deinitialize()

void TMR2_Deinitialize (void )

Deinitializes the TMR2 to POR values.

Parameters:
None.
Returns:

None.

TMR2_ExtResetSourceSet()

void TMR2_ExtResetSourceSet (TMR2_HLT_EXT_RESET_SOURCE reset)

Sets the HLT External Reset source.

Precondition:

Initialize TMR2 with TMR2_Initialize() before calling this API.

Parameters:
reset

- Value to be written to the T2RSTbits.RSEL bits

Returns:

None.

TMR2_Initialize()

void TMR2_Initialize (void )

Initializes the Timer2 (TMR2) module. This routine must be called before any other TMR2 routines.

Section: TMR2 APIs

Parameters:
None.
Returns:

None.

Section: TMR2 APIs

TMR2_ISR()

void TMR2_ISR (void )

Interrupt Service Routine (ISR) for the TMR2 period match interrupt.

Parameters:
None.
Returns:

None.

TMR2_MaxCountGet()

uint8_t TMR2_MaxCountGet (void )

Returns the TMR2 maximum count value.

Parameters:
None.
Returns:

Maximum count value

TMR2_ModeSet()

void TMR2_ModeSet (TMR2_HLT_MODE mode)

Sets the Hardware Limit Timer (HLT) mode.

Precondition:

Initialize TMR2 with TMR2_Initialize() after calling this API.

Parameters:
mode

- Value to be written to the T2HLTbits.MODE bits

Returns:

None.

TMR2_PeriodGet()

uint8_t TMR2_PeriodGet (void )

Returns the current period count value.

Precondition:

Initialize TMR2 with TMR2_Initialize() before calling this API.

Parameters:
None.
Returns:

Period count value from the T2PR register

TMR2_PeriodMatchCallbackRegister()

void TMR2_PeriodMatchCallbackRegister (void(*)(void) callbackHandler)

Setter function for the TMR2 period match event.

Parameters:
CallbackHandler

- Address of the custom callback function

Returns:

None.

TMR2_PeriodMatchStatusClear()

void TMR2_PeriodMatchStatusClear (void )

Clears the TMR2 Period Match Interrupt flag.

Parameters:
None.
Returns:

None.

TMR2_PeriodMatchStatusGet()

bool TMR2_PeriodMatchStatusGet (void )

Returns the status of the TMR2 Period Match Interrupt flag.

Parameters:
None.
Returns:

Interrupt flag status

TMR2_PeriodSet()

void TMR2_PeriodSet (uint8_t periodVal)

Sets the period count value.

Precondition:

Initialize TMR2 with TMR2_Initialize() before calling this API.

Parameters:
periodVal

- Period count value to be written to the T2PR register

Returns:

None.

TMR2_Start()

void TMR2_Start (void )

Starts the TMR2 timer.

Precondition:

Initialize TMR2 with TMR2_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR2_Stop()

void TMR2_Stop (void )

Stops the TMR2 timer.

Precondition:

Initialize TMR2 with TMR2_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR2_Tasks()

void TMR2_Tasks (void )

Performs tasks to be executed during the TMR2 period match event.

Parameters:
None.
Returns:

None.

TMR2_TMRInterruptDisable()

void TMR2_TMRInterruptDisable (void )

Disables the TMR2 interrupt.

Parameters:
None.
Returns:

None.

TMR2_TMRInterruptEnable()

void TMR2_TMRInterruptEnable (void )

Enables the TMR2 interrupt.

Parameters:
None.
Returns:

None.

Enumeration Type Documentation

TMR2_HLT_EXT_RESET_SOURCE

enum TMR2_HLT_EXT_RESET_SOURCE

Defines the several modes of operation of the timer with the HLT extension.

Defines the different Reset sources of the HLT.

TMR2_T2CKIPPS_PIN
TMR2_TMR4_POSTSCALED
TMR2_TMR6_POSTSCALED
TMR2_CCP1OUT
TMR2_CCP2OUT
TMR2_CCP3OUT
TMR2_PWM1_OUT1
TMR2_PWM1_OUT2
TMR2_PWM2_OUT1
TMR2_PWM2_OUT2
TMR2_PWM3_OUT1
TMR2_PWM3_OUT2
TMR2_PWM4_OUT1
TMR2_PWM4_OUT2
TMR2_CMP1OUT
TMR2_CMP2OUT
TMR2_ZCDOUT
TMR2_CLC1_OUT
TMR2_CLC2_OUT
TMR2_CLC3_OUT
TMR2_CLC4_OUT
TMR2_CLC5_OUT
TMR2_CLC6_OUT
TMR2_CLC7_OUT
TMR2_CLC8_OUT
TMR2_UART1_RX_EDGE
TMR2_UART1_TX_EDGE
TMR2_UART2_RX_EDGE
TMR2_UART2_TX_EDGE
TMR2_UART3_RX_EDGE
TMR2_UART3_TX_EDGE
TMR2_UART4_RX_EDGE
TMR2_UART4_TX_EDGE
TMR2_UART5_RX_EDGE
TMR2_UART5_TX_EDGE

3.24.3.2.7 TMR2 Examples

This section explains various use case examples of the Timer module within an MPLAB® Code Configurator (MCC) Melody Project.

TMR2 PLIB Examples

4 ms/8 ms Timer Period Change in Non-Interrupt Mode

This use case configures the TMR2 PLIB to generate the timer period. The period of a timer is changed at run time during each overflow event. The callback function is used to change the timer period.

System Configuration
  • System>Clock Control:
    • Clock Source: HFINTOSC

    • HF Internal Clock: 64 MHz

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

Timer Configuration
  • Drivers>TMR2:
    • Timer Enable: False

    • Prescaler: 1:128

    • Postscaler: 1:16  

    • TMR Interrupt Enable: False

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
 * Development Board: PIC18F57Q43 Curiosity Nano Board
 * MCC Configuration:
 *     System module: HFINTOSC (64 MHz)
 *     TMR2 module: Timer Enable - False, Prescaler - 1:128, Postscaler - 1:16, TMR Interrupt Enable - False
 *     Pins module: RF3 - GPIO Output, CustomName - LED 
 */
 
#define USE_TASKS_API 1
#define POSTSCALER 16UL
#define MS_TO_TICKS(ms) (((TMR2_CLOCK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_4_MS (MS_TO_TICKS(4UL)/POSTSCALER)
#define LED_8_MS (MS_TO_TICKS(8UL)/POSTSCALER)

static void Timer_PeriodChange(void)
{ 
    static volatile bool changePeriod = false;
    LED_Toggle();
    TMR2_Stop();
    
    uint8_t maxCount = TMR2_MaxCountGet();
    uint8_t newPeriod = changePeriod ? LED_4_MS:LED_8_MS;
    
    if(maxCount > newPeriod)
    {      
        TMR2_PeriodSet(newPeriod);
    }
    else
    {
        // Invalid period
    }
    changePeriod = !changePeriod;
    TMR2_Start();
}

int main(void)
{
    SYSTEM_Initialize();

    TMR2_PeriodSet(LED_4_MS);   
    TMR2_PeriodMatchStatusClear();
    
#if USE_TASKS_API    
    TMR2_PeriodMatchCallbackRegister(Timer_PeriodChange);
#endif
    TMR2_Start();
    
    while(1)
    {
#if   USE_TASKS_API
        TMR2_Tasks();
#else
        if(1U == TMR2_PeriodMatchStatusGet())
        {
            Timer_PeriodChange();
            TMR2_PeriodMatchStatusClear();
        }
#endif
    }    
}

4 ms Timer Period in Interrupt Mode

This use case configures the TMR2 PLIB to generate the overflow interrupt at a period of 4 ms, while toggling an LED. This use case uses the generated APIs to set the timer period.

System Configuration
  • System>Clock Control:
    • Clock Source: HFINTOSC

    • HF Internal Clock: 64 MHz

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

Timer Configuration
  • Drivers>TMR2:
    • Timer Enable: False

    • Clock Source: FOSC/4

    • Prescaler: 1:64

    • Postscaler: 1:8

    • TMR Interrupt Enable: True

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
 * Development Board: PIC18F57Q43 Curiosity Nano Board
 * MCC Configuration:
 *     System module: HFINTOSC (64 MHz)
 *     TMR2 module: Timer Enable - False, Clock Source - FOSC/4, Prescaler - 1:64,  Postscaler - 1:8, TMR Interrupt Enable - True
 *     Pins module: RF3 - GPIO Output, CustomName - LED 
 */
 
#define POSTSCALER 8UL
#define MS_TO_TICKS(ms) (((TMR2_CLOCK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_4_MS  (MS_TO_TICKS(4UL)/POSTSCALER)

void Custom_Callback(void)
{
    LED_Toggle();
}

int main(void)
{
    SYSTEM_Initialize();

    const uint8_t maxCount = TMR2_MaxCountGet();
    TMR2_CounterSet(0);
    
    if(maxCount > LED_4_MS)
    {
        TMR2_PeriodSet(LED_4_MS);
    }
    else 
    {
        // Invalid period
        // Generate default timer period
        
    }
    
    TMR2_PeriodMatchCallbackRegister(Custom_Callback);
    TMR2_Start();
    
    INTERRUPT_GlobalInterruptEnable();
    
    while(1)
    {
    }    
}

Timer Wake From Sleep At Every 5s

This use case configures the TMR2 PLIB to wake the microcontroller (MCU) from Sleep mode every five seconds. The wake up event toggles an LED, then puts the MCU back to Sleep.

System Configuration
  • System>Clock Control:
    • Clock Source: HFINTOSC

    • HF Internal Clock: 64 MHz

  • System>Pins:
    • Pin Grid View: Select LED pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED"

Timer Configuration
  • Drivers>TMR2:
    • Timer Enable: True

    • Clock Source: LFINTOSC

    • Prescaler: 1:64

    • Postscaler: 1:16

    • Requested Period: 5s

    • TMR Interrupt Enable: True

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

/*
 * Development Board: PIC18F57Q43 Curiosity Nano Board
 * MCC Configuration:
 *     System module: HFINTOSC (64 MHz)
 *     TMR2 module: Timer Enable - True, Clock Source - LFINTOSC, Synchronization Enable - False, Prescaler - 1:64,  Postscaler - 1:16, Requested Period - 5s, TMR Interrupt Enable - True
 *     Pins module: RF3 - GPIO Output, CustomName - LED 
 */
 
int main(void)
{
    SYSTEM_Initialize();

    // Enable the Global Interrupts 
    INTERRUPT_GlobalInterruptEnable(); 

    while(1)
    {
        LED_Toggle();
        SLEEP(); 
        
        // Add your code here to execute after exiting Sleep mode 
    }    
}

3.24.3.2.8 File Documentation

source/tmr2.c File Reference

Driver implementation for the TMR2 module.

#include <xc.h>
#include "../tmr2.h"

Functions

Detailed Description

Driver implementation for the TMR2 module.

Driver implementation for the TMR2 Timer driver.

TMR2 Generated Driver File

Version: Driver Version 4.0.0 Package Version 5.0.0

TMR2 Generated Timer Driver File

Version: Driver Version 4.0.0 Package Version 5.0.0

Function Documentation

TMR2_DefaultPeriodMatchCallback()

static void TMR2_DefaultPeriodMatchCallback (void )[static]

Variable Documentation

TMR2_PeriodMatchCallback

void(* TMR2_PeriodMatchCallback) (void)[static]

Section: Included Files

source/tmr2.h File Reference

This file contains API prototypes and other data types for the TMR2 Timer driver.

#include <stdint.h>
#include <stdbool.h>
#include "tmr2_deprecated.h"

Functions

Macros

Enumerations

Detailed Description

This file contains API prototypes and other data types for the TMR2 Timer driver.

TMR2 Generated Driver API Header File

TMR2 Generated Timer Driver API Header File

Version: Driver Version 4.0.0 Package Version 5.0.0

Enumeration Type Documentation

TMR2_HLT_MODE

enum TMR2_HLT_MODE

TMR2_ROP_STARTS_TMRON
TMR2_ROP_STARTS_TMRON_ERSHIGH
TMR2_ROP_STARTS_TMRON_ERSLOW
TMR2_ROP_RESETS_ERSBOTHEDGE
TMR2_ROP_RESETS_ERSRISINGEDGE
TMR2_ROP_RESETS_ERSFALLINGEDGE
TMR2_ROP_RESETS_ERSLOW
TMR2_ROP_RESETS_ERSHIGH
TMR2_OS_STARTS_TMRON
TMR2_OS_STARTS_ERSRISINGEDGE
TMR2_OS_STARTS_ERSFALLINGEDGE
TMR2_OS_STARTS_ERSBOTHEDGE
TMR2_OS_STARTS_ERSFIRSTRISINGEDGE
TMR2_OS_STARTS_ERSFIRSTFALLINGEDGE
TMR2_OS_STARTS_ERSRISINGEDGEDETECT
TMR2_OS_STARTS_ERSFALLINGEDGEDETECT
TMR2_OS_STARTS_TMRON_ERSHIGH
TMR2_OS_STARTS_TMRON_ERSLOW
TMR2_MS_STARTS_TMRON_ERSRISINGEDGEDETECT
TMR2_MS_STARTS_TMRON_ERSFALLINGEDGEDETECT
TMR2_MS_STARTS_TMRON_ERSBOTHEDGE

source/tmr2_deprecated.h File Reference

Macros

source/tmr2_drv.c File Reference

#include <xc.h>
#include "../tmr2.h"

Functions

Function Documentation

TMR2_DefaultPeriodMatchCallback()

static void TMR2_DefaultPeriodMatchCallback (void )[static]

Variable Documentation

TMR2_PeriodMatchCallback

void(* TMR2_PeriodMatchCallback) (void)[static]

source/tmr2_drv.h File Reference

#include <stdint.h>
#include <stdbool.h>
#include "timer_interface.h"

Functions

  • void TMR2_Initialize (void)

    Initializes the Timer2 (TMR2) module. This routine must be called before any other TMR2 routines.

  • void TMR2_Deinitialize (void)

    Deinitializes the TMR2 module.

  • void TMR2_Start (void)

    Starts the TMR2 timer.

  • void TMR2_Stop (void)

    Stops the TMR2 timer.

  • uint32_t TMR2_CounterGet (void)

    Returns the current counter value.

  • void TMR2_CounterSet (uint32_t count)

    Sets the counter value.

  • void TMR2_PeriodSet (uint32_t periodVal)

    Sets the period count value.

  • uint32_t TMR2_PeriodGet (void)

    Returns the current period value.

  • uint32_t TMR2_MaxCountGet (void)

    Returns the maximum count value of the timer.

  • void TMR2_ISR (void)

    Interrupt Service Routine (ISR) for the TMR2 period match event.

  • void TMR2_Tasks (void)

    Performs tasks to be executed during the TMR2 period match event.

  • void TMR2_PeriodMatchCallbackRegister (void(*callbackHandler)(void))

    Registers a callback function for the TMR2 period match event.

Macros

Variable Documentation

Timer2

const struct TIMER_INTERFACE Timer2

Section: Included Files

source/tmr2_example.dox File Reference