3.24.2.1 TMR1
Timer1
3.24.2.1.1 Introduction
The Timer1 (TMR1) module is a 16-bit timer with configurable timeout period and gate control features.
3.24.2.1.2 Supported Device Families
PIC16F145x | PIC12/16F150x | PIC16F151x |
PIC16F152x | PIC16F152xx | PIC16F153xx |
PIC12/16F157x | PIC12/16F161x | PIC16F170x |
PIC16F171x | PIC16F171xx | PIC16F176x |
PIC16F177x | PIC16F178x | PIC16F180xx |
PIC16F181xx | PIC16F183xx | PIC16F184xx |
PIC12/16F184x | PIC16F188xx | PIC16F191xx |
PIC16F194x | PIC16LF190x | PIC16LF156x |
PIC12/16LF155x | PIC18F-K20 | PIC18F-K22 |
PIC18F-K40 | PIC18F-K42 | PIC18F-K50 |
PIC18F-K80 | PIC18F-K83 | PIC18F-K90 |
PIC18F-Q10 | PIC18F-Q20 | PIC18F-Q24 |
PIC18F-Q40 | PIC18F-Q41 | PIC18F-Q43 |
PIC18F-Q71 | PIC18F-Q83 | PIC18F-Q84 |
3.24.2.1.3 Required header files:
#include "mcc_generated_files/timer/tmr1.h"
3.24.2.1.4 How to use the Timer 1 PLIB
Click the links to view the code snippets associated with each example:
- Timer1 Use Case 1: Timer Period Change This example demonstrates an application where the timer period changes from 4 ms to 8 ms and vice versa with every overflow of the timer.
- Timer1 Use Case 2: LED Toggle at Overflow This example toggles an LED when an overflow interrupt event occurs.
- Timer1 Use Case 3: Timer Wake from Sleep at Interrupt event This example wakes the microcontroller from Sleep mode every five seconds.
3.24.2.1.5 Specific MISRA C:2012 Deviations
- Global TMR1_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 TMR1_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.2.1.6 Module Documentation
TMR1
This file contains API prototypes and other data types for the TMR1 driver.
Module description
This file contains API prototypes and other data types for the TMR1 driver.
This file contains the deprecated macros or APIs for the TMR1 driver.
Definitions
#define TMR1_MAX_COUNT (65535U)
Defines the maximum count value of the timer.
#define TMR1_CLOCK_FREQ (64000000UL)
Defines the TMR1 prescaled clock frequency in hertz.
#define TIMER1_MAX_COUNT TMR1_MAX_COUNT
Defines the Custom Name for the TMR1_MAX_COUNT.
#define TIMER1_CLOCK_FREQ TMR1_CLOCK_FREQ
Defines the Custom Name for the TMR1_CLOCK_FREQ.
#define Timer1_Initialize TMR1_Initialize
Defines the Custom Name for the TMR1_Initialize API.
#define Timer1_Deinitialize TMR1_Deinitialize
Defines the Custom Name for the TMR1_Deinitialize API.
#define Timer1_Start TMR1_Start
Defines the Custom Name for the TMR1_Start API.
#define Timer1_Stop TMR1_Stop
Defines the Custom Name for the TMR1_Stop API.
#define Timer1_CounterGet TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API.
#define Timer1_CounterSet TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API.
#define Timer1_PeriodSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API.
#define Timer1_PeriodGet TMR1_PeriodGet
Defines the Custom Name for the TMR1_PeriodGet API.
#define Timer1_MaxCountGet TMR1_MaxCountGet
Defines the Custom Name for the TMR1_MaxCountGet API.
#define Timer1_Reload TMR1_Reload
Defines the Custom Name for the TMR1_Reload API.
#define Timer1_GateStateGet TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API.
#define Timer1_SinglePulseAcquisitionStart TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API.
#define Timer1_GateCallbackRegister TMR1_GateCallbackRegister
Defines the Custom Name for the TMR1_GateCallbackRegister API.
#define Timer1_OverflowCallbackRegister TMR1_OverflowCallbackRegister
Defines the Custom Name for the TMR1_OverflowCallbackRegister API.
#define Timer1_OverflowStatusGet TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API.
#define Timer1_OverflowStatusClear TMR1_OverflowStatusClear
Defines the Custom Name for the TMR1_OverflowStatusClear API.
#define Timer1_TMRInterruptEnable TMR1_TMRInterruptEnable
Defines the Custom Name for the TMR1_TMRInterruptEnable API.
#define Timer1_TMRInterruptDisable TMR1_TMRInterruptDisable
Defines the Custom Name for the TMR1_TMRInterruptDisable API.
#define Timer1_TMRGInterruptEnable TMR1_TMRGInterruptEnable
Defines the Custom Name for the TMR1_TMRGInterruptEnable API.
#define Timer1_TMRGInterruptDisable TMR1_TMRGInterruptDisable
Defines the Custom Name for the TMR1_TMRGInterruptDisable API.
#define Timer1_Tasks TMR1_Tasks
Defines the Custom Name for the TMR1_Tasks API.
#define TMR1_Read TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API. The TMR1_Read will be deprecated in the future release. Use TMR1_CounterGet instead.
#define TMR1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The TMR1_Write will be deprecated in the future release. Use TMR1_CounterSet instead.
#define TMR1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The TMR1_Write will be deprecated in the future release. Use TMR1_CounterSet instead.
#define TMR1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The TMR1_CheckGateValueStatus will be deprecated in the future release. Use TMR1_GateStateGet instead.
#define TMR1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The TMR1_CheckGateValueStatus will be deprecated in the future release. Use TMR1_GateStateGet instead.
#define Timer1_Read TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API. The Timer1_Read will be deprecated in the future release. Use Timer1_CounterGet instead.
#define Timer1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The Timer1_Write will be deprecated in the future release. Use Timer1_CounterSet instead.
#define TMR1_PeriodCountSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API. The TMR1_PeriodCountSet will be deprecated in the future release. Use TMR1_PeriodSet instead.
#define Timer1_PeriodCountSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API. The Timer1_PeriodCountSet will be deprecated in the future release. Use Timer1_PeriodSet instead.
#define Timer1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The Timer1_CheckGateValueStatus will be deprecated in the future release. Use Timer1_GateStateGet instead.
#define TMR1_StartSinglePulseAcquisition TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API. The TMR1_StartSinglePulseAcquisition will be deprecated in the future release. Use TMR1_SinglePulseAcquisitionStart instead.
#define Timer1_StartSinglePulseAcquisition TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API. The Timer1_StartSinglePulseAcquisition will be deprecated in the future release. Use Timer1_SinglePulseAcquisitionStart instead.
#define TMR1_HasOverflowOccured TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API. The TMR1_HasOverflowOccured will be deprecated in the future release. Use TMR1_OverflowStatusGet instead.
#define Timer1_HasOverflowOccured TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API. The Timer1_HasOverflowOccured will be deprecated in the future release. Use Timer1_OverflowStatusGet instead.
Functions
void TMR1_Initialize (void)
Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.
void TMR1_Deinitialize (void)
Deinitializes the TMR1 to POR values.
void TMR1_Start (void)
Starts the TMR1 timer.
void TMR1_Stop (void)
Stops the TMR1 timer.
uint16_t TMR1_CounterGet (void)
Returns the current counter value.
void TMR1_CounterSet (uint16_t timerVal)
Sets the counter value.
void TMR1_PeriodSet (uint16_t periodVal)
Sets the period count value for the TMR1 timer. The period count is calculated by subtracting the number of ticks required for the period from the maximum count.
uint16_t TMR1_PeriodGet (void)
Returns the period count value.
void TMR1_Reload (void)
Loads the period count value to the TMR1 register.
uint16_t TMR1_MaxCountGet (void)
Returns the TMR1 maximum count value.
void TMR1_SinglePulseAcquisitionStart (void)
Starts the single pulse acquisition in TMR1 gate operation.
uint8_t TMR1_GateStateGet (void)
Returns the TMR1 gate state value.
bool TMR1_OverflowStatusGet (void)
Returns the TMR1 overflow flag status in Non-Interrupt mode.
void TMR1_OverflowStatusClear (void)
Clears the TMR1 overflow flag in Non-Interrupt mode.
void TMR1_TMRInterruptEnable (void)
Enables the TMR1 overflow interrupt.
void TMR1_TMRInterruptDisable (void)
Disables the TMR1 overflow interrupt.
void TMR1_OverflowISR (void)
Interrupt Service Routine (ISR) for the TMR1 overflow interrupt.
void TMR1_TMRGInterruptEnable (void)
Enables the TMR1 gate interrupt.
void TMR1_TMRGInterruptDisable (void)
Disables the TMR1 gate interrupt.
void TMR1_GateISR (void)
Interrupt Service Routine (ISR) for the TMR1 gate event.
void TMR1_Tasks (void)
Performs the tasks to be executed during the TMR1 overflow or gate event.
void TMR1_OverflowCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 overflow event.
void TMR1_GateCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 gate event.
Definition Documentation
Timer1_CheckGateValueStatus
#define Timer1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The Timer1_CheckGateValueStatus will be deprecated in the future release. Use Timer1_GateStateGet instead.
TIMER1_CLOCK_FREQ
#define TIMER1_CLOCK_FREQ TMR1_CLOCK_FREQ
Defines the Custom Name for the TMR1_CLOCK_FREQ.
Timer1_CounterGet
#define Timer1_CounterGet TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API.
Timer1_CounterSet
#define Timer1_CounterSet TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API.
Timer1_Deinitialize
#define Timer1_Deinitialize TMR1_Deinitialize
Defines the Custom Name for the TMR1_Deinitialize API.
Timer1_GateCallbackRegister
#define Timer1_GateCallbackRegister TMR1_GateCallbackRegister
Defines the Custom Name for the TMR1_GateCallbackRegister API.
Timer1_GateStateGet
#define Timer1_GateStateGet TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API.
Timer1_HasOverflowOccured
#define Timer1_HasOverflowOccured TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API. The Timer1_HasOverflowOccured will be deprecated in the future release. Use Timer1_OverflowStatusGet instead.
Timer1_Initialize
#define Timer1_Initialize TMR1_Initialize
Defines the Custom Name for the TMR1_Initialize API.
TIMER1_MAX_COUNT
#define TIMER1_MAX_COUNT TMR1_MAX_COUNT
Defines the Custom Name for the TMR1_MAX_COUNT.
Timer1_MaxCountGet
#define Timer1_MaxCountGet TMR1_MaxCountGet
Defines the Custom Name for the TMR1_MaxCountGet API.
Timer1_OverflowCallbackRegister
#define Timer1_OverflowCallbackRegister TMR1_OverflowCallbackRegister
Defines the Custom Name for the TMR1_OverflowCallbackRegister API.
Timer1_OverflowISR
#define Timer1_OverflowISR TMR1_OverflowISR
Defines the Custom Name for the TMR1_OverflowISR API.
Timer1_OverflowStatusClear
#define Timer1_OverflowStatusClear TMR1_OverflowStatusClear
Defines the Custom Name for the TMR1_OverflowStatusClear API.
Timer1_OverflowStatusGet
#define Timer1_OverflowStatusGet TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API.
Timer1_PeriodCountSet
#define Timer1_PeriodCountSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API. The Timer1_PeriodCountSet will be deprecated in the future release. Use Timer1_PeriodSet instead.
Timer1_PeriodGet
#define Timer1_PeriodGet TMR1_PeriodGet
Defines the Custom Name for the TMR1_PeriodGet API.
Timer1_PeriodSet
#define Timer1_PeriodSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API.
Timer1_Read
#define Timer1_Read TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API. The Timer1_Read will be deprecated in the future release. Use Timer1_CounterGet instead.
Timer1_Reload
#define Timer1_Reload TMR1_Reload
Defines the Custom Name for the TMR1_Reload API.
Timer1_SinglePulseAcquisitionStart
#define Timer1_SinglePulseAcquisitionStart TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API.
Timer1_Start
#define Timer1_Start TMR1_Start
Defines the Custom Name for the TMR1_Start API.
Timer1_StartSinglePulseAcquisition
#define Timer1_StartSinglePulseAcquisition TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API. The Timer1_StartSinglePulseAcquisition will be deprecated in the future release. Use Timer1_SinglePulseAcquisitionStart instead.
Timer1_Stop
#define Timer1_Stop TMR1_Stop
Defines the Custom Name for the TMR1_Stop API.
Timer1_Tasks
#define Timer1_Tasks TMR1_Tasks
Defines the Custom Name for the TMR1_Tasks API.
Timer1_TMRGInterruptDisable
#define Timer1_TMRGInterruptDisable TMR1_TMRGInterruptDisable
Defines the Custom Name for the TMR1_TMRGInterruptDisable API.
Timer1_TMRGInterruptEnable
#define Timer1_TMRGInterruptEnable TMR1_TMRGInterruptEnable
Defines the Custom Name for the TMR1_TMRGInterruptEnable API.
Timer1_TMRInterruptDisable
#define Timer1_TMRInterruptDisable TMR1_TMRInterruptDisable
Defines the Custom Name for the TMR1_TMRInterruptDisable API.
Timer1_TMRInterruptEnable
#define Timer1_TMRInterruptEnable TMR1_TMRInterruptEnable
Defines the Custom Name for the TMR1_TMRInterruptEnable API.
Timer1_Write
#define Timer1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The Timer1_Write will be deprecated in the future release. Use Timer1_CounterSet instead.
TMR1_CheckGateValueStatus
#define TMR1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The TMR1_CheckGateValueStatus will be deprecated in the future release. Use TMR1_GateStateGet instead.
TMR1_CLOCK_FREQ
#define TMR1_CLOCK_FREQ (64000000UL)
Defines the timer prescaled clock frequency in hertz.
TMR1_HasOverflowOccured
#define TMR1_HasOverflowOccured TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API. The TMR1_HasOverflowOccured will be deprecated in the future release. Use TMR1_OverflowStatusGet instead.
TMR1_MAX_COUNT
#define TMR1_MAX_COUNT (65535U)
Defines the maximum count value of the timer.
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.TMR1_PeriodCountSet
#define TMR1_PeriodCountSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API. The TMR1_PeriodCountSet will be deprecated in the future release. Use TMR1_PeriodSet instead.
TMR1_Read
#define TMR1_Read TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API. The TMR1_Read will be deprecated in the future release. Use TMR1_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.TMR1_StartSinglePulseAcquisition
#define TMR1_StartSinglePulseAcquisition TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API. The TMR1_StartSinglePulseAcquisition will be deprecated in the future release. Use TMR1_SinglePulseAcquisitionStart instead.
TMR1_Write
#define TMR1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The TMR1_Write will be deprecated in the future release. Use TMR1_CounterSet instead.
Function Documentation
TMR1_CounterGet()
uint32_t TMR1_CounterGet (void )
Returns the current counter value.
Initialize TMR1 with TMR1_Initialize() before calling this API. |
None. |
Counter value from the TMR1 register |
TMR1_CounterSet()
void TMR1_CounterSet (uint32_t timerVal)
Sets the counter value.
Initialize TMR1 with TMR1_Initialize() before calling this API. |
timerVal |
- Counter value to be written to the TMR1 register |
None. |
TMR1_Deinitialize()
void TMR1_Deinitialize (void )
Deinitializes the TMR1 to POR values.
None. |
None. |
TMR1_GateCallbackRegister()
void TMR1_GateCallbackRegister (void(*)(void) CallbackHandler)
Registers a callback function for the TMR1 gate event.
CallbackHandler |
- Address of the custom callback function |
None. |
TMR1_GateISR()
void TMR1_GateISR (void )
Interrupt Service Routine (ISR) for the TMR1 gate event.
None. |
None. |
TMR1_GateStateGet()
uint8_t TMR1_GateStateGet (void )
Returns the TMR1 gate state value.
Use this function only the TMR1 gate is enabled. |
None. |
Gate state value |
TMR1_Initialize()
void TMR1_Initialize (void )
Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.
None. |
None. |
TMR1_MaxCountGet()
uint16_t TMR1_MaxCountGet (void )
Returns the TMR1 maximum count value.
None. |
Maximum count value of the timer |
TMR1_OverflowCallbackRegister()
void TMR1_OverflowCallbackRegister (void(*)(void) CallbackHandler)
Registers a callback function for the TMR1 overflow event.
CallbackHandler |
- Address of the custom callback function |
None. |
TMR1_OverflowISR()
void TMR1_OverflowISR (void )
Interrupt Service Routine (ISR) for the TMR1 overflow interrupt.
None. |
None. |
TMR1_OverflowStatusClear()
void TMR1_OverflowStatusClear (void )
Clears the TMR1 overflow flag in Non-Interrupt mode.
None. |
None. |
TMR1_OverflowStatusGet()
bool TMR1_OverflowStatusGet (void )
Returns the TMR1 overflow flag status in Non-Interrupt mode.
None. |
True |
- Timer overflow has occurred |
False |
- Timer overflow has not occurred |
TMR1_PeriodGet()
uint16_t TMR1_PeriodGet (void )
Returns the period count value.
None. |
Period count value |
TMR1_PeriodSet()
void TMR1_PeriodSet (uint16_t periodVal)
Sets the period count value for the TMR1 timer. The period count is calculated by subtracting the number of ticks required for the period from the maximum count.
periodVal |
- Period value to be stored in the timer period variable |
None. |
TMR1_Reload()
void TMR1_Reload (void )
Loads the period count value to the TMR1 register.
Initialize TMR1 with TMR1_Initialize before calling this API. |
None. |
None. |
TMR1_SinglePulseAcquisitionStart()
void TMR1_SinglePulseAcquisitionStart (void )
Starts the single pulse acquisition in TMR1 gate operation.
Use this function only the TMR1 gate is enabled. |
None. |
None. |
TMR1_Start()
void TMR1_Start (void )
Starts the TMR1 timer.
Initialize TMR1 with TMR1_Initialize() before calling this API. |
None. |
None. |
TMR1_Stop()
void TMR1_Stop (void )
Stops the TMR1 timer.
Initialize TMR1 with TMR1_Initialize() before calling this API. |
None. |
None. |
TMR1_Tasks()
void TMR1_Tasks (void )
Performs the tasks to be executed during the TMR1 overflow or gate event.
None. |
None. |
TMR1_TMRGInterruptDisable()
void TMR1_TMRGInterruptDisable (void )
Disables the TMR1 gate interrupt.
None. |
None. |
TMR1_TMRGInterruptEnable()
void TMR1_TMRGInterruptEnable (void )
Enables the TMR1 gate interrupt.
None. |
None. |
TMR1_TMRInterruptDisable()
void TMR1_TMRInterruptDisable (void )
Disables the TMR1 overflow interrupt.
None. |
None. |
TMR1_TMRInterruptEnable()
void TMR1_TMRInterruptEnable (void )
Enables the TMR1 overflow interrupt.
None. |
None. |
3.24.2.1.7 TMR1 Examples
This section explains various use case examples of the Timer module within an MPLAB® Code Configurator (MCC) Melody Project.
TMR1 PLIB Examples
4 ms/8 ms Timer Period Change in Non-Interrupt Mode
This use case configures the TMR1 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>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"
-
-
Drivers>TMR1:
-
Timer Enable: False
-
Prescaler: 1:8
-
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) * TMR1 module: Timer Enable - False, Prescaler - 1:8, TMR Interrupt Enable - False * Pins module: RF3 - GPIO Output, CustomName - LED */ #define USE_TASKS_API 0 #define MS_TO_TICKS(ms) (((TMR1_CLOCK_FREQ * (ms)) / 1000UL) - 1UL) #define LED_4_MS (TMR1_MAX_COUNT - MS_TO_TICKS(4UL)) #define LED_8_MS (TMR1_MAX_COUNT - MS_TO_TICKS(8UL)) static void Timer_PeriodChange(void) { static volatile bool changePeriod = false; LED_Toggle(); TMR1_Stop(); uint16_t maxCount = TMR1_MaxCountGet(); uint16_t newPeriod = changePeriod ? LED_4_MS:LED_8_MS; if(maxCount > newPeriod) { TMR1_PeriodSet(newPeriod); TMR1_CounterSet(newPeriod); } else { // Invalid period } changePeriod = !changePeriod; TMR1_Start(); } int main(void) { SYSTEM_Initialize(); TMR1_PeriodSet(LED_4_MS); TMR1_CounterSet(LED_4_MS); TMR1_OverflowStatusClear(); #if USE_TASKS_API TMR1_OverflowCallbackRegister(Timer_PeriodChange); #endif TMR1_Start(); while(1) { #if USE_TASKS_API TMR1_Tasks(); #else if(1U == TMR1_OverflowStatusGet()) { Timer_PeriodChange(); TMR1_OverflowStatusClear(); } #endif } }
4 ms Timer Period in Interrupt Mode
This use case configures the TMR1 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>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"
-
-
Drivers>TMR1:
-
Timer Enable: False
-
Prescaler: 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) * TMR1 module: Timer Enable - False, Prescaler - 1:8, TMR Interrupt Enable - True * Pins module: RF3 - GPIO Output, CustomName - LED */ #define MS_TO_TICKS(ms) (((TMR1_CLOCK_FREQ * (ms)) / 1000UL) - 1UL) #define LED_4_MS (TMR1_MAX_COUNT - (MS_TO_TICKS(4UL))) void Custom_Callback(void) { LED_Toggle(); } int main(void) { SYSTEM_Initialize(); if(TMR1_MaxCountGet() > LED_4_MS) { TMR1_CounterSet(LED_4_MS); TMR1_PeriodSet(LED_4_MS); } else { // Invalid period // Generate default timer period } TMR1_OverflowCallbackRegister(Custom_Callback); TMR1_Start(); INTERRUPT_GlobalInterruptEnable(); while(1) { } }
Timer Wake From Sleep at Every 5s
This use case configures the TMR1 PLIB to wake the microcontroller from Sleep mode every five seconds. The wake up event toggles an LED, then puts the MCU back to Sleep.
-
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"
-
-
Drivers>TMR1:
-
Timer Enable: True
-
Clock Source: LFINTOSC
-
Prescaler: 1:4
-
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) * TMR1 module: Timer Enable - True, Clock Source - LFINTOSC, Prescaler - 1:4, 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.2.1.8 File Documentation
source/tmr1.c File Reference
Driver implementation for the TMR1 driver.
#include <xc.h> #include "../tmr1.h"
Functions
static void TMR1_DefaultOverflowCallback (void)
static void TMR1_DefaultGateCallback (void)
void TMR1_Initialize (void)
Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.
void TMR1_Deinitialize (void)
Deinitializes the TMR1 to POR values.
void TMR1_Start (void)
Starts the TMR1 timer.
void TMR1_Stop (void)
Stops the TMR1 timer.
uint16_t TMR1_CounterGet (void)
Returns the current counter value.
void TMR1_CounterSet (uint16_t timerVal)
Sets the counter value.
void TMR1_PeriodSet (uint16_t periodVal)
Sets the period count value for the TMR1 timer. The period count is calculated by subtracting the number of ticks required for the period from the maximum count.
uint16_t TMR1_PeriodGet (void)
Returns the period count value.
void TMR1_Reload (void)
Loads the period count value to the TMR1 register.
uint16_t TMR1_MaxCountGet (void)
Returns the TMR1 maximum count value.
void TMR1_SinglePulseAcquisitionStart (void)
Starts the single pulse acquisition in TMR1 gate operation.
uint8_t TMR1_GateStateGet (void)
Returns the TMR1 gate state value.
bool TMR1_OverflowStatusGet (void)
Returns the TMR1 overflow flag status in Non-Interrupt mode.
void TMR1_OverflowStatusClear (void)
Clears the TMR1 overflow flag in Non-Interrupt mode.
void TMR1_Tasks (void)
Performs the tasks to be executed during the TMR1 overflow or gate event.
void TMR1_OverflowCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 overflow event.
void TMR1_GateCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 gate event.
Variables
static volatile uint16_t timer1ReloadVal
static void(* TMR1_OverflowCallback )(void)
static void(* TMR1_GateCallback )(void)
Detailed Description
Driver implementation for the TMR1 driver.
Driver implementation for the TMR1 Timer driver.
TMR1 Generated Driver File
TMR1 Generated Timer Driver File
Function Documentation
TMR1_DefaultGateCallback()
static void TMR1_DefaultGateCallback (void )[static]
TMR1_DefaultOverflowCallback()
static void TMR1_DefaultOverflowCallback (void )[static]
Variable Documentation
timer1ReloadVal
volatile uint16_t timer1ReloadVal[static]
Section: Included Files
TMR1_GateCallback
void(* TMR1_GateCallback) (void)[static]
TMR1_OverflowCallback
void(* TMR1_OverflowCallback) (void)[static]
source/tmr1.h File Reference
This file contains API prototypes and other data types for the TMR1 Timer driver.
#include <stdbool.h> #include <stdint.h> #include "tmr1_deprecated.h"
Functions
-
void TMR1_Initialize (void)
Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.
-
void TMR1_Deinitialize (void)
Deinitializes the TMR1 to POR values.
-
void TMR1_Start (void)
Starts the TMR1 timer.
-
void TMR1_Stop (void)
Stops the TMR1 timer.
-
uint16_t TMR1_CounterGet (void)
Returns the current counter value.
-
void TMR1_CounterSet (uint16_t timerVal)
Sets the counter value.
-
void TMR1_PeriodSet (uint16_t periodVal)
Sets the period count value for the TMR1 timer. The period count is calculated by subtracting the number of ticks required for the period from the maximum count.
-
uint16_t TMR1_PeriodGet (void)
Returns the period count value.
-
void TMR1_Reload (void)
Loads the period count value to the TMR1 register.
-
uint16_t TMR1_MaxCountGet (void)
Returns the TMR1 maximum count value.
-
void TMR1_SinglePulseAcquisitionStart (void)
Starts the single pulse acquisition in TMR1 gate operation.
-
uint8_t TMR1_GateStateGet (void)
Returns the TMR1 gate state value.
-
bool TMR1_OverflowStatusGet (void)
Returns the TMR1 overflow flag status in Non-Interrupt mode.
-
void TMR1_OverflowStatusClear (void)
Clears the TMR1 overflow flag in Non-Interrupt mode.
-
void TMR1_TMRInterruptEnable (void)
Enables the TMR1 overflow interrupt.
-
void TMR1_TMRInterruptDisable (void)
Disables the TMR1 overflow interrupt.
-
void TMR1_OverflowISR (void)
Interrupt Service Routine (ISR) for the TMR1 overflow interrupt.
-
void TMR1_TMRGInterruptEnable (void)
Enables the TMR1 gate interrupt.
-
void TMR1_TMRGInterruptDisable (void)
Disables the TMR1 gate interrupt.
-
void TMR1_GateISR (void)
Interrupt Service Routine (ISR) for the TMR1 gate event.
-
void TMR1_Tasks (void)
Performs the tasks to be executed during the TMR1 overflow or gate event.
-
void TMR1_OverflowCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 overflow event.
-
void TMR1_GateCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 gate event.
Macros
#define TMR1_MAX_COUNT (65535U)
Defines the maximum count value of the timer.
#define TMR1_CLOCK_FREQ (64000000UL)
Defines the TMR1 prescaled clock frequency in hertz.
#define TIMER1_MAX_COUNT TMR1_MAX_COUNT
Defines the Custom Name for the TMR1_MAX_COUNT.
#define TIMER1_CLOCK_FREQ TMR1_CLOCK_FREQ
Defines the Custom Name for the TMR1_CLOCK_FREQ.
#define Timer1_Initialize TMR1_Initialize
Defines the Custom Name for the TMR1_Initialize API.
#define Timer1_Deinitialize TMR1_Deinitialize
Defines the Custom Name for the TMR1_Deinitialize API.
#define Timer1_Start TMR1_Start
Defines the Custom Name for the TMR1_Start API.
#define Timer1_Stop TMR1_Stop
Defines the Custom Name for the TMR1_Stop API.
#define Timer1_CounterGet TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API.
#define Timer1_CounterSet TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API.
#define Timer1_PeriodSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API.
#define Timer1_PeriodGet TMR1_PeriodGet
Defines the Custom Name for the TMR1_PeriodGet API.
#define Timer1_MaxCountGet TMR1_MaxCountGet
Defines the Custom Name for the TMR1_MaxCountGet API.
#define Timer1_Reload TMR1_Reload
Defines the Custom Name for the TMR1_Reload API.
#define Timer1_GateStateGet TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API.
#define Timer1_SinglePulseAcquisitionStart TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API.
#define Timer1_GateCallbackRegister TMR1_GateCallbackRegister
Defines the Custom Name for the TMR1_GateCallbackRegister API.
#define Timer1_OverflowCallbackRegister TMR1_OverflowCallbackRegister
Defines the Custom Name for the TMR1_OverflowCallbackRegister API.
#define Timer1_OverflowStatusGet TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API.
#define Timer1_OverflowStatusClear TMR1_OverflowStatusClear
Defines the Custom Name for the TMR1_OverflowStatusClear API.
#define Timer1_TMRInterruptEnable TMR1_TMRInterruptEnable
Defines the Custom Name for the TMR1_TMRInterruptEnable API.
#define Timer1_TMRInterruptDisable TMR1_TMRInterruptDisable
Defines the Custom Name for the TMR1_TMRInterruptDisable API.
#define Timer1_TMRGInterruptEnable TMR1_TMRGInterruptEnable
Defines the Custom Name for the TMR1_TMRGInterruptEnable API.
#define Timer1_TMRGInterruptDisable TMR1_TMRGInterruptDisable
Defines the Custom Name for the TMR1_TMRGInterruptDisable API.
#define Timer1_Tasks TMR1_Tasks
Defines the Custom Name for the TMR1_Tasks API.
Detailed Description
This file contains API prototypes and other data types for the TMR1 Timer driver.
TMR1 Generated Driver API Header File
TMR1 Generated Timer Driver API Header File
source/tmr1_deprecated.h File Reference
Macros
#define TMR1_Read TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API. The TMR1_Read will be deprecated in the future release. Use TMR1_CounterGet instead.
#define TMR1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The TMR1_Write will be deprecated in the future release. Use TMR1_CounterSet instead.
#define TMR1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The TMR1_CheckGateValueStatus will be deprecated in the future release. Use TMR1_GateStateGet instead.
#define Timer1_Read TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API. The Timer1_Read will be deprecated in the future release. Use Timer1_CounterGet instead.
#define TMR1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The TMR1_Write will be deprecated in the future release. Use TMR1_CounterSet instead.
#define Timer1_Write TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API. The Timer1_Write will be deprecated in the future release. Use Timer1_CounterSet instead.
#define TMR1_PeriodCountSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API. The TMR1_PeriodCountSet will be deprecated in the future release. Use TMR1_PeriodSet instead.
#define Timer1_PeriodCountSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API. The Timer1_PeriodCountSet will be deprecated in the future release. Use Timer1_PeriodSet instead.
#define TMR1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The TMR1_CheckGateValueStatus will be deprecated in the future release. Use TMR1_GateStateGet instead.
#define Timer1_CheckGateValueStatus TMR1_GateStateGet
Defines the Custom Name for the TMR1_GateStateGet API. The Timer1_CheckGateValueStatus will be deprecated in the future release. Use Timer1_GateStateGet instead.
#define TMR1_StartSinglePulseAcquisition TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API. The TMR1_StartSinglePulseAcquisition will be deprecated in the future release. Use TMR1_SinglePulseAcquisitionStart instead.
#define Timer1_StartSinglePulseAcquisition TMR1_SinglePulseAcquisitionStart
Defines the Custom Name for the TMR1_SinglePulseAcquisitionStart API. The Timer1_StartSinglePulseAcquisition will be deprecated in the future release. Use Timer1_SinglePulseAcquisitionStart instead.
#define TMR1_HasOverflowOccured TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API. The TMR1_HasOverflowOccured will be deprecated in the future release. Use TMR1_OverflowStatusGet instead.
#define Timer1_HasOverflowOccured TMR1_OverflowStatusGet
Defines the Custom Name for the TMR1_OverflowStatusGet API. The Timer1_HasOverflowOccured will be deprecated in the future release. Use Timer1_OverflowStatusGet instead.
source/tmr1_drv.c File Reference
#include <xc.h> #include "../tmr1.h"
Functions
static void TMR1_DefaultOverflowCallback (void)
-
void TMR1_Initialize (void)
Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.
-
void TMR1_Deinitialize (void)
Deinitializes the TMR1 to POR values.
-
void TMR1_Start (void)
Starts the TMR1 timer.
-
void TMR1_Stop (void)
Stops the TMR1 timer.
-
uint16_t TMR1_CounterGet (void)
Returns the current counter value.
-
void TMR1_CounterSet (uint16_t timerVal)
Sets the counter value.
-
void TMR1_PeriodSet (uint16_t periodVal)
Sets the period count value for the TMR1 timer. The period count is calculated by subtracting the number of ticks required for the period from the maximum count.
-
uint16_t TMR1_PeriodGet (void)
Returns the period count value.
uint32_t TMR1_MaxCountGet (void)
Returns the TMR1 maximum count value.
void TMR1_OverflowISR (void)
Interrupt Service Routine (ISR) for the TMR1 overflow interrupt.
void TMR1_OverflowCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 overflow event.
Variables
const struct TIMER_INTERFACE Timer1
static volatile uint16_t timer1ReloadVal
static void(* TMR1_OverflowCallback )(void)
Function Documentation
TMR1_DefaultOverflowCallback()
static void TMR1_DefaultOverflowCallback (void )[static]
Variable Documentation
Timer1
const struct TIMER_INTERFACE Timer1
Initial value:
= { .Initialize = TMR1_Initialize, .Deinitialize = TMR1_Deinitialize, .Start = TMR1_Start, .Stop = TMR1_Stop, .PeriodSet = TMR1_PeriodSet, .PeriodGet = TMR1_PeriodGet, .CounterGet = TMR1_CounterGet, .CounterSet = TMR1_CounterSet, .MaxCountGet = TMR1_MaxCountGet, .TimeoutCallbackRegister = TMR1_OverflowCallbackRegister, .Tasks = NULL }
Section: Included Files
timer1ReloadVal
volatile uint16_t timer1ReloadVal[static]
TMR1_OverflowCallback
void(* TMR1_OverflowCallback) (void)[static]
source/tmr1_drv.h File Reference
#include <stdint.h> #include <stdbool.h> #include "timer_interface.h"
Functions
void TMR1_Initialize (void)
Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.
void TMR1_Deinitialize (void)
Deinitializes the TMR1 module.
void TMR1_Start (void)
Starts the TMR1 timer.
void TMR1_Stop (void)
Stops the TMR1 timer.
uint32_t TMR1_CounterGet (void)
Returns the current counter value.
void TMR1_CounterSet (uint32_t timerVal)
Sets the counter value.
void TMR1_PeriodSet (uint32_t periodVal)
Sets the period count value.
uint32_t TMR1_PeriodGet (void)
Returns the current period value.
uint32_t TMR1_MaxCountGet (void)
Returns the maximum count value of timer.
void TMR1_OverflowISR (void)
Interrupt Service Routine (ISR) for the TMR1 overflow interrupt.
void TMR1_Tasks (void)
Performs tasks to be executed during the TMR1 overflow event.
void TMR1_OverflowCallbackRegister (void(*CallbackHandler)(void))
Registers a callback function for the TMR1 overflow event.
Macros
#define TMR1_MAX_COUNT (65535U)
Defines the maximum count value of the timer.
#define TMR1_CLOCK_FREQ (64000000UL)
Defines the timer prescaled clock frequency in hertz.
#define TIMER1_MAX_COUNT TMR1_MAX_COUNT
Defines the Custom Name for the TMR1_MAX_COUNT.
#define TIMER1_CLOCK_FREQ TMR1_CLOCK_FREQ
Defines the Custom Name for the TMR1_CLOCK_FREQ.
-
#define Timer1_Initialize TMR1_Initialize
Defines the Custom Name for the TMR1_Initialize API.
-
#define Timer1_Deinitialize TMR1_Deinitialize
Defines the Custom Name for the TMR1_Deinitialize API.
-
#define Timer1_Start TMR1_Start
Defines the Custom Name for the TMR1_Start API.
-
#define Timer1_Stop TMR1_Stop
Defines the Custom Name for the TMR1_Stop API.
-
#define Timer1_CounterGet TMR1_CounterGet
Defines the Custom Name for the TMR1_CounterGet API.
-
#define Timer1_CounterSet TMR1_CounterSet
Defines the Custom Name for the TMR1_CounterSet API.
-
#define Timer1_PeriodSet TMR1_PeriodSet
Defines the Custom Name for the TMR1_PeriodSet API.
-
#define Timer1_PeriodGet TMR1_PeriodGet
Defines the Custom Name for the TMR1_PeriodGet API.
#define Timer1_MaxCountGet TMR1_MaxCountGet
Defines the Custom Name for the TMR1_MaxCountGet API.
#define Timer1_OverflowISR TMR1_OverflowISR
Defines the Custom Name for the TMR1_OverflowISR API.
#define Timer1_Tasks TMR1_Tasks
Defines the Custom Name for the TMR1_Tasks API.
#define Timer1_OverflowCallbackRegister TMR1_OverflowCallbackRegister
Defines the Custom Name for the TMR1_OverflowCallbackRegister API.
Variables
const struct TIMER_INTERFACE Timer1
Variable Documentation
Timer1
const struct TIMER_INTERFACE Timer1
Section: Included Files