Sei sulla pagina 1di 17

MODULO CAPTURA/COMPARACIN/PWM

CAPTURA/COMPARACIN/PWM (CCP)
VISIN GENERAL

Captura

Mide la duracin de un evento externo usando como entra el pin

Comparacin
Cambia el estado de un pin de salida o genera una interrrupcin cuando una cantidad especifica de tiempo ha pasado.

Modulacin de ancho de pulso (PWM)


Crea una salida de onda cuadrada con una frecuencia y ciclo de trabajo definido
CCP MODE Capture Compare PWM
Programacin de Microcontroladores usando XC8.

Recurso usado Timer 1 Timer 1 Timer 2


2013 Mk-Pro Mekatronika 2

REGISTRO DE CONTROL DEL ECCP


CCP1 Control Register (CCP1CON) P1M1 P1M0 DC1B1 DC1B0 BIT
CCP1M<3:0>
CCPxM3
0 0 0 0 0 0

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

CCP Mode Selected


Capture/Compare/PWM Apagado (resetea el modulo CCP) Sin usar (reservado) modo comparador, togglea la salida al coincidir Sin usar (reservado) Modo de captura, cada flanco de bajada Modo captura cada flanco de subida

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

Modo captura, cada 4 flancos de subida


Modo captura , cada 16 flancos de subida Modo comparacin setea la salida al coicidir Modo comparador, limpia la salida al coicidirmatch Modo comprador, genera una interrupcin en software al coicidir Modo comparador, dispara un evento especial Modo PWM 3

Programacin de Microcontroladores usando XC8.

REGISTRO DE CONTROL DEL CCP


CCP1 Control Register (CCP1CON) P1M1 P1M0 DC1B1 DC1B0 BIT
CCP1M<3:0> DC1B<1:0>

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)

Programacin de Microcontroladores usando XC8.

2013 Mk-Pro Mekatronika

REGISTRO DE CONTROL DEL CCP


CCP1 Control Register (CCP1CON) P1M1 P1M0 DC1B1 DC1B0 BIT
CCP1M<3:0> DC1B<1:0> P1M<1:0>

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.

Programacin de Microcontroladores usando XC8.

2013 Mk-Pro Mekatronika

CAPTURE MODE
Prescaler 1, 4, 16
CCPx TMR1H Edge Detect and TMR1L

Set the CCP Interrupt flag

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

CAPTURE INITIALIZATION C CCP1


TMR1H

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

0 TIMER1 0 INCREMENTING!! 0Timer1 0 Value 0 0 Current

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

NO YES OUTPUT LOGIC


CCPx

??

Special Event Trigger

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

//Initialize Compare to set output on match CCP1CON = 0b00001000;


//Load compare value into CCPR1H:CCPR1L CCPR1H = 0b10000000; CCPR1L = 0x00; //Start Timer1 incrementing TMR1ON = 1; //Test CCP1IF for Timer1 match with CCPR1x while (!CCP1IF)

T1CON

CCP1CON

TMR1ON

0
PIR1

1 0

0
0 1
CCP1IF

Programacin de Microcontroladores usando XC8.

2013 Mk-Pro Mekatronika

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

Programacin de Microcontroladores usando XC8.

2013 Mk-Pro Mekatronika

10

PWM OPERATION
CCPR1L
DC1B1:DC1B0 bits in CCP1CON

0 10 0

CCPR1H

0
Period Start

10
COMPARATOR
TMR2

CCP1 Output Pin

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

TMR2 Reset to 0s Determines Period

Programacin de Microcontroladores usando XC8.

2013 Mk-Pro Mekatronika

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

CCP1 Output Pin

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

Programacin de Microcontroladores usando XC8.

PWM OPERATION
CCPR1L
DC1B1:DC1B0 bits in CCP1CON

Period 1

0 10
0

CCPR1H

1
Period Start

10
COMPARATOR
TMR2

CCP1 Output Pin

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

Programacin de Microcontroladores usando XC8.

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

CCP1 Output Pin

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

Programacin de Microcontroladores usando XC8.

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

duty cycle LSBs DC1B<1:0>

PWM Mode CCP1M <3:0>

0 1

Prescaler bits TOUTPS<3:0>

Prescaler bits T2CKPS<1:0>


TMR2ON

Programacin de Microcontroladores usando XC8.

2013 Mk-Pro Mekatronika

15

PULSE WIDTH MODULATION (PWM) LAB

OUTPUT COMPARE LAB

Potrebbero piacerti anche