Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CAPTURA/COMPARACIN/PWM (CCP)
VISIN GENERAL
Captura
Comparacin
Cambia el estado de un pin de salida o genera una interrrupcin cuando una cantidad especifica de tiempo ha pasado.
CCP1M3
CCP1M2
CCP1M1
CCP1M0
FUNCTION
Bits de seleccin de Modo del CCP configuran el modulo como una entrada de captura, salida de comparacin o PWM
CCPxM1
0 0 1 1 0 0
CCPxM2
0 0 0 0 1 1
CCPxM0
0 1 0 1 0 1
0
0 1 1 1 1 1
1
1 0 0 0 0 1
1
1 0 0 1 1 x
0
1 0 1 0 1 x
2013 Mk-Pro Mekatronika
CCP1M3
CCP1M2
CCP1M1
CCP1M0
FUNCTION
CCP Mode Select Bits configure the module as Input Capture, Output Compare, or PWM PWM duty cycle 2 LSBs (8 MSBs located in CCPR1L)
CCP1M3
CCP1M2
CCP1M1
CCP1M0
FUNCTION
CCP Mode Select Bits configure the module as Input Capture, Output Compare, or PWM PWM duty cycle 2 LSBs (8 MSBs located in CCPR1L) These PWM output configuration bits are available for Enhanced CCP (ECCP) modules only. They provide half-bridge or full-bridge output steering control.
CAPTURE MODE
Prescaler 1, 4, 16
CCPx TMR1H Edge Detect and TMR1L
Single Buffered
Capture every rising or falling edge, or every 4th or 16th rising edge.
Programacin de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika
CCPRxH
CCPRxL
Store capture time here
Pin ;Turn off CCP module CCP1CON = 0x00; 0 1 ;Make sure Timer1 is off rd nd st 4 3 TMR1ON = 0; 2 1th ;Clear Timer1 registers Rising Edge TMR1H = 0x00; Detected!! TMR1L = 0x00; ;Disable all interrupts for CCP CCP1IF = 0; CCP1IE = 0; ;Set CCP1 pin for input TRISC2 = 1; ;Set Capture for every 4th rising edge CCP1CON = b00000110; ;Start Timer1 incrementing TMR1ON = 1; ;Test the interrupt flag for capture while (!CCP1IF);
0 0
0 0
TMR1L
CCPR1H CCPR1L
Captured!
PIR1
0 1
CCP1IF CCP1CON
0 1
1 0
T1CON
1 0
TMR1ON
Programacin de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 7
COMPARE MODE
TMR1H TMR1L
Set the CCP Interrupt flag
COMPARATOR
Does TMR1H:TMR1L = CCPRxH:CCPRxL
??
CCPRxH
CCPRxL
Set or clear the CCP pin or clear Timer1 and start an ADC conversion
Programacin de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 8
COMPARE INITIALIZATION C
//Turn off the CCP module CCP1CON = 0x00; //Turn off Timer1 TMR1ON = 0; //Clear Timer1 result registers TMR1H = 0x00; TMR1L = 0x00; //Disable CCP1 interrupt and make sure //its flag is clear CCP1IE = 0; CCP1IF = 0; //Make CCP1 pin output TRISC2 = 0;
TMR1H
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
TMR1L
0 1
1 0
0 1
0 0
0 1
0 0
0 1
0 0
0 1
0 0
0 1
0 0
0 1
0 0
1 0
0 0 0 1
CCPR1H
CCPR1L
T1CON
CCP1CON
TMR1ON
0
PIR1
1 0
0
0 1
CCP1IF
PWM MODE Generates a Pulse-Width Modulated (PWM) signal on the CCP1 and CCP2 pins
Duty cycle, period and resolution determined by the following registers
Register
PR2
Description
Period Register
T2CON
CCPRxL CCPxCON
Timer2 Control
Duty Cycle Register CCP Control Register
10
PWM OPERATION
CCPR1L
DC1B1:DC1B0 bits in CCP1CON
0 10 0
CCPR1H
0
Period Start
10
COMPARATOR
TMR2
10 0 0 0 8 COMPARATOR 8 0 0 1 1 0 1 0 1 0
R
Latch
1 CCP1 pin
TMR2 is concatenated with 2 bits from FOSC, or 2 bits from Prescaler to create a 10 bit time base
11
PR2
PWM OPERATION
CCPR1L Determines Duty Cycle
DC1B1:DC1B0 bits in CCP1CON
CCPR1H
Double Buffer
0 10
0
1 0
1 0
1 0
Period Start
10
COMPARATOR
TMR2
10 0 0 0 8 COMPARATOR 8 0 0 1 0 1 1 0 1 0
R
Latch
1 0
CCP1 pin
PR2
1
2013 Mk-Pro Mekatronika 12
PWM OPERATION
CCPR1L
DC1B1:DC1B0 bits in CCP1CON
Period 1
0 10
0
CCPR1H
1
Period Start
10
COMPARATOR
TMR2
10 0 0 0 8 COMPARATOR 8 0 1 0 1 0 1 0 1 0
R
Latch
1 0
CCP1 pin
PR2
1
2013 Mk-Pro Mekatronika 13
PWM OPERATION
CCPR1L Determines Duty Cycle
DC1B1:DC1B0 bits in CCP1CON
Period 1
Period 2
CCPR1H
Double Buffer
0 10
0
1
Period Start
10
COMPARATOR
TMR2
10 0 0 0 8 COMPARATOR 8 0 1 0 0 1
0 1
0 1
Latch
0 1 CCP1 pin
PR2
TMR2 Reset to 0s
1
2013 Mk-Pro Mekatronika 14
PWM INITIALIZATION C
TMR2
0
//Turn off CCP1 pin by making it an input TRISC2 = 1; //Turn off Timer2 TMR2ON = 0; //Clear Timer2 TMR2 = 0x00; //Set up Period and Duty Cycle PR2 = 0b01111111; //Load a Period Value CCPR1L = 0b00011111; //Load a Duty Cycle Value //Configure CCP module for PWM // and LSBs of Duty Cycle = b10 CCP1CON = 0b00101100; //Turn CCP1 pin back on (make it an output) TRISC2 = 0; //Start the PWM by turning on Timer2 //Configure Prescaler and Postscaler to 1:1) T2CON = 0b00000100;
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
0 1 1 1
0 1 1 0
0 1 1 0
PR2
0 0 0
CCPR1L
CCP1CON
T2CON
0 1
15