Asynchronous Counter Mode
REGISTER 14-1: T2CON: TIMER2 CONTROL REGISTER
15.1 Capture Mode
The Capture mode function described in this section is identical for all CCP and ECCP modules available on this device family.
Capture mode makes use of the 16-bit Timer resources, Timer1 and Timer3. The timer resources for each CCP capture function are independent and are selected using the CCPTMRS register. When an event occurs on the CCPx pin, the 16-bit CCPRxH:CCPRxL register pair captures and stores the 16-bit value of the TMRxH:TMRxL register pair, respectively. An event is defined as one of the following and is configured by the CCPxM<3:0> bits of the CCPxCON register:
• Every falling edge
• Every rising edge
• Every 4th rising edge
• Every 16th rising edge
When a capture is made, the corresponding Interrupt Request Flag bit CCPxIF of the PIR1 and PIR2 register is set. The interrupt flag must be cleared in software. If another capture occurs before the value in the CCPRxH:CCPRxL register pair is read, the old captured value is overwritten by the new captured value.
Figure 15-1 shows a simplified diagram of the Capture operation.
FIGURE 15-1: CAPTURE MODE OPERATION BLOCK DIAGRAM
Note 1: In devices with more than one CCP module, it is very important to pay close attention to the register names used. A number placed after the module acronym is used to distinguish between separate modules. For example, the CCP1CON and CCP2CON control the same operational aspects of two completely different CCP modules.
2: Throughout this section, generic references to a CCP module in any of its operating modes may be interpreted as being equally applicable to ECCP1 and CCP2. Register names, module signals, I/O pins and bit names may use the generic designator ‘x’ to indicate the use of a numeral to distinguish a particular module, when required.
CCPRxH CCPRxL
TMRxH TMRxL
Set Flag bit CCPxIF (PIRx register)
Capture Enable
CCPxM<3:0>
Prescaler
1, 4, 16
and Edge Detect pin
CCPx
System Clock (FOSC)
PIC18(L)F2X/45K50
15.1.1 CCP PIN CONFIGURATION
In Capture mode, the CCPx pin should be configured as an input by setting the associated TRIS control bit.
Some CCPx outputs are multiplexed on a couple of pins. Table 15-1 shows the CCP output pin multiplexing. Selection of the output pin is determined by the CCPxMX bits in Configuration register 3H (CONFIG3H). Refer to Register 26-5 for more details.
15.1.2 TIMER1 MODE RESOURCE
The 16-bit Timer resource must be running in Timer mode or Synchronized Counter mode for the CCP module to use the capture feature. In Asynchronous Counter mode, the capture operation may not work.
See Section 13.0 “Timer1/3 Module with Gate Control” for more information on configuring the 16-bit Timers.
15.1.3 SOFTWARE INTERRUPT MODE When the Capture mode is changed, a false capture interrupt may be generated. The user should keep the CCPxIE interrupt enable bit of the PIEx register clear to avoid false interrupts. Additionally, the user should clear the CCPxIF interrupt flag bit of the PIRx register following any change in Operating mode.
Note: If the CCPx pin is configured as an output, a write to the port can cause a capture condition.
TABLE 15-1: CCP PIN MULTIPLEXING
CCP OUTPUT CONFIG 3H Control Bit Bit Value I/O pin
CCP2 CCP2MX 0 RB3
1(*) RC1
Legend: * = Default
Note: Clocking the 16-bit Timer resource from the system clock (FOSC) should not be used in Capture mode. In order for Capture mode to recognize the trigger event on the CCPx pin, the Timer resource must be clocked from the instruction clock (FOSC/4) or from an external clock source.
PIC18(L)F2X/45K50
15.1.4 CCP PRESCALER
There are four prescaler settings specified by the CCPxM<3:0> bits of the CCPxCON register. Whenever the CCP module is turned off, or the CCP module is not in Capture mode, the prescaler counter is cleared. Any Reset will clear the prescaler counter.
Switching from one capture prescaler to another does not clear the prescaler and may generate a false interrupt. To avoid this unexpected operation, turn the module off by clearing the CCPxCON register before changing the prescaler. Example 15-1 demonstrates the code to perform this function.
EXAMPLE 15-1: CHANGING BETWEEN CAPTURE PRESCALERS
15.1.5 CAPTURE DURING SLEEP
Capture mode requires a 16-bit TimerX module for use as a time base. There are four options for driving the 16-bit TimerX module in Capture mode. It can be driven by the system clock (FOSC), the instruction clock (FOSC/ 4), or by the external clock sources, the Secondary Oscillator (SOSC), or the TxCKI clock input. When the 16-bit TimerX resource is clocked by FOSC or FOSC/4, TimerX will not increment during Sleep. When the device wakes from Sleep, TimerX will continue from its previous state. Capture mode will operate during Sleep when the 16-bit TimerX resource is clocked by one of the external clock sources (SOSC or the TxCKI pin).
#define NEW_CAPT_PS 0x06 //Capture // Prescale 4th
... // rising edge
CCPxCON = 0; // Turn the CCP // Module Off CCPxCON = NEW_CAPT_PS; // Turn CCP module
// on with new // prescale value
PIC18(L)F2X/45K50
TABLE 15-2: REGISTERS ASSOCIATED WITH CAPTURE
Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Register
on page
CCP1CON P1M<1:0> DC1B<1:0> CCP1M<3:0> 197
CCP2CON — — DC2B<1:0> CCP2M<3:0> 197
CCPR1H Capture/Compare/PWM Register 1, High Byte (MSB) —
CCPR1L Capture/Compare/PWM Register 1, Low Byte (LSB) —
CCPR2H Capture/Compare/PWM Register 2, High Byte (MSB) —
CCPR2L Capture/Compare/PWM Register 2, Low Byte (LSB) —
CCPTMRS — — — — C2TSEL — — C1TSEL 200
INTCON GIE/GIEH PEIE/GIEL TMR0IE INT0IE IOCIE TMR0IF INT0IF IOCIF 114
IPR1 ACTIP ADIP RCIP TXIP SSPIP CCP1IP TMR2IP TMR1IP 123
IPR2 OSCFIP C1IP C2IP EEIP BCLIP HLVDIP TMR3IP CCP2IP 124
PIE1 ACTIE ADIE RCIE TXIE SSPIE CCP1IE TMR2IE TMR1IE 120
PIE2 OSCFIE C1IE C2IE EEIE BCLIE HLVDIE TMR3IE CCP2IE 121
PIR1 ACTIF ADIF RCIF TXIF SSPIF CCP1IF TMR2IF TMR1IF 117
PIR2 OSCFIF C1IF C2IF EEIF BCLIF HLVDIF TMR3IF CCP2IF 118
PMD0 — UARTMD USBMD ACTMD — TMR3MD TMR2MD TMR1MD 61
PMD1 — MSSPMD CTMUMD CMP2MD CMP1MD ADCMD CCP2MD CCP1MD 62
T1CON TMR1CS<1:0> T1CKPS<1:0> SOSCEN T1SYNC RD16 TMR1ON 165
T1GCON TMR1GE T1GPOL T1GTM T1GSPM T1GGO/DONE T1GVAL T1GSS<1:0> 166
T3CON TMR3CS<1:0> T3CKPS<1:0> SOSCEN T3SYNC RD16 TMR3ON 165
T3GCON TMR3GE T3GPOL T3GTM T3GSPM T3GGO/DONE T3GVAL T3GSS<1:0> 166
TMR1H Holding Register for the Most Significant Byte of the 16-bit TMR1 Register — TMR1L Holding Register for the Least Significant Byte of the 16-bit TMR1 Register — TMR3H Holding Register for the Most Significant Byte of the 16-bit TMR3 Register — TMR3L Holding Register for the Least Significant Byte of the 16-bit TMR3 Register —
TRISB TRISB7 TRISB6 TRISB5 TRISB4 TRISB3 TRISB2 TRISB1 TRISB0 149
TRISC TRISC7 TRISC6 — — — TRISC2 TRISC1 TRISC0 149
Legend: — = Unimplemented location, read as ‘0’. Shaded bits are not used by Capture mode.
TABLE 15-3: CONFIGURATION REGISTERS ASSOCIATED WITH CAPTURE
Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Register
on page
CONFIG3H MCLRE SDOMX — T3CMX — — PBADEN CCP2MX 376
Legend: — = Unimplemented location, read as ‘0’. Shaded bits are not used by Capture mode.