Introduction

Authors: Alin Stoicescu, Marius Nicolae, Stefan Vlad, Microchip Technology Inc.

This technical brief provides information about the Timers/Counters present on the PIC18 families of microcontrollers.

The document describes the application area, the modes of operation and the hardware and software requirements of the Timers/Counters and configurable output or input for internal or external use with the help of the Peripheral Pin Select (PPS).

Throughout the document, the configuration of the used peripherals for each use case will be described in detail. Additionally, this technical brief explains the concepts of the TMR0, TMR1/3/5 and TMR2/4/6 and their implementation in the PIC18 family of microcontrollers with the following use cases:

  1. 1.TIMER 0
    1. a.Using TMR0 in 8-bit Mode with Periodic Interrupt:

      This example describes how to configure TMR0 in 8-bit mode and generate a compare interrupt every 100 ms, using LFINTOSC as clock source. A GPIO pin is toggled each time an interrupt occurs.

    2. b.Using and operating TMR0 in 16-bit Mode while the Microcontroller is in Sleep:

      This example describes how to configure and operate TMR0 in 16-bit mode while the microcontroller is in Sleep mode and generate an overflow interrupt every ten seconds. When the interrupt occurs, a GPIO pin connected to an LED is ON for 100 ms and then the microcontroller is put back to Sleep.

    3. c.Using TMR0 in 8-bit Mode and to Generate an Output Signal:

      This example describes how to configure TMR0 in 8-bit mode and generate a 125 Hz signal on one of the T0 output pins using Peripheral Pin Select (PPS).

  2. 2.TIMER 1/3/5
    1. a.Using TMR1 Gate to Measure Frequency:

      This example shows how to use the TMR1 configured in Gate Single-Pulse and Toggle Combined mode. It will sample a full period of a signal. A GPIO pin will be configured as input and it will be connected to a periodical signal.

    2. b.Using TMR1 to Trigger a Special Event:

      This example shows how to use the TMR1 configured as a counter. The Capture/Compare/PWM (CCP) module will be configured with a user-defined value. A GPIO pin will be configured as an output for the CCP. When the counter reaches the CCP value, the pin logic value will be toggled.

    3. c.Using TMR1 Gate to Measure Short vs Long Button Press:

      This example shows how to use the TMR1 configured in Gate Single-Pulse mode. It will start counting when the button is pressed. Two different interrupts will be activated based on how long the button was pressed.

  3. 3.TIMER 2/4/6
    1. a.Using TMR2 as Auto-conversion Trigger for ADCC Module

      This example will present how to use TMR2 peripheral to trigger the ADCC to make conversions at a fixed frequency rate that can be adjusted by modifying the period of TMR2.

    2. b.Using TMR4 in One-Shot Mode with External Signal as Reset

      This example will present how to use TMR4 peripheral in One-Shot mode to stop TMR2 if an external pin is pulled to GND for more than the desired period.

    3. c.Using TMR4 as HLT to Generate an Interrupt (like a WDT without Reset)

      This example will present how to use the TMR4 as a Hardware Limit Timer (HLT) in order to generate an interrupt and stop TMR2 that also stops the ADCC auto-conversion.

    4. d.Using TMR2 as Alternate SPI Clock

      This example will present how to use the TMR2 as alternate clock for SPI peripheral with a 10 kHz frequency.

Note: For each use case, there are two different implementations that have the same functionality: one bare metal code example and one MPLAB® Code Configurator (MCC) generated code example.