3.24.1.2 8/16-bit TMR0

8/16-bit Timer0

3.24.1.2.2 Supported Device Families

PIC12/16F184xPIC16F152xxPIC16F153xx
PIC16F171xPIC16F171xxPIC16F180xx
PIC16F181xxPIC16F183xxPIC16F184xx
PIC16F188xxPIC16F191xxPIC18F-K20
PIC18F-K22PIC18F-K40PIC18F-K42
PIC18F-K50PIC18F-K80PIC18F-K83
PIC18F-K90PIC18F-Q10PIC18F-Q20
PIC18F-Q40PIC18F-Q41PIC18F-Q43
PIC18F-Q71PIC18F-Q83PIC18F-Q84

3.24.1.2.3 Required header files:

#include "mcc_generated_files/timer/tmr0.h"

3.24.1.2.4 How to use the Timer 0 PLIB

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

3.24.1.2.5 Specific MISRA C:2012 Deviations

Global TMR0_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.

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

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.1.2.6 Module Documentation

TMR0 in 16-Bit Mode

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

Module description

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

This file contains the deprecated macros or APIs for the TMR0 driver.

Version: TMR0 Driver Version 3.0.0 Package Version 5.0.0
Definitions
Functions

Definition Documentation

TIMER0_CLOCK_FREQ

#define TIMER0_CLOCK_FREQ TMR0_CLOCK_FREQ

Defines the Custom Name for the TMR0_CLOCK_FREQ.

Timer0_CounterGet

#define Timer0_CounterGet TMR0_CounterGet

Defines the Custom Name for the TMR0_CounterGet API.

Timer0_CounterSet

#define Timer0_CounterSet TMR0_CounterSet

Defines the Custom Name for the TMR0_CounterSet API.

Timer0_Deinitialize

#define Timer0_Deinitialize TMR0_Deinitialize

Defines the Custom Name for the TMR0_Deinitialize API.

Timer0_Initialize

#define Timer0_Initialize TMR0_Initialize

Defines the Custom Name for the TMR0_Initialize API.

Timer0_ISR

#define Timer0_ISR TMR0_ISR

Defines the Custom Name for the TMR0_ISR API.

TIMER0_MAX_COUNT

#define TIMER0_MAX_COUNT TMR0_MAX_COUNT

Defines the Custom Name for the TMR0_MAX_COUNT.

Timer0_MaxCountGet

#define Timer0_MaxCountGet TMR0_MaxCountGet

Defines the Custom Name for the TMR0_MaxCountGet API.

Timer0_OverflowCallbackRegister

#define Timer0_OverflowCallbackRegister TMR0_OverflowCallbackRegister

Defines the Custom Name for the TMR0_OverflowCallbackRegister API.

Timer0_OverflowStatusClear

#define Timer0_OverflowStatusClear TMR0_OverflowStatusClear

Defines the Custom Name for the TMR0_OverflowStatusClear API.

Timer0_OverflowStatusGet

#define Timer0_OverflowStatusGet TMR0_OverflowStatusGet

Defines the Custom Name for the TMR0_OverflowStatusGet API.

Timer0_PeriodCountSet

#define Timer0_PeriodCountSet TMR0_PeriodSet

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

Timer0_PeriodGet

#define Timer0_PeriodGet TMR0_PeriodGet

Defines the Custom Name for the TMR0_PeriodGet API.

Timer0_PeriodSet

#define Timer0_PeriodSet TMR0_PeriodSet

Defines the Custom Name for the TMR0_PeriodSet API.

Timer0_Read

#define Timer0_Read TMR0_CounterGet

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

Timer0_Reload

#define Timer0_Reload TMR0_Reload

Defines the Custom Name for the TMR0_Reload API. This macro will be deprecated in the future release.

Timer0_Start

#define Timer0_Start TMR0_Start

Defines the Custom Name for the TMR0_Start API.

Timer0_Stop

#define Timer0_Stop TMR0_Stop

Defines the Custom Name for the TMR0_Stop API.

Timer0_Tasks

#define Timer0_Tasks TMR0_Tasks

Defines the Custom Name for the TMR0_Tasks API.

Timer0_TMRInterruptDisable

#define Timer0_TMRInterruptDisable TMR0_TMRInterruptDisable

Defines the Custom Name for the TMR0_TMRInterruptDisable API.

Timer0_TMRInterruptEnable

#define Timer0_TMRInterruptEnable TMR0_TMRInterruptEnable

Defines the Custom Name for the TMR0_TMRInterruptEnable API.

Timer0_Write

#define Timer0_Write TMR0_CounterSet

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

TMR0_CLOCK_FREQ

#define TMR0_CLOCK_FREQ (32000000UL)

Defines the timer prescaled clock frequency in hertz.

TMR0_INTERRUPT_TICKER_FACTOR

#define TMR0_INTERRUPT_TICKER_FACTOR (5U)

Defines the timer interrupt ticker factor.

TMR0_MAX_COUNT

#define TMR0_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.

TMR0_PeriodCountSet

#define TMR0_PeriodCountSet TMR0_PeriodSet

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

TMR0_Read

#define TMR0_Read TMR0_CounterGet

Defines the Custom Name for the TMR0_CounterGet API. The TMR0_Read will be deprecated in the future release. Use TMR0_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.

TMR0_Write

#define TMR0_Write TMR0_CounterSet

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

Function Documentation

TMR0_CounterGet()

uint16_t TMR0_CounterGet (void )

Returns the current counter value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

Counter value from the TMR0 register

TMR0_CounterSet()

void TMR0_CounterSet (uint16_t counterValue)

Sets the counter value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
counterValue

- Counter value to be written to the TMR0 register

Returns:

None.

TMR0_Deinitialize()

void TMR0_Deinitialize (void )

Deinitializes the TMR0 module.

Parameters:
None.
Returns:

None.

TMR0_Initialize()

void TMR0_Initialize (void )

Initializes the Timer0 (TMR0) module. This routine must be called before any other TMR0 routines.

Parameters:
None.
Returns:

None.

Section: TMR0 APIs

TMR0_ISR()

void TMR0_ISR (void )

Interrupt Service Routine (ISR) for the TMR0 overflow or period match interrupt.

Parameters:
None.
Returns:

None.

TMR0_MaxCountGet()

uint16_t TMR0_MaxCountGet (void )

Returns the maximum count value.

Parameters:
None.
Returns:

Maximum count value

TMR0_OverflowCallbackRegister()

void TMR0_OverflowCallbackRegister (void(*)(void) CallbackHandler)

Registers a callback function for the TMR0 overflow or period match event.

Parameters:
CallbackHandler

- Address to the custom callback function

Returns:

None.

TMR0_OverflowStatusClear()

void TMR0_OverflowStatusClear (void )

Clears the TMR0 Interrupt flag.

Parameters:
None.
Returns:

None.

TMR0_OverflowStatusGet()

bool TMR0_OverflowStatusGet (void )

Returns the status of the TMR0 Interrupt flag.

Parameters:
None.
Returns:

Interrupt flag status

TMR0_PeriodGet()

uint16_t TMR0_PeriodGet (void )

Returns the current period value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

Period count value

TMR0_PeriodSet()

void TMR0_PeriodSet (uint16_t periodCount)

Sets the period count value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
PeriodCount

- Period count value to be written to the TMR0 register

Returns:

None.

TMR0_Reload()

void TMR0_Reload (void )

Reloads the period value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR0_Start()

void TMR0_Start (void )

Starts the TMR0 timer.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR0_Stop()

void TMR0_Stop (void )

Stops the TMR0 timer.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR0_Tasks()

void TMR0_Tasks (void )

Performs tasks to be executed during the timer interrupt event.

Parameters:
None.
Returns:

None.

TMR0_TMRInterruptDisable()

void TMR0_TMRInterruptDisable (void )

Disables the TMR0 interrupt.

Parameters:
None.
Returns:

None.

TMR0_TMRInterruptEnable()

void TMR0_TMRInterruptEnable (void )

Enables the TMR0 interrupt.

Parameters:
None.
Returns:

None.

TMR0 in 8-Bit Mode

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

Module description

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

This file contains the deprecated macros or APIs for the TMR0 driver.

Version: TMR0 Driver Version 3.0.0 Package Version 5.0.0
Definitions
Functions

Definition Documentation

TIMER0_CLOCK_FREQ

#define TIMER0_CLOCK_FREQ TMR0_CLOCK_FREQ

Defines the Custom Name for the TMR0_CLOCK_FREQ.

Timer0_CounterGet

#define Timer0_CounterGet TMR0_CounterGet

Defines the Custom Name for the TMR0_CounterGet API.

Timer0_CounterSet

#define Timer0_CounterSet TMR0_CounterSet

Defines the Custom Name for the TMR0_CounterSet API.

Timer0_Deinitialize

#define Timer0_Deinitialize TMR0_Deinitialize

Defines the Custom Name for the TMR0_Deinitialize API.

Timer0_Initialize

#define Timer0_Initialize TMR0_Initialize

Defines the Custom Name for the TMR0_Initialize API.

Timer0_ISR

#define Timer0_ISR TMR0_ISR

Defines the Custom Name for the TMR0_ISR API.

TIMER0_MAX_COUNT

#define TIMER0_MAX_COUNT TMR0_MAX_COUNT

Defines the Custom Name for the TMR0_MAX_COUNT.

Timer0_MaxCountGet

#define Timer0_MaxCountGet TMR0_MaxCountGet

Defines the Custom Name for the TMR0_MaxCountGet API.

Timer0_OverflowCallbackRegister

#define Timer0_OverflowCallbackRegister TMR0_PeriodMatchCallbackRegister

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

Timer0_PeriodCountSet

#define Timer0_PeriodCountSet TMR0_PeriodSet

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

Timer0_PeriodGet

#define Timer0_PeriodGet TMR0_PeriodGet

Defines the Custom Name for the TMR0_PeriodGet API.

Timer0_PeriodMatchCallbackRegister

#define Timer0_PeriodMatchCallbackRegister TMR0_PeriodMatchCallbackRegister

Defines the Custom Name for the TMR0_PeriodMatchCallbackRegister API.

Timer0_PeriodMatchStatusClear

#define Timer0_PeriodMatchStatusClear TMR0_PeriodMatchStatusClear

Defines the Custom Name for the TMR0_PeriodMatchStatusClear API.

Timer0_PeriodMatchStatusGet

#define Timer0_PeriodMatchStatusGet TMR0_PeriodMatchStatusGet

Defines the Custom Name for the TMR0_PeriodMatchStatusGet API.

Timer0_PeriodSet

#define Timer0_PeriodSet TMR0_PeriodSet

Defines the Custom Name for the TMR0_PeriodSet API.

Timer0_Read

#define Timer0_Read TMR0_CounterGet

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

Timer0_Reload

#define Timer0_Reload TMR0_PeriodSet

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

Timer0_Start

#define Timer0_Start TMR0_Start

Defines the Custom Name for the TMR0_Start API.

Timer0_Stop

#define Timer0_Stop TMR0_Stop

Defines the Custom Name for the TMR0_Stop API.

Timer0_Tasks

#define Timer0_Tasks TMR0_Tasks

Defines the Custom Name for the TMR0_Tasks API.

Timer0_TMRInterruptDisable

#define Timer0_TMRInterruptDisable TMR0_TMRInterruptDisable

Defines the Custom Name for the TMR0_TMRInterruptDisable API.

Timer0_TMRInterruptEnable

#define Timer0_TMRInterruptEnable TMR0_TMRInterruptEnable

Defines the Custom Name for the TMR0_TMRInterruptEnable API.

Timer0_Write

#define Timer0_Write TMR0_CounterSet

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

TMR0_CLOCK_FREQ

#define TMR0_CLOCK_FREQ (32000000UL)

Defines the timer prescaled clock frequency in hertz.

TMR0_INTERRUPT_TICKER_FACTOR

#define TMR0_INTERRUPT_TICKER_FACTOR (5U)

Defines the timer interrupt ticker factor.

TMR0_MAX_COUNT

#define TMR0_MAX_COUNT (255U)

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.

TMR0_OverflowCallbackRegister

#define TMR0_OverflowCallbackRegister TMR0_PeriodMatchCallbackRegister

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

TMR0_PeriodCountSet

#define TMR0_PeriodCountSet TMR0_PeriodSet

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

TMR0_Read

#define TMR0_Read TMR0_CounterGet

Defines the Custom Name for the TMR0_CounterGet API. The TMR0_Read will be deprecated in the future release. Use TMR0_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.

TMR0_Reload

#define TMR0_Reload TMR0_PeriodSet

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

TMR0_Write

#define TMR0_Write TMR0_CounterSet

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

Function Documentation

TMR0_CounterGet()

uint8_t TMR0_CounterGet (void )

Returns the current counter value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

Counter value from the TMR0L register

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

Counter value from the TMR0 register

TMR0_CounterSet()

void TMR0_CounterSet (uint8_t counterValue)

Sets the counter value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
counterValue

- Counter value to be written to the TMR0L register

Returns:

None.

TMR0_Deinitialize()

void TMR0_Deinitialize (void )

Deinitializes the TMR0 module.

Parameters:
None.
Returns:

None.

TMR0_Initialize()

void TMR0_Initialize (void )

Initializes the Timer0 (TMR0) module. This routine must be called before any other TMR0 routines.

Parameters:
None.
Returns:

None.

Section: TMR0 APIs

TMR0_ISR()

void TMR0_ISR (void )

Interrupt Service Routine (ISR) for the TMR0 overflow or period match interrupt.

Parameters:
None.
Returns:

None.

TMR0_MaxCountGet()

uint8_t TMR0_MaxCountGet (void )

Returns the maximum count value.

Parameters:
None.
Returns:

Maximum count value

TMR0_PeriodGet()

uint8_t TMR0_PeriodGet (void )

Returns the current period value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

Period count value from the TMR0H register

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

Period count value

TMR0_PeriodMatchCallbackRegister()

void TMR0_PeriodMatchCallbackRegister (void(*)(void) CallbackHandler)

Registers a callback function for the TMR0 overflow or period match event.

Parameters:
CallbackHandler

- Address to the custom callback function

Returns:

None.

TMR0_PeriodMatchStatusClear()

void TMR0_PeriodMatchStatusClear (void )

Clears the TMR0 Interrupt flag.

Parameters:
None.
Returns:

None.

TMR0_PeriodMatchStatusGet()

bool TMR0_PeriodMatchStatusGet (void )

Returns the status of the TMR0 Interrupt flag.

Parameters:
None.
Returns:

Interrupt flag status

TMR0_PeriodSet()

void TMR0_PeriodSet (uint8_t periodCount)

Sets the period value.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
periodCount

- Period count value written to the TMR0H register

Returns:

None.

TMR0_Start()

void TMR0_Start (void )

Starts the TMR0 timer.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR0_Stop()

void TMR0_Stop (void )

Stops the TMR0 timer.

Precondition:

Initialize TMR0 with TMR0_Initialize() before calling this API.

Parameters:
None.
Returns:

None.

TMR0_Tasks()

void TMR0_Tasks (void )

Performs tasks to be executed during the timer interrupt event.

Parameters:
None.
Returns:

None.

TMR0_TMRInterruptDisable()

void TMR0_TMRInterruptDisable (void )

Disables the TMR0 interrupt.

Parameters:
None.
Returns:

None.

TMR0_TMRInterruptEnable()

void TMR0_TMRInterruptEnable (void )

Enables the TMR0 interrupt.

Parameters:
None.
Returns:

None.

3.24.1.2.7 TMR0 Examples

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

TMR0 PLIB Examples

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

This use case configures the TMR0 PLIB in 16-bit mode 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>TMR0:
    • Timer Enable: False

    • Timer Mode: 16-bit

    • Prescaler: 1:64

    • 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:
*     Clock Control: HFINTOSC (64 MHz)
*     TMR0 module: Timer Enable - False, Timer Mode - 16-bit, Prescaler - 1:64,  TMR Interrupt Enable - False
*     Pins module: RF3 - GPIO Output, CustomName - LED 
*/
 
#define  USE_TASKS_API 1

#define MS_TO_TICKS(ms) (((TMR0_CLOCK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_4_MS  (TMR0_MAX_COUNT - MS_TO_TICKS(4UL))
#define LED_8_MS (TMR0_MAX_COUNT - MS_TO_TICKS(8UL))

static void Timer_PeriodChange(void)
{ 
    static volatile bool changePeriod = false;
    LED_Toggle();
    TMR0_Stop();
    
    uint16_t maxCount = TMR0_MaxCountGet();
    uint16_t newPeriod = changePeriod ? LED_4_MS:LED_8_MS;
    
    if(maxCount > newPeriod)
    {   
        TMR0_PeriodSet(newPeriod);
        TMR0_CounterSet(newPeriod);
    }
    else
    {
        // Invalid period
    }
    changePeriod = !changePeriod;
    TMR0_Start();
}

int main(void)
{
    SYSTEM_Initialize();
    
    TMR0_PeriodSet(LED_4_MS);
    TMR0_CounterSet(LED_4_MS);
    TMR0_OverflowStatusClear();
    
#if USE_TASKS_API    
    TMR0_OverflowCallbackRegister(Timer_PeriodChange);
#endif
    TMR0_Start();
    
    while(1)
    {
#if   USE_TASKS_API
        TMR0_Tasks();
#else
        if(1U == TMR0_OverflowStatusGet())
        {
            Timer_PeriodChange();
            TMR0_OverflowStatusClear();
        }
#endif
    }    
}

4 ms Timer Period in 8-bit Timer Interrupt Mode

This use case configures the TMR0 PLIB in 8-bit mode to generate the period match 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>TMR0:
    • Timer Enable: False

    • Timer Mode: 8-bit

    • Prescaler: 1:128

    • Postscaler: 1:16

    • 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:
*     Clock Control: HFINTOSC (64 MHz)
*     TMR0 module: Timer Enable - False, Timer Mode - 8-bit, Prescaler - 1:128,  Postscaler - 1:16, TMR Interrupt Enable - True
*     Pins module: RF3 - GPIO Output, CustomName - LED 
*/
 
#define POSTSCALER 16UL
#define MS_TO_TICKS(ms) (((TMR0_CLOCK_FREQ * (ms)) / 1000UL) - 1UL)
#define LED_4_MS  (MS_TO_TICKS(4UL)/POSTSCALER)

void Custom_Callback(void)
{
    LED_Toggle();
}

int main(void)
{
    SYSTEM_Initialize();
    
    myTimer_CounterSet(0);
    if(myTimer_MaxCountGet() > LED_4_MS)
    {
        myTimer_PeriodSet(LED_4_MS);
    }
    else 
    {
        //Invalid period
        //Generate default timer period
        
    }
    myTimer_PeriodMatchCallbackRegister(Custom_Callback);
    myTimer_Start();
    
    INTERRUPT_GlobalInterruptEnable();
    
    while(1)
    {
    }    
}

Timer Wake From Sleep at Every 5s

This use case configures the TMR0 PLIB in 8-bit mode 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>TMR0:
    • Timer Enable: True

    • Timer Mode: 8-bit

    • Clock Source: LFINTOSC

    • Synchronization Enable: False

    • Prescaler: 1:64

    • Postscaler: 1:10

    • 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)
 *     TMR0 module: Timer Enable - True, Timer Mode - 8-bit, Clock Source - LFINTOSC, Synchronization Enable - False, Prescaler - 1:64,  Postscaler - 1:10, 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.1.2.8 File Documentation

source/tmr0_16bit.c File Reference

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

Functions

Function Documentation

TMR0_DefaultCallback()

static void TMR0_DefaultCallback (void )[static]

Variable Documentation

TMR0_OverflowCallback

void(* TMR0_OverflowCallback) (void)[static]

tmr0PeriodCount

volatile uint16_t tmr0PeriodCount[static]

source/tmr0_16bit.h File Reference

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

Functions

Macros

source/tmr0_8bit.c File Reference

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

Functions

Function Documentation

TMR0_DefaultCallback()

static void TMR0_DefaultCallback (void )[static]

Variable Documentation

TMR0_PeriodMatchCallback

void(* TMR0_PeriodMatchCallback) (void)[static]

source/tmr0_8bit.h File Reference

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

Functions

Macros

source/tmr0_deprecated_16bit.h File Reference

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

Macros

source/tmr0_deprecated_8bit.h File Reference

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

Macros

source/tmr0_drv.c File Reference

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

Functions

Function Documentation

TMR0_DefaultOverflowCallback()

static void TMR0_DefaultOverflowCallback (void )[static]

Variable Documentation

TMR0_OverflowCallback

void(* TMR0_OverflowCallback) (void)[static]

tmr0PeriodCount

volatile uint16_t tmr0PeriodCount[static]

source/tmr0_drv.h File Reference

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

Functions

  • void TMR0_Initialize (void)

    Initializes the Timer0 (TMR0) module. This routine must be called before any other TMR0 routines.

  • void TMR0_Deinitialize (void)

    Deinitializes the TMR0 module.

  • void TMR0_Start (void)

    Starts the TMR0 timer.

  • void TMR0_Stop (void)

    Stops the TMR0 timer.

  • uint32_t TMR0_CounterGet (void)

    Returns the current counter value.

  • void TMR0_CounterSet (uint32_t counterValue)

    Sets the counter value.

  • void TMR0_PeriodSet (uint32_t periodCount)

    Sets the period count value.

  • uint32_t TMR0_PeriodGet (void)

    Returns the current period value.

  • uint32_t TMR0_MaxCountGet (void)

    Returns the maximum count value.

  • void TMR0_ISR (void)

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

  • void TMR0_OverflowCallbackRegister (void(*CallbackHandler)(void))

    Registers a callback function for the TMR0 overflow event.

  • void TMR0_Tasks (void)

    Performs tasks to be executed during the TMR0 overflow event.

Macros

Variable Documentation

Timer0

const struct TIMER_INTERFACE Timer0

source/tmr0_example.dox File Reference