Using TMR1 to Trigger a Special Event

This example describes how to initialize and use the TMR1 as a counter. The Capture/Compare/PWM (CCP) module will be configured with a user-defined value. A GPIO pin will be configured as output and the event will toggle the logic value of this pin. The event will be triggered when the counted value from TMR1 will be equal with the CCP value.

In this example, the microcontroller was configured with a clock system of 1 MHz and the timer was configured with a clock source frequency of 250 kHz. It is a 16-bit timer so it can count up to 65,535. The CCP value was set to 4,095 in this example. When the counter reaches this value, an event will occur which will be strictly handled by the hardware peripheral, without any software and load on the core.

The event can be configured to clear or not clear the timer counter value and, if the GPIO pin should be set high, set low or toggled every time the event is triggered. In this example, the event will toggle the GPIO pin and will not clear the timer, so the timer counted value will overflow when reaches the 65,535 maxim value and will restart counting from zero. Thus, even when a value was predefined for CCP, the event will be triggered with a frequency of 250 kHz / 65,535 ~ = 3.81 Hz.

To achieve the functionality described by this use case, the following actions will have to be performed: