ccp con ccs

17
MODULO CAPTURA/COMPARACIÓN/PWM

Upload: gabo-casarrubias

Post on 28-Oct-2015

74 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CCP con CCS

MODULO CAPTURA/COMPARACIÓN/PWM

Page 2: CCP con CCS

CAPTURA/COMPARACIÓN/PWM (CCP) VISIÓN GENERAL• Captura• Mide la duración de un evento externo usando como entra el pin

• Comparación• Cambia el estado de un pin de salida o genera una interrrupción

cuando una cantidad especifica de tiempo ha pasado.

• Modulación de ancho de pulso (PWM)• Crea una salida de onda cuadrada con una frecuencia y ciclo de

trabajo definidoCCP MODE Recurso usado

Capture Timer 1

Compare Timer 1

PWM Timer 2Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 2

Page 3: CCP con CCS

CCP1M<3:0> Bits de selección de Modo del CCP configuran el modulo como una entrada de captura, salida de comparación o PWM

BIT FUNCTION

CCP1 Control Register (CCP1CON) P1M1 P1M0 DC1B1 DC1B0 CCP1M3 CCP1M2 CCP1M1 CCP1M0

REGISTRO DE CONTROL DEL ECCP

Modo PWMxx11

Modo comparador, dispara un evento especial1101

Modo comprador, genera una interrupción en software al coicidir0101

Modo comparador, limpia la salida al coicidirmatch1001

Modo comparación setea la salida al coicidir0001

Modo captura , cada 16 flancos de subida1110

Modo captura, cada 4 flancos de subida0110

Modo captura cada flanco de subida1010

Modo de captura, cada flanco de bajada0010

Sin usar (reservado)1100

modo comparador, togglea la salida al coincidir0100

Sin usar (reservado)1000

Capture/Compare/PWM Apagado (resetea el modulo CCP)0000

CCP Mode SelectedCCPxM0CCPxM1CCPxM2CCPxM3

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 3

Page 4: CCP con CCS

DC1B<1:0> PWM duty cycle 2 LSB’s (8 MSB’s located in CCPR1L)

CCP1M<3:0> CCP Mode Select Bits configure the module as Input Capture, Output Compare, or PWM

BIT FUNCTION

CCP1 Control Register (CCP1CON) P1M1 P1M0 DC1B1 DC1B0 CCP1M3 CCP1M2 CCP1M1 CCP1M0

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 4

REGISTRO DE CONTROL DEL CCP

Page 5: CCP con CCS

P1M<1:0>These PWM output configuration bits are available for Enhanced CCP (ECCP) modules only. They provide half-bridge or full-bridge output steering control.

DC1B<1:0> PWM duty cycle 2 LSB’s (8 MSB’s located in CCPR1L)

CCP1M<3:0> CCP Mode Select Bits configure the module as Input Capture, Output Compare, or PWM

BIT FUNCTION

CCP1 Control Register (CCP1CON) P1M1 P1M0 DC1B1 DC1B0 CCP1M3 CCP1M2 CCP1M1 CCP1M0

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 5

REGISTRO DE CONTROL DEL CCP

Page 6: CCP con CCS

CCPx

CAPTURE MODE

TMR1H TMR1L

CCPRxH CCPRxL

Prescaler÷1, 4, 16

Edge Detectand

Set the CCPInterrupt flag

Single Buffered

Capture every rising or falling edge, or every 4th or 16th rising edge. Store capture time

here

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 6

Page 7: CCP con CCS

CAPTURE INITIALIZATION … C;Turn off CCP module CCP1CON = 0x00;;Make sure Timer1 is off TMR1ON = 0;;Clear Timer1 registers TMR1H = 0x00; 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 = b’00000110’;;Start Timer1 incrementing TMR1ON = 1;;Test the interrupt flag for capture while (!CCP1IF);

TMR1H

TMR1ON

CCP1IF

TMR1L

PIR1

0

CCP1CON

T1CON

1

0

CCPR1H

CCPR1L

1

Rising Edge Detected!!

Captured!

CCP1Pin

01111

1st2nd3rd4th

0 0 0 0 0 0 0 011

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

TIMER1 INCREMENTING!!Current Timer1 Value

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 7

Page 8: CCP con CCS

YES CCPx

Special Event Trigger

COMPARE MODE

OUTPUTLOGIC

OUTPUTLOGIC

TMR1H TMR1L

COMPARATORDoes TMR1H:TMR1L =

CCPRxH:CCPRxL ??

COMPARATORDoes TMR1H:TMR1L =

CCPRxH:CCPRxL ??

NO

CCPRxH CCPRxL

Set the CCPInterrupt flag

Set or clear the CCP pin or clear Timer1 and start an ADC conversion

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 8

Page 9: CCP con CCS

COMPARE INITIALIZATION … C//Turn off the CCP moduleCCP1CON = 0x00;

//Turn off Timer1TMR1ON = 0;

//Clear Timer1 result registersTMR1H = 0x00;TMR1L = 0x00;

//Disable CCP1 interrupt and make sure //its flag is clearCCP1IE = 0;CCP1IF = 0;

//Make CCP1 pin outputTRISC2 = 0;

//Initialize Compare to set output on matchCCP1CON = 0b00001000;

//Load compare value into CCPR1H:CCPR1LCCPR1H = 0b10000000;CCPR1L = 0x00;

//Start Timer1 incrementingTMR1ON = 1;

//Test CCP1IF for Timer1 match with CCPR1xwhile (!CCP1IF)

TMR1H

TMR1ON

CCP1IF

TMR1L

PIR1

0

CCP1CON

T1CON

CCPR1H

CCPR1L

01

1

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

1

111 111 11 1 1 1 1

0 1 1 1 1 1 1 11

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 9

Page 10: CCP con CCS

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

Period RegisterPR2

Timer2 ControlT2CON

Duty Cycle RegisterCCPRxL

CCP Control RegisterCCPxCON

DescriptionRegister

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 10

Page 11: CCP con CCS

PWM OPERATION

CCP1 pin

Latch

CCP1 Output Pin

COMPARATOR

COMPARATOR

R

S

8

8

TMR2 is concatenated with 2 bits from FOSC, or 2 bits from Prescaler to create a 10 bit time base

10

10

10

1

Period Start

0 0 0 0 0 0 0 0 0 0TMR2

PR2

0 0 0 0 0 0 1 1

CCPR1H

0 0 0 0 0 0 0 0 0 0

CCPR1L

DC1B1:DC1B0bits in CCP1CON

0 0 0 0 0 0 0 1 1 1

Determines Period

0 0 0 0 0 0 1 0 11 11

TMR2 Reset to 0’s

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 11

Page 12: CCP con CCS

PWM OPERATION

CCP1 pin

Latch

CCP1 Output Pin

COMPARATOR

COMPARATOR

DoubleBuffer

R

S

8

8

10

10

10

10

Period Start

0 0 0 0 0 0 0 0 0 0TMR2

PR2

0 0 0 0 0 0 1 1

CCPR1H

0 0 0 0 0 0 0 0 0 0

CCPR1L

DC1B1:DC1B0bits in CCP1CON

0 0 0 0 0 0 0 1 1 1

Determines Duty Cycle

0 0 0 0 0 0 0 1 1 10 0 0 0 0 0 0 1 1 1

0 0 0 0 0 0 1 0 111 111 10

0 0 0 0 0 0 0 1 1 1

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 12

Page 13: CCP con CCS

PWM OPERATION

CCP1 pin

Latch

CCP1 Output Pin

COMPARATOR

COMPARATOR

R

S

8

8

10

10

10

Period 1

10

Period Start

0 0 0 0 0 0 0 0 0 0TMR2

PR2

0 0 0 0 0 0 1 1

CCPR1H

0 0 0 0 0 0 0 1 1 1

CCPR1L

DC1B1:DC1B0bits in CCP1CON

0 0 0 0 0 0 0 1 1 10 0 0 0 0 0 0 1

0 0 0 0 0 0 0 0 111 11 1 1 1

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 13

Page 14: CCP con CCS

PWM OPERATION

CCP1 pin

Latch

CCP1 Output Pin

COMPARATOR

COMPARATOR

DoubleBuffer

R

S

8

8

10

10

10

Period 2

10

Period Start

Period 1

0 0 0 0 0 0 0 0 0 0TMR2

PR2

0 0 0 0 0 0 1 1

CCPR1H

0 0 0 0 0 0 0 1 1 1

CCPR1L

DC1B1:DC1B0bits in CCP1CON

0 0 0 0 0 0 0 1 1 1

Determines Duty Cycle

0 0 0 0 0 0 0 1 1 10 0 0 0 0 0 0 1 1 1

TMR2 Reset to 0’sTMR2 Reset to 0’s

0 0 0 0 0 0 1 01 111 111 1111 11

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 14

Page 15: CCP con CCS

PWM INITIALIZATION … C

PR2

10 1 1 1 1 1 1

CCP1CON

0 0 0 0 0

PWM ModeCCP1M <3:0>

duty cycleLSBs

DC1B<1:0>

CCPR1L

10 0 0 1 1 1 1

TMR2

000 0 0000

111

T2CON

1

TMR2ON

0 0 0 0 0 0 0

Prescaler bitsTOUTPS<3:0>

Prescaler bitsT2CKPS<1:0>

0

//Turn off CCP1 pin by making it an inputTRISC2 = 1;

//Turn off Timer2TMR2ON = 0;

//Clear Timer2TMR2 = 0x00;

//Set up Period and Duty CyclePR2 = 0b01111111; //Load a Period ValueCCPR1L = 0b00011111; //Load a Duty Cycle Value

//Configure CCP module for PWM// and LSB’s of Duty Cycle = b’10’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;

Programación de Microcontroladores usando XC8. 2013 Mk-Pro Mekatronika 15

Page 16: CCP con CCS

PULSE WIDTH MODULATION(PWM) LAB

Page 17: CCP con CCS

OUTPUT COMPARE LAB