4.24.2.1 TMR1

Timer1

4.24.2.1.1 Introduction

The Timer1 (TMR1) module is a 16-bit timer with configurable timeout period and gate control features.

4.24.2.1.2 Supported Device Families

PIC16F145xPIC12/16F150xPIC16F151x
PIC16F152xPIC16F152xxPIC16F153xx
PIC12/16F157xPIC12/16F161xPIC16F170x
PIC16F171xPIC16F171xxPIC16F176x
PIC16F177xPIC16F178xPIC16F180xx
PIC16F181xxPIC16F183xxPIC16F184xx
PIC12/16F184xPIC16F188xxPIC16F191xx
PIC16F194xPIC16LF190xPIC16LF156x
PIC12/16LF155xPIC18F-K20PIC18F-K22
PIC18F-K40PIC18F-K42PIC18F-K50
PIC18F-K80PIC18F-K83PIC18F-K90
PIC18F-Q10PIC18F-Q20PIC18F-Q24
PIC18F-Q40PIC18F-Q41PIC18F-Q43
PIC18F-Q71PIC18F-Q83PIC18F-Q84

4.24.2.1.3 Required header files:

#include "mcc_generated_files/timer/tmr1.h"

4.24.2.1.4 How to use the Timer 1 PLIB

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

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

4.24.2.1.6 Module Documentation

Timer Driver

The Timer driver is an 8-bit, 16-bit or 32-bit timer that can operate as a free-running interval timer using PIC®, dsPIC® and AVR® microcontrollers (MCUs).

Module description

The Timer driver is an 8-bit, 16-bit or 32-bit timer that can operate as a free-running interval timer using PIC®, dsPIC® and AVR® microcontrollers (MCUs).

Data structures
  • struct TIMER_INTERFACE

    Structure containing the function pointers of the Timer driver.

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

Definition Documentation

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_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_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_Start

#define Timer1_Start TMR1_Start

Defines the Custom Name for the TMR1_Start API.

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.

TIMER_INTERRUPT_PRIORITY_SUPPORT

#define TIMER_INTERRUPT_PRIORITY_SUPPORT 0

The InterruptPrioritySet API is not supported.

TIMER_PERIODCOUNTSET_API_SUPPORT

#define TIMER_PERIODCOUNTSET_API_SUPPORT 0

Defines the deprecated SCCPx_Timer_PeriodCountSet or TMRx_PeriodCountSet API. Set it to 1 for backward compatibility for 16-bit MCUs only.

TMR1_CLOCK_FREQ

#define TMR1_CLOCK_FREQ (64000000UL)

Defines the timer prescaled clock frequency in hertz.

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.

Function Documentation

TMR1_CounterGet()

uint32_t TMR1_CounterGet (void )

Returns the current counter value.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
None.
Returns:

Counter value from the TMR1 register

TMR1_CounterSet()

void TMR1_CounterSet (uint32_t timerVal)

Sets the counter value.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
timerVal

- Counter value to be written to the TMR1 register

Returns:

None.

TMR1_Deinitialize()

void TMR1_Deinitialize (void )

Deinitializes the TMR1 to POR values.

Parameters:
None.
Returns:

None.

TMR1_Initialize()

void TMR1_Initialize (void )

Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.

Parameters:
None.
Returns:

None.

TMR1_MaxCountGet()

uint16_t TMR1_MaxCountGet (void )

Returns the TMR1 maximum count value.

Parameters:
None.
Returns:

Maximum count value of the timer

TMR1_OverflowCallbackRegister()

void TMR1_OverflowCallbackRegister (void(*)(void) CallbackHandler)

Registers a callback function for the TMR1 overflow event.

Parameters:
CallbackHandler

- Address of the custom callback function

Returns:

None.

TMR1_OverflowISR()

void TMR1_OverflowISR (void )

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

Parameters:
None.
Returns:

None.

TMR1_PeriodGet()

uint16_t TMR1_PeriodGet (void )

Returns the period count value.

Parameters:
None.
Returns:

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.

Parameters:
periodVal

- Period value to be stored in the timer period variable

Returns:

None.

TMR1_Start()

void TMR1_Start (void )

Starts the TMR1 timer.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR1_Stop()

void TMR1_Stop (void )

Stops the TMR1 timer.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR1_Tasks()

void TMR1_Tasks (void )

Performs the tasks to be executed during the TMR1 overflow or gate event.

Parameters:
None.
Returns:

None.

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.

Version: TMR1 Driver Version 4.0.0 Package Version 5.0.0
Definitions
Functions

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_GateEventStatusClear

#define Timer1_GateEventStatusClear TMR1_GateEventStatusClear

Defines the Custom Name for the TMR1_GateEventStatusClear API.

Timer1_GateEventStatusGet

#define Timer1_GateEventStatusGet TMR1_GateEventStatusGet

Defines the Custom Name for the TMR1_GateEventStatusGet 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_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_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 TMR1 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.

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

uint16_t TMR1_CounterGet (void )

Returns the current counter value.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
None.
Returns:

Counter value from the TMR1 register

TMR1_CounterSet()

void TMR1_CounterSet (uint16_t timerVal)

Sets the counter value.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
timerVal

- Counter value to be written to the TMR1 register

Returns:

None.

TMR1_Deinitialize()

void TMR1_Deinitialize (void )

Deinitializes the TMR1 to POR values.

Parameters:
None.
Returns:

None.

TMR1_GateCallbackRegister()

void TMR1_GateCallbackRegister (void(*)(void) CallbackHandler)

Registers a callback function for the TMR1 gate event.

Parameters:
CallbackHandler

- Address of the custom callback function

Returns:

None.

TMR1_GateEventStatusClear()

void TMR1_GateEventStatusClear (void )

Clears the TMR1 Gate flag in Non-Interrupt mode.

Parameters:
None.
Returns:

None.

TMR1_GateEventStatusGet()

bool TMR1_GateEventStatusGet (void )

Returns the TMR1 Gate flag status in Non-Interrupt mode.

Parameters:
None.
Return values:
True

- Timer Gate Event has occurred

False

- Timer Gate Event has not occurred

TMR1_GateISR()

void TMR1_GateISR (void )

Interrupt Service Routine (ISR) for the TMR1 gate event.

Parameters:
None.
Returns:

None.

TMR1_GateStateGet()

uint8_t TMR1_GateStateGet (void )

Returns the TMR1 gate state value.

Precondition:

Use this function only the TMR1 gate is enabled.

Parameters:
None.
Returns:

Gate state value

TMR1_Initialize()

void TMR1_Initialize (void )

Initializes the Timer1 (TMR1) module. This routine must be called before any other TMR1 routines.

Parameters:
None.
Returns:

None.

TMR1_MaxCountGet()

uint16_t TMR1_MaxCountGet (void )

Returns the TMR1 maximum count value.

Parameters:
None.
Returns:

Maximum count value of the timer

TMR1_OverflowCallbackRegister()

void TMR1_OverflowCallbackRegister (void(*)(void) CallbackHandler)

Registers a callback function for the TMR1 overflow event.

Parameters:
CallbackHandler

- Address of the custom callback function

Returns:

None.

TMR1_OverflowISR()

void TMR1_OverflowISR (void )

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

Parameters:
None.
Returns:

None.

TMR1_OverflowStatusClear()

void TMR1_OverflowStatusClear (void )

Clears the TMR1 overflow flag in Non-Interrupt mode.

Parameters:
None.
Returns:

None.

TMR1_OverflowStatusGet()

bool TMR1_OverflowStatusGet (void )

Returns the TMR1 overflow flag status in Non-Interrupt mode.

Parameters:
None.
Return values:
True

- Timer overflow has occurred

False

- Timer overflow has not occurred

TMR1_PeriodGet()

uint16_t TMR1_PeriodGet (void )

Returns the period count value.

Parameters:
None.
Returns:

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.

Parameters:
periodVal

- Period value to be stored in the timer period variable

Returns:

None.

TMR1_Reload()

void TMR1_Reload (void )

Loads the period count value to the TMR1 register.

Precondition:

Initialize TMR1 with TMR1_Initialize before calling this API.

Parameters:
None.
Returns:

None.

TMR1_SinglePulseAcquisitionStart()

void TMR1_SinglePulseAcquisitionStart (void )

Starts the single pulse acquisition in TMR1 gate operation.

Precondition:

Use this function only the TMR1 gate is enabled.

Parameters:
None.
Returns:

None.

TMR1_Start()

void TMR1_Start (void )

Starts the TMR1 timer.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR1_Stop()

void TMR1_Stop (void )

Stops the TMR1 timer.

Precondition:

Initialize TMR1 with TMR1_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR1_Tasks()

void TMR1_Tasks (void )

Performs the tasks to be executed during the TMR1 overflow or gate event.

Parameters:
None.
Returns:

None.

TMR1_TMRGInterruptDisable()

void TMR1_TMRGInterruptDisable (void )

Disables the TMR1 gate interrupt.

Parameters:
None.
Returns:

None.

TMR1_TMRGInterruptEnable()

void TMR1_TMRGInterruptEnable (void )

Enables the TMR1 gate interrupt.

Parameters:
None.
Returns:

None.

TMR1_TMRInterruptDisable()

void TMR1_TMRInterruptDisable (void )

Disables the TMR1 overflow interrupt.

Parameters:
None.
Returns:

None.

TMR1_TMRInterruptEnable()

void TMR1_TMRInterruptEnable (void )

Enables the TMR1 overflow interrupt.

Parameters:
None.
Returns:

None.

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

Timer Driver Examples

40 ms/80 ms Timer Period Change When Switch Is Pressed

This use case sets up a project which changes the time-out period between 40 ms and 80 ms, when a switch is pressed.

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"

    • Pin Grid View: Select SWITCH pin as input (check the schematic for your board)

    • Pins: Rename Custom Name to "SW"

    • Enable Weak Pullup if needed

Timer Configuration
  • Drivers>Timer:
    • Timer PLIB Selector: TMR1

    • Timer Enable: False

    • Interrupt Driven: True

    • Requested Period: 100 ms

  • Drivers>TMR1:
    • Clock Source: MFINTOSC_500kHz

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)
 *     Timer module: Timer PLIB Selector - TMR1, Timer Enable - False, Interrupt Driven - True, Requested Timer Period - 100 ms
 *     TMR1 module: Clock Source - MFINTOSC_500KHz, Timer_PeriodSet() API Range - [2 us - 131.072 ms] (read-only)
 *     Pins module: RF3 - GPIO Output, CustomName - LED
 *                  RB4 - GPIO Input, CustomName - SW, Weak Pull Up - Enable 
 */

#define SW_PRESSED 0U          // Switch - Active Low
#define SW_NOT_PRESSED 1U 
#define DEBONCE_DELAY_MS 20U      // Depends on hardware

#define MS_TO_TICKS(ms) (((TMR1_CLOCK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_40_MS (MS_TO_TICKS(40UL))           
#define LED_80_MS (MS_TO_TICKS(80UL))

static const struct TIMER_INTERFACE *Timer = &Timer1;

bool SW_StateGet(void)
{
    bool status = SW_NOT_PRESSED;
    if(SW_PRESSED == SW_GetValue())
    {
        __delay_ms(DEBONCE_DELAY_MS);
        if(SW_PRESSED == SW_GetValue())
        {
            status = SW_PRESSED;
        }        
    }
    return status;
}

void Timer_FrequencyChange(void)
{
    static volatile bool changePeriod = false;
    Timer->Stop();
    
    uint32_t maxCount = Timer->MaxCountGet();
    uint32_t newPeriod = changePeriod ? LED_40_MS:LED_80_MS;
    
    if(maxCount > newPeriod)
    {      
        Timer->PeriodSet(newPeriod);
    }
    else
    {
        // Invalid period
    }
    changePeriod = !changePeriod;
    Timer->Start(); 
}

void Timer_Callback(void)
{
    LED_Toggle();
}

int main(void)
{
    SYSTEM_Initialize();

    Timer->PeriodSet(LED_40_MS);
    Timer->TimeoutCallbackRegister(Timer_Callback);
    Timer->Start();    
  
    // Enable the Global Interrupts 
    INTERRUPT_GlobalInterruptEnable(); 
    
    while(1)
    {
        if(SW_PRESSED == SW_StateGet())
        {
            Timer_FrequencyChange();
        }
    }    
}

A Simple Task Scheduler That Executes Two Different Tasks At Different Intervals Using Timer Interrupt

This use case sets up a project which executes two different tasks at different intervals using a timer interrupt.

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

    • HF Internal Clock: 64 MHz

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

    • Pins: Rename Custom Name to "LED1"

    • Pin Grid View: Select LED2 pin as output (Check the schematic for your board)

    • Pins: Rename Custom Name to "LED2"

Timer Configuration
  • Drivers>Timer:
    • Timer PLIB Selector: TMR1

    • Timer Enable: False

    • Interrupt Driven: True

    • Requested Period: 1 ms

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 (64MHz)
 *     Timer module: Timer PLIB Selector - TMR1, Timer Enable - False, Interrupt Driven - True, Requested Timer Period - 1 ms
 *     Pins module: RF2, RF3 - GPIO Output, CustomName - RF2->LED1, RF3->LED2 
 */

static void Task_A_2ms(void)
{
    LED1_SetHigh();
    NOP();
    NOP();
    LED1_SetLow();
    
    // Add your code here. E.g. LED Toggle
}
static void Task_B_5ms(void)
{
    LED2_SetHigh();
    NOP();
    NOP();
    LED2_SetLow();  
    
    // Add your code here. E.g. Read Sensor
}
static void Tasks_Scheduler(void)
{
    static volatile uint8_t oneMs_tickCounter =  0;
    
    oneMs_tickCounter++;
            
    if((oneMs_tickCounter % 2U) == 0U)
    {
        Task_A_2ms();    // Run at 2 ms, 4 ms, 6 ms, 8 ms, and 10 ms.
    }
    
    if((oneMs_tickCounter % 5U) == 0U)
    {
        Task_B_5ms();    // Run at 5 ms and 10 ms.
    }
    if(oneMs_tickCounter == 10U)
    {
        oneMs_tickCounter = 0;
    }
}

int main(void)
{
    const struct TIMER_INTERFACE *Timer = &Timer1;
        
    SYSTEM_Initialize();

    Timer->TimeoutCallbackRegister(Tasks_Scheduler);
    Timer->Start();
    
    // Enable the Global Interrupts 
    INTERRUPT_GlobalInterruptEnable(); 
    
    while(1)
    {
    }    
}

4 ms/8 ms Timer Period Change During Each Interrupt Event

This use case configures the Timer module to use the TMR1 Peripheral Library (PLIB) to generate an overflow interrupt. The period of a timer is changed at run time during each interrupt event.

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>Timer:
    • Timer PLIB Selector: TMR1

    • Timer Enable: False

    • Interrupt Driven: True

    • Requested Period: 8 ms

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)
 *     Timer module: Timer PLIB Selector - TMR1, Timer Enable - False, Interrupt Driven - True, Requested Timer Period - 8 ms 
 *     TMR1 module: Timer_PeriodSet() API Range - [125 ns - 8.19187 ms] (read-only)
 *     Pins module: RF3 - GPIO Output, CustomName - LED 
*/

#define TIMER_TICK_FREQ TMR1_CLOCK_FREQ
#define MS_TO_TICKS(ms) (((TIMER_TICK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_4_MS MS_TO_TICKS(4UL)                   // Set the period to be within the PeriodSet API range
#define LED_8_MS MS_TO_TICKS(8UL)

static const struct TIMER_INTERFACE *Timer = &Timer1;
static void Timer_PeriodChange(void)
{ 
    static volatile bool changePeriod = false;
    LED_Toggle();
    Timer->Stop();
    
    uint32_t maxCount = Timer->MaxCountGet();
    uint32_t newPeriod = changePeriod ? LED_4_MS : LED_8_MS;
    
    if(maxCount > newPeriod)
    {     
        Timer->PeriodSet(newPeriod);
    }
    else
    {
        //Iinvalid period       
    }
    
    changePeriod = !changePeriod;
    Timer->Start();
}

int main(void)
{
    SYSTEM_Initialize();
    
    Timer->PeriodSet(LED_4_MS);
    Timer->TimeoutCallbackRegister(Timer_PeriodChange);
    Timer->Start();
    
    // Enable the Global Interrupts 
    INTERRUPT_GlobalInterruptEnable(); 
    
    while(1)
    {
    }    
}

500 ms Timer Period in Non-Interrupt Mode

This use case configures the Timer module to use the TMR1 PLIB to generate a specified timer period in Non-Interrupt mode.

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>Timer:
    • Timer PLIB Selector: TMR1

    • Timer Enable: False

    • Interrupt Driven: False

    • Requested Period: 500 ms

  • Drivers>TMR1:
    • Clock Source: MFINTOSC_500kHz

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)
 *     Timer module: timer PLIB Selector - TMR1, Timer Enable - False, Interrupt Driven - False, Requested Timer Period - 500 ms 
 *     TMR1 module: Clock Source: MFINTOSC_500kHz    
 * Pins module: RF3 - GPIO Output, CustomName - LED 
 */

static void Timer_Callback(void)
{
    LED_Toggle();   
}

int main(void)
{
    SYSTEM_Initialize();
 
    Timer1.TimeoutCallbackRegister(Timer_Callback);
    Timer1.Start();
    
    while(1)
    {
        Timer1.Tasks();
    }    
}

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 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>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 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>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 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>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 
    }    
}

4.24.2.1.8 Data Structure Documentation

TIMER_INTERFACE Struct Reference

Structure containing the function pointers of the Timer driver.

Detailed Description

Structure containing the function pointers of the Timer driver.

Declares an instance of TIMER_INTERFACE for the TMR1 module.

#include <timer_interface.h>

Data Fields
  • void(* Initialize )(void)

    Pointer to MCCPx_Timer_Initialize, SCCPx_Timer_Initialize or TMRx_Initialize (e.g., SCCP1_Timer_Initialize or TMR1_Initialize).

  • void(* Deinitialize )(void)

    Pointer to MCCPx_Timer_Deinitialize, SCCPx_Timer_Deinitialize or TMRx_Deinitialize (e.g., SCCP1_Timer_Deinitialize or TMR1_Deinitialize).

  • void(* Start )(void)

    Pointer to MCCPx_Timer_Start, SCCPx_Timer_Start or TMRx_Start (e.g., SCCP1_Timer_Start or TMR1_Start).

  • void(* Stop )(void)

    Pointer to MCCPx_Timer_Stop, SCCPx_Timer_Stop or TMRx_Stop (e.g., SCCP1_Timer_Stop or TMR1_Stop).

  • void(* PeriodSet )(uint32_t count)

    Pointer to MCCPx_Timer_PeriodSet, SCCPx_Timer_PeriodSet or TMRx_PeriodSet (e.g., SCCP1_Timer_PeriodSet or TMR1_PeriodSet).

  • uint32_t(* PeriodGet )(void)

    Pointer to MCCPx_Timer_PeriodGet, SCCPx_Timer_PeriodGet or TMRx_PeriodGet (e.g., SCCP1_Timer_PeriodGet or TMR1_PeriodGet).

  • uint32_t(* CounterGet )(void)

    Pointer to MCCPx_Timer_CounterGet, SCCPx_Timer_CounterGet or TMRx_CounterGet (e.g., SCCP1_Timer_CounterGet or TMR1_CounterGet).

  • void(* CounterSet )(uint32_t count)

    Pointer to MCCPx_Timer_CounterSet, SCCPx_Timer_CounterSet or TMRx_CounterSet (e.g., SCCP1_Timer_CounterSet or TMR1_CounterSet).

  • uint32_t(* MaxCountGet )(void)

    Pointer to MCCPx_Timer_MaxCountGet, SCCPx_Timer_MaxCountGet or TMRx_MaxCountGet (e.g., SCCP1_Timer_MaxCountGet or TMR1_MaxCountGet).

  • void(* TimeoutCallbackRegister )(void(*CallbackHandler)(void))

    Pointer to MCCPx_TimeoutCallbackRegister, SCCPx_TimeoutCallbackRegister or TMRx_TimeoutCallbackRegister (e.g., SCCP1_TimeoutCallbackRegister or TMR1_TimeoutCallbackRegister).

  • void(* Tasks )(void)

    Pointer to MCCPx_Timer_Tasks, SCCPx_Timer_Tasks or TMRx_Tasks (e.g., SCCP1_Timer_Tasks or TMR1_Tasks , supported only in Polling mode).

Field Documentation

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

source/

timer_interface.h

CounterGet

uint32_t(* CounterGet) (void)

Pointer to MCCPx_Timer_CounterGet, SCCPx_Timer_CounterGet or TMRx_CounterGet (e.g., SCCP1_Timer_CounterGet or TMR1_CounterGet).

CounterSet

void(* CounterSet) (uint32_t count)

Pointer to MCCPx_Timer_CounterSet, SCCPx_Timer_CounterSet or TMRx_CounterSet (e.g., SCCP1_Timer_CounterSet or TMR1_CounterSet).

Deinitialize

void(* Deinitialize) (void)

Pointer to MCCPx_Timer_Deinitialize, SCCPx_Timer_Deinitialize or TMRx_Deinitialize (e.g., SCCP1_Timer_Deinitialize or TMR1_Deinitialize).

Initialize

void(* Initialize) (void)

Pointer to MCCPx_Timer_Initialize, SCCPx_Timer_Initialize or TMRx_Initialize (e.g., SCCP1_Timer_Initialize or TMR1_Initialize).

MaxCountGet

uint32_t(* MaxCountGet) (void)

Pointer to MCCPx_Timer_MaxCountGet, SCCPx_Timer_MaxCountGet or TMRx_MaxCountGet (e.g., SCCP1_Timer_MaxCountGet or TMR1_MaxCountGet).

PeriodGet

uint32_t(* PeriodGet) (void)

Pointer to MCCPx_Timer_PeriodGet, SCCPx_Timer_PeriodGet or TMRx_PeriodGet (e.g., SCCP1_Timer_PeriodGet or TMR1_PeriodGet).

PeriodSet

void(* PeriodSet) (uint32_t count)

Pointer to MCCPx_Timer_PeriodSet, SCCPx_Timer_PeriodSet or TMRx_PeriodSet (e.g., SCCP1_Timer_PeriodSet or TMR1_PeriodSet).

Start

void(* Start) (void)

Pointer to MCCPx_Timer_Start, SCCPx_Timer_Start or TMRx_Start (e.g., SCCP1_Timer_Start or TMR1_Start).

Stop

void(* Stop) (void)

Pointer to MCCPx_Timer_Stop, SCCPx_Timer_Stop or TMRx_Stop (e.g., SCCP1_Timer_Stop or TMR1_Stop).

Tasks

void(* Tasks) (void)

Pointer to MCCPx_Timer_Tasks, SCCPx_Timer_Tasks or TMRx_Tasks (e.g., SCCP1_Timer_Tasks or TMR1_Tasks , supported only in Polling mode).

TimeoutCallbackRegister

void(* TimeoutCallbackRegister) (void(*CallbackHandler)(void))

Pointer to MCCPx_TimeoutCallbackRegister, SCCPx_TimeoutCallbackRegister or TMRx_TimeoutCallbackRegister (e.g., SCCP1_TimeoutCallbackRegister or TMR1_TimeoutCallbackRegister).

4.24.2.1.9 File Documentation

source/timer_interface.h File Reference

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

Data structures

  • struct TIMER_INTERFACE

    Structure containing the function pointers of the Timer driver.

Detailed Description

TIMER Generated Driver Interface Header File

source/tmr1.c File Reference

Driver implementation for the TMR1 driver.

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

Functions

Detailed Description

Driver implementation for the TMR1 driver.

Driver implementation for the TMR1 Timer driver.

TMR1 Generated Driver File

Version: TMR1 Driver Version 4.1.0 Package Version 5.1.0

TMR1 Generated Timer Driver File

Version: TMR1 Timer Driver Version 4.0.0 Package Version 5.1.0

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

Macros

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

Version: TMR1 Timer Driver Version 4.0.0 Package Version 5.1.0

source/tmr1_deprecated.h File Reference

Macros

source/tmr1_drv.c File Reference

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

Functions

Function Documentation

TMR1_DefaultOverflowCallback()

static void TMR1_DefaultOverflowCallback (void )[static]

Variable Documentation

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

Variable Documentation

Timer1

const struct TIMER_INTERFACE Timer1

Section: Included Files

source/tmr1_example.dox File Reference