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.
TMR2
Module description
This file contains the API prototypes and other data types for the TMR2 module.
Data structures
struct TMR_INTERFACE
This is an instance of the TMR_INTERFACE for TMR2 module.
Enumerations
enum Timer2_HLT_MODE { 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 = 0x16, Timer2_OS_STARTS_TMRON_ERSLOW = 0x17, Timer2_MS_STARTS_TMRON_ERSRISINGEDGEDETECT = 0x11, Timer2_MS_STARTS_TMRON_ERSFALLINGEDGEDETECT = 0x12, Timer2_MS_STARTS_TMRON_ERSBOTHEDGE = 0x13 }
Defines the several modes of operation of the timer with the Hardware Limit Timer (HLT) extension.
enum Timer2_HLT_EXT_RESET_SOURCE { Timer2_T2INPPS_PIN = 0x0, Timer2_TMR4_POSTSCALED = 0x2, Timer2_CCP1OUT = 0x4, Timer2_PWM1_OUT1 = 0x5, Timer2_PWM1_OUT2 = 0x6, Timer2_PWM2_OUT1 = 0x7, Timer2_PWM2_OUT2 = 0x8, Timer2_PWM3_OUT1 = 0x9, Timer2_PWM3_OUT2 = 0xa, Timer2_CMP1_OUT = 0xb, Timer2_CMP2_OUT = 0xc, Timer2_ZCD_OUT = 0xd, Timer2_CLC1_OUT = 0xe, Timer2_CLC2_OUT = 0xf, Timer2_CLC3_OUT = 0x10, Timer2_CLC4_OUT = 0x11, Timer2_UART1_RX_EDGE = 0x12, Timer2_UART1_TX_EDGE = 0x13, Timer2_UART2_RX_EDGE = 0x14, Timer2_UART2_TX_EDGE = 0x15, Timer2_UART3_RX_EDGE = 0x16, Timer2_UART3_TX_EDGE = 0x17 }
Defines the different reset sources of the Hardware Limit Timer (HLT).
Functions
void Timer2_Initialize (void)
Initializes the TMR2 module. This routine must be called before any other TMR2 routines.
void Timer2_ModeSet (Timer2_HLT_MODE mode)
Configures the Hardware Limit Timer mode.
void Timer2_ExtResetSourceSet (Timer2_HLT_EXT_RESET_SOURCE reset)
Configures the HLT external reset source.
void Timer2_Start (void)
Starts TMR2.
void Timer2_Stop (void)
Stops TMR2.
uint8_t Timer2_Read (void)
Reads a 8-bit value from the TMR2 register.
void Timer2_Write (uint8_t timerVal)
Writes a 8-bit value to the TMR2 register.
void Timer2_PeriodCountSet (size_t periodVal)
Loads a 8-bit value to the PR2H register.
void Timer2_ISR (void)
Interrupt Service Routine (ISR) for the TMR2 overflow interrupt.
void Timer2_OverflowCallbackRegister (void(*InterruptHandler)(void))
Setter function for the TMR2 overflow callback.
Function Documentation
Timer2_ExtResetSourceSet()
void Timer2_ExtResetSourceSet (Timer2_HLT_EXT_RESET_SOURCE reset)
Configures the HLT external reset source.
Initialize the timer with Timer2_Initialize before calling this API. |
|
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
|
None. |
Section: TMR2 APIs
Timer2_ISR()
void Timer2_ISR (void )
Interrupt Service Routine (ISR) for the TMR2 overflow interrupt.
|
None. |
Timer2_ModeSet()
void Timer2_ModeSet (Timer2_HLT_MODE mode)
Configures the Hardware Limit Timer mode.
Initialize the timer with Timer2_Initialize before calling this API. |
|
None. |
Timer2_OverflowCallbackRegister()
void Timer2_OverflowCallbackRegister (void(*)(void) InterruptHandler)
Setter function for the TMR2 overflow callback.
|
None. |
Timer2_PeriodCountSet()
void Timer2_PeriodCountSet (size_t periodVal)
Loads a 8-bit value to the PR2H register.
Initialize the timer with Timer2_Initialize() before calling this API. |
|
None. |
Timer2_Read()
uint8_t Timer2_Read (void )
Reads a 8-bit value from the TMR2 register.
Initialize the timer with Timer2_Initialize() before calling this API. |
|
8-bit data from the TMR2 register. |
Timer2_Start()
void Timer2_Start (void )
Starts TMR2.
Initialize the timer with Timer2_Initialize() before calling this API. |
|
None. |
Timer2_Stop()
void Timer2_Stop (void )
Stops TMR2.
Initialize the timer with Timer2_Initialize() before calling this API. |
|
None. |
Timer2_Write()
void Timer2_Write (uint8_t timerVal)
Writes a 8-bit value to the TMR2 register.
Initialize the timer with Timer2_Initialize() before calling this API. |
|
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.
Detailed Description
This is an instance of the TMR_INTERFACE for TMR2 module.
Section: Data Type Definitions
#include <timer_interface.h>
Public Attributes
void(* Initialize )(void)
void(* Start )(void)
void(* Stop )(void)
void(* PeriodCountSet )(size_t count)
void(* TimeoutCallbackRegister )(void(*CallbackHandler)(void))
void(* Tasks )(void)
Member Data Documentation
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>
Data structures
struct TMR_INTERFACE
This is an instance of the TMR_INTERFACE for TMR2 module.
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
static void Timer2_DefaultOverflowCallback (void)
void Timer2_Initialize (void)
Initializes the TMR2 module. This routine must be called before any other TMR2 routines.
void Timer2_ModeSet (Timer2_HLT_MODE mode)
Configures the Hardware Limit Timer mode.
void Timer2_ExtResetSourceSet (Timer2_HLT_EXT_RESET_SOURCE reset)
Configures the HLT external reset source.
void Timer2_Start (void)
Starts TMR2.
void Timer2_Stop (void)
Stops TMR2.
uint8_t Timer2_Read (void)
Reads a 8-bit value from the TMR2 register.
void Timer2_Write (uint8_t timerVal)
Writes a 8-bit value to the TMR2 register.
void Timer2_PeriodCountSet (size_t periodVal)
Loads a 8-bit value to the PR2H register.
void Timer2_ISR (void)
Interrupt Service Routine (ISR) for the TMR2 overflow interrupt.
void Timer2_OverflowCallbackRegister (void(*InterruptHandler)(void))
Setter function for the TMR2 overflow callback.
Variables
const struct TMR_INTERFACE Timer2
static void(* Timer2_OverflowCallback )(void)
Detailed Description
This file contains the API implementations for the TMR2 module.
TMR2 Generated Driver File
Function Documentation
Timer2_DefaultOverflowCallback()
static void Timer2_DefaultOverflowCallback (void )[static]
Variable Documentation
Timer2
const struct TMR_INTERFACE Timer2
Initial value:
= { .Initialize = Timer2_Initialize, .Start = Timer2_Start, .Stop = Timer2_Stop, .PeriodCountSet = Timer2_PeriodCountSet, .TimeoutCallbackRegister = Timer2_OverflowCallbackRegister, .Tasks = NULL }
Section: Included Files
Timer2_OverflowCallback
void(* Timer2_OverflowCallback) (void)[static]
source/tmr2.h File Reference
#include <stdint.h> #include <stdbool.h> #include "timer_interface.h"
Functions
void Timer2_Initialize (void)
Initializes the TMR2 module. This routine must be called before any other TMR2 routines.
void Timer2_ModeSet (Timer2_HLT_MODE mode)
Configures the Hardware Limit Timer mode.
void Timer2_ExtResetSourceSet (Timer2_HLT_EXT_RESET_SOURCE reset)
Configures the HLT external reset source.
void Timer2_Start (void)
Starts TMR2.
void Timer2_Stop (void)
Stops TMR2.
uint8_t Timer2_Read (void)
Reads a 8-bit value from the TMR2 register.
void Timer2_Write (uint8_t timerVal)
Writes a 8-bit value to the TMR2 register.
void Timer2_PeriodCountSet (size_t periodVal)
Loads a 8-bit value to the PR2H register.
void Timer2_ISR (void)
Interrupt Service Routine (ISR) for the TMR2 overflow interrupt.
void Timer2_OverflowCallbackRegister (void(*InterruptHandler)(void))
Setter function for the TMR2 overflow callback.
Enumerations
enum Timer2_HLT_MODE { 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 = 0x16, Timer2_OS_STARTS_TMRON_ERSLOW = 0x17, Timer2_MS_STARTS_TMRON_ERSRISINGEDGEDETECT = 0x11, Timer2_MS_STARTS_TMRON_ERSFALLINGEDGEDETECT = 0x12, Timer2_MS_STARTS_TMRON_ERSBOTHEDGE = 0x13 }
Defines the several modes of operation of the timer with the Hardware Limit Timer (HLT) extension.
enum Timer2_HLT_EXT_RESET_SOURCE { Timer2_T2INPPS_PIN = 0x0, Timer2_TMR4_POSTSCALED = 0x2, Timer2_CCP1OUT = 0x4, Timer2_PWM1_OUT1 = 0x5, Timer2_PWM1_OUT2 = 0x6, Timer2_PWM2_OUT1 = 0x7, Timer2_PWM2_OUT2 = 0x8, Timer2_PWM3_OUT1 = 0x9, Timer2_PWM3_OUT2 = 0xa, Timer2_CMP1_OUT = 0xb, Timer2_CMP2_OUT = 0xc, Timer2_ZCD_OUT = 0xd, Timer2_CLC1_OUT = 0xe, Timer2_CLC2_OUT = 0xf, Timer2_CLC3_OUT = 0x10, Timer2_CLC4_OUT = 0x11, Timer2_UART1_RX_EDGE = 0x12, Timer2_UART1_TX_EDGE = 0x13, Timer2_UART2_RX_EDGE = 0x14, Timer2_UART2_TX_EDGE = 0x15, Timer2_UART3_RX_EDGE = 0x16, Timer2_UART3_TX_EDGE = 0x17 }
Defines the different reset sources of the Hardware Limit Timer (HLT).
Variables
const struct TMR_INTERFACE Timer2
Detailed Description
TMR2 Generated Driver API Header File
Variable Documentation
Timer2
const struct TMR_INTERFACE Timer2
Section: Included Files