8-bit CCP/ECCP Capture Mode
Capture mode operates the same way for a CCP or ECCP peripheral. There can be multiple CCP peripherals in one device and each can be independently configured for Capture mode. Each will also have a designated CCP I/O pin associated with the CCP module.
Capture mode makes use of the 16-bit Timer1. When a specified event occurs on the designated CCP pin, the 16-bit Timer1 value is stored in a pair of 8-bit registers dedicated to the Compare module. Those registers are the Compare Register High Byte and Compare Register Low Byte (CPRxH:CCPRxL).
A specified Compare event is defined as one of the options below and is configured by the four CCP Mode Select Bits CCPxM<3:0> of the Capture/Compare/PWM Control Register (CCPxCON).
- Every falling edge
- Every rising edge
- Every 4th rising edge
- Every 16th rising edge
When a capture is made, the CCP Interrupt Flag bit CCPxIF of the Peripheral Interrupt Register PIRx is set. The software can automatically react, when interrupts are enabled, by running an interrupt service routine that will read the CPRxH:CCPRxL registers. The flag can also be monitored in software and operate on the CPRxH:CCPRxL captured time value when the flag bit is set.
In either case, the interrupt flag must be cleared in software. If another capture occurs before the value in the CPRxH:CCPRxL register pair is read, the old captured value is overwritten by the new captured value.
For more information on interrupts, refer to the "8-bit PIC MCU Interrupts" page.
Timer1 (Capture Mode)
In order for CCP/ECCP Capture mode to operate properly, Timer1 must be running in either Timer mode or Synchronized Counter mode. This allows the Capture mode to correctly capture the Timer1 value on the external CCPx pin event.
Reference the Timer1 module for more information on configuring Timer1.
CCPx Pin Configuration (Capture Mode)
The software design must configure the CCPx pin as an input for the Capture mode by setting the associated TRIS bit. Also, on some newer devices, the CCPx pin function can be moved to alternative pins using the APFCON0 or APFCON1 registers or through the Peripheral Pin Select feature.
Software Interrupt (Capture Mode)
When a capture is made in the Capture mode, the CCP Interrupt Flag bit (CCPxIF) of the Peripheral Interrupt (PIRx) register is set. The interrupt flag can be monitored via software or an automatic interrupt can be initiated, sending the program control to an interrupt service routine. The interrupt flag must be cleared in software. For more detail on the interrupt structure of 8-bit devices, refer to the interrupts training module.
Prescaler (Capture Mode)
The prescaler can delay reaction to an event on the CCPx pin. The capture of the Timer1 value can be delayed to capture on the first edge (falling or rising), the fourth edge (rising only), or the 16th edge (rising only). These three prescaler settings are determined by the CCPxM<3:0> bits of the CCPxCON register.
- Prescaler divide by 1
- Capture mode: every falling edge (CCPxM<3:0> = 0100)
- Capture mode: every rising edge (CCPxM<3:0> = 0101)
- Prescaler divide by 4
- Capture mode: every fourth rising edge (CCPxM<3:0> = 0110)
- Prescaler divide by 16
- Capture mode: every 16th rising edge (CCPxM<3:0> = 0111
Whenever the CCP module is turned off, or the CCP module is not in Capture mode, the prescaler counter is cleared. Any Reset will also clear the prescaler counter.
Sleep (Capture Mode)
Capture mode depends upon the Timer1 module for proper operation. Two of the Timer1 clock source options will work with the Capture mode: the FOSC/4 instruction clock and the external clock option. Each clock source has a different effect on Capture mode during sleep.
If Timer1 is driven by the instruction clock (FOSC/4), Timer1 will not increment during sleep. This means that the Capture mode will not work during sleep. When the device wakes from sleep, Timer1 will continue from its previous state.
If Timer1 is driven by an external clock, then Timer1 will continue to increment in Sleep mode and Capture mode will also operate during sleep.