4.24.3.2 TMR2 with One-Shot Operation

Timer2 with One-Shot Operation

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

4.24.3.2.2 Supported Device Families

PIC12/16F161x PIC16F152xx PIC16F153xx
PIC16F171x PIC16F171xx PIC16F176x
PIC16F177x PIC16F180xx PIC16F181xx
PIC16F184x PIC16F184xx PIC16F188xx
PIC16F191xx PIC18F-K40 PIC18F-K42
PIC18F-K83 PIC18F-Q10 PIC18F-Q20
PIC18F-Q40 PIC18F-Q41 PIC18F-Q43
PIC18F-Q71 PIC18F-Q83 PIC18F-Q84

4.24.3.2.3 Timer Interface

  • struct TMR_INTERFACE

    Structure containing the function pointers of the TMR driver.

4.24.3.2.4 Required header files:

#include "mcc_generated_files/timer/tmr2.h"

4.24.3.2.5 How to Use the Timer1 or Timer2 PLIB Drivers

For use cases see: 4.24.1.2.5 How to Use the Timer0 PLIB Driver

Since Timer0 (TMR0), Timer1 (TMR1) and Timer2 (TMR2) share the same timer interface, running the TMR0 use cases on the other timers is relatively simple. Specifically, look for the TMR_INTERFACE definition in timerX.c, then set the timer pointer to use the correct timer interface, e.g., *Timer = &TimerX, where X is the timer instance.

/* Create a pointer of the type TMR_INTERFACE and assign it to the Timer0 TMR_INTERFACE struct address.
This enables access to the portable API interface, facilitating the change of the peripheral instance the timer runs on. */
const struct TMR_INTERFACE *Timer = &Timer0; 

Similarly, for more specific code, such as the line below, replace these with the equivalent code for the selected timer.

timerTMR0ReloadVal16bit = (uint16_t)LED_500ms;  //Clear the Counter register

4.24.3.2.6 Module Documentation

TIMER INTERFACE

This file contains API prototypes and other data types for the timer module.

This file contains API prototypes and other data types for the timer module.

Version: Timer Interface Version 1.0.0

TMR2

Module description

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

Version: TMR2 Driver Version 3.0.1
Enumerations
Functions

Function Documentation

Timer2_ExtResetSourceSet()

void Timer2_ExtResetSourceSet (Timer2_HLT_EXT_RESET_SOURCE reset)

Configures the HLT external reset source.

Precondition:

Initialize the timer with Timer2_Initialize before calling this API.

Parameters:
Reset

- Value to be written to the T2RSTbits.RSEL bits.

Returns:

None.

Timer2_Initialize()

void Timer2_Initialize (void )

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

Section: Macro DeclarationsSection: TMR2 APIs

Parameters:
None.
Returns:

None.

Section: TMR2 APIs

Timer2_ISR()

void Timer2_ISR (void )

Interrupt Service Routine (ISR) for the TMR2 overflow interrupt.

Parameters:
None.
Returns:

None.

Timer2_ModeSet()

void Timer2_ModeSet (Timer2_HLT_MODE mode)

Configures the Hardware Limit Timer mode.

Precondition:

Initialize the timer with Timer2_Initialize before calling this API.

Parameters:
Mode

- Value to be written to the T2HLTbits.MODE bits.

Returns:

None.

Timer2_OverflowCallbackRegister()

void Timer2_OverflowCallbackRegister (void(*)(void) InterruptHandler)

Setter function for the TMR2 overflow callback.

Parameters:
*InterruptHandler

- Pointer to custom callback function for overflow events.

Returns:

None.

Timer2_PeriodCountSet()

void Timer2_PeriodCountSet (size_t periodVal)

Loads a 8-bit value to the PR2H register.

Precondition:

Initialize the timer with Timer2_Initialize() before calling this API.

Parameters:
8-bit

value to be written to the PR2 register.

Returns:

None.

Timer2_Read()

uint8_t Timer2_Read (void )

Reads a 8-bit value from the TMR2 register.

Precondition:

Initialize the timer with Timer2_Initialize() before calling this API.

Parameters:
None.
Returns:

8-bit data from the TMR2 register.

Timer2_Start()

void Timer2_Start (void )

Starts TMR2.

Precondition:

Initialize the timer with Timer2_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

Timer2_Stop()

void Timer2_Stop (void )

Stops TMR2.

Precondition:

Initialize the timer with Timer2_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

Timer2_Write()

void Timer2_Write (uint8_t timerVal)

Writes a 8-bit value to the TMR2 register.

Precondition:

Initialize the timer with Timer2_Initialize() before calling this API.

Parameters:
timerVal

- 8-bit value to be written to the TMR2 register.

Returns:

None.

Enumeration Type Documentation

Timer2_HLT_EXT_RESET_SOURCE

enum Timer2_HLT_EXT_RESET_SOURCE

Defines the different reset sources of the Hardware Limit Timer (HLT).

Timer2_T2INPPS_PIN
Timer2_TMR4_POSTSCALED
Timer2_CCP1OUT
Timer2_PWM1_OUT1
Timer2_PWM1_OUT2
Timer2_PWM2_OUT1
Timer2_PWM2_OUT2
Timer2_PWM3_OUT1
Timer2_PWM3_OUT2
Timer2_CMP1_OUT
Timer2_CMP2_OUT
Timer2_ZCD_OUT
Timer2_CLC1_OUT
Timer2_CLC2_OUT
Timer2_CLC3_OUT
Timer2_CLC4_OUT
Timer2_UART1_RX_EDGE
Timer2_UART1_TX_EDGE
Timer2_UART2_RX_EDGE
Timer2_UART2_TX_EDGE
Timer2_UART3_RX_EDGE
Timer2_UART3_TX_EDGE
Timer2_HLT_MODE

enum Timer2_HLT_MODE

Defines the several modes of operation of the timer with the Hardware Limit Timer (HLT) extension.

Timer2_ROP_STARTS_TMRON
Timer2_ROP_STARTS_TMRON_ERSHIGH
Timer2_ROP_STARTS_TMRON_ERSLOW
Timer2_ROP_RESETS_ERSBOTHEDGE
Timer2_ROP_RESETS_ERSRISINGEDGE
Timer2_ROP_RESETS_ERSFALLINGEDGE
Timer2_ROP_RESETS_ERSLOW
Timer2_ROP_RESETS_ERSHIGH
Timer2_OS_STARTS_TMRON
Timer2_OS_STARTS_ERSRISINGEDGE
Timer2_OS_STARTS_ERSFALLINGEDGE
Timer2_OS_STARTS_ERSBOTHEDGE
Timer2_OS_STARTS_ERSFIRSTRISINGEDGE
Timer2_OS_STARTS_ERSFIRSTFALLINGEDGE
Timer2_OS_STARTS_ERSRISINGEDGEDETECT
Timer2_OS_STARTS_ERSFALLINGEDGEDETECT
Timer2_OS_STARTS_TMRON_ERSHIGH
Timer2_OS_STARTS_TMRON_ERSLOW
Timer2_MS_STARTS_TMRON_ERSRISINGEDGEDETECT
Timer2_MS_STARTS_TMRON_ERSFALLINGEDGEDETECT
Timer2_MS_STARTS_TMRON_ERSBOTHEDGE

4.24.3.2.7 Class Documentation

TMR_INTERFACE Struct Reference

This is an instance of the TMR_INTERFACE for TMR2 module.

Member Data Documentation

The documentation for this struct was generated from the following file:

source/

timer_interface.h

Initialize

void(* TMR_INTERFACE::Initialize) (void)

PeriodCountSet

void(* TMR_INTERFACE::PeriodCountSet) (size_t count)

Start

void(* TMR_INTERFACE::Start) (void)

Stop

void(* TMR_INTERFACE::Stop) (void)

Tasks

void(* TMR_INTERFACE::Tasks) (void)

TimeoutCallbackRegister

void(* TMR_INTERFACE::TimeoutCallbackRegister) (void(*CallbackHandler)(void))

4.24.3.2.8 File Documentation

source/timer_interface.h File Reference

#include <stddef.h>

Detailed Description

Timer Interface Generated Driver API Header File

source/tmr2.c File Reference

This file contains the API implementations for the TMR2 module.

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

Functions

Detailed Description

This file contains the API implementations for the TMR2 module.

TMR2 Generated Driver File

Version: TMR2 Driver Version 3.0.1

Function Documentation

Timer2_DefaultOverflowCallback()

static void Timer2_DefaultOverflowCallback (void )[static]

source/tmr2.h File Reference

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

Functions

Enumerations

Detailed Description

TMR2 Generated Driver API Header File

Variable Documentation

Timer2

const struct TMR_INTERFACE Timer2

Section: Included Files