1 / 49

PLT 206 Microcontroller Systems CCP

Learn about the CCP module modes of PIC16 microcontroller systems and how to program them in Hitech-C. Includes a detailed example.

hvigil
Download Presentation

PLT 206 Microcontroller Systems CCP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PLT 206Microcontroller SystemsCCP PIC16 Timer Programming in Hitech-C

  2. Today’s Lecture • List the CCP module/s of PIC16 and their associated registers. • Describe the various modes of the PIC16 CCP module. • Program the PIC16 CCP module in ‘Hitech C’.

  3. Introduction • Stands for Capture/Compare/PWM. • Allows the user to time and control different events. • The (CCP) module is a programmable software able to operate in one of the three modes: • A Capture input • A Compare output • A Pulse Width Modulation (PWM).

  4. Introduction • PIC 16F CCP resource :

  5. Capture • Consider a stop watch • You start the stop watch • The stop watch runs while you wait for an event(s) to happen • Once the event(s) happen the stop watch is stopped and the time is recorded • For the PIC • Timer 0, Timer 1 or Timer 2 start • An event occurs on the CCPxpin • A the moment the event occurs the time is captured and stored in CCPRxH:CCPRxL • The contents of an internal 16 bit timer, upon detecting an n-th rising or falling edge, to be written to on-board special function registers Figure 1

  6. Compare • Consider your alarm clock • When the time is 07:00 the alarm goes off. • This is a compare of the current time with a stored time. • If the two match then an event (the alarm goes off) happens. • For the PIC • When the value of timer1 matches what is stored in CCPR1H:CCPR1L the CCP1 pin output is changed and the CCP1IF bit is set. • Timer2 and CCP2 can also be used for this purpose. • Generates an interrupt, or change on output pin, when Timer 1 matches a pre-set comparison value. Figure 2

  7. Pulse Width Modulation (PWM) • Creates a re-configurable square wave duty cycle output at a user set frequency. • The higher duty cycle, the better performance of an output. Figure 3

  8. CCP resources : • Single source clocks for Capture and Compare or PWM modes. • Two separate CCP Modules(CCP1 and CCP2). • Each CCP module supports Capture, Compare or PWM modes

  9. CCP1 Module • 16-bit register. • Consists of the CCPR1L and CCPR1H registers. • Used for capturing or comparing with binary number stored in the timer register TMR1 (TMR1H and TMR1L). Figure 4

  10. CCP1 Module • The CCP1CON register controls the operation of CCP1. • CCP Register nomenclature • CCPxCON == CCP1CON or CCP2CON • CCPxSTAT == CCP1STAT or CCP2STAT • CCPxL == CCP1L or CCP2L • CCPxH == CCP1H or CCP2H

  11. CCP1 Module

  12. CCP1 Module

  13. CCP2 Module • CCP1 and CCP2 modules are identical in operation. • Difference exist when CCP2 operates in Compare mode. • Timer1 reset signal will automatically start A/D conversion (if enabled).

  14. CCP2 Module

  15. Capture input • CCP pin is set to input and monitored for a change of state: • Every falling edge • Every rising edge • Every 4th rising edge • Every 16th rising edge • Upon detecting an nth rising or falling edge, timer register value(16-bit) is copied to the CCP register. • Allows timing for the duration of an event.

  16. Capture input • Example • If CCP1CON = 0b00000101; • Every rising edge • Low-to-High transition • Timer1 must be turned on as continuous clock source, T1CON= 0b00000001;

  17. Programming in capture mode • Initialize CCP1CON for compare. CCP1CON=0b00000100; //Capture rising edge • CCP1 as input pin. TRISC=0b00000100; //Portc bit 2 as input • Initialize T1CON for capture. T1CON=0b00000000; //T1CON for capture • Read register value on the first rising edge and save it. • Read register value on the second rising edge and save it. • Subtract the value in step 4 from the value in step 3.

  18. Example 1 • Write a program to measure the period of the pulse being fed to CCP pin. PORTB CCP1(RC2) PORTC PIC16F877A PORTD Figure 5

  19. Example 1: Program #include <htc.h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED5 RB5 void pic_init(void); void timer_init(void); void ccp_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module ccp_init(); //initialize CCP Module while(1) { while(PIR1bits.CCP1IF==0) { T1CON=0b00000001; //start Timer1 PIR1=0b00000000; }

  20. Example 1: Program while(PIR1bits.CCP1IF==1) { T1CON=0b00000000; //stop Timer1 PIR1bits.CCP1IF=0; PORTB=CCPR1L; PORTD=CCPR1H; } } } void pic_init(void) { TRISB=0b00000000; PORTB=0b00000000; TRISC=0b11111111; PORTC=0b00000000; TRISD=0b00000000; PORTD=0b00000000; }

  21. Example 1: Program void timer_init(void) { T1CON=0b00000000; PIR1=0b00000000; TMR1H=0; TMR1L=0; } void ccp_init(void) { CCP1CON=0b00000100; //Capture rising edge CCPR1H=0; CCPR1L=0; }

  22. Example1: Simulation Result • When clock at 150cyc low and 150cyc high, PIR will interrupt at CCPR1L = DFH (high to low). Figure 8 Figure 7 Figure 9

  23. Example1: Simulation Result • After 2nd cycle • PIR will interrupt at CCPR1H=2H and CCPR1L=0BH. Figure 10 Figure 11

  24. Example1: Simulation Result • Subtracting these two values: • 20BH-DFH=12CH→300d Figure 12

  25. Compare input • Generate an interrupt, or change on output pin, when Timer1 matches a pre-set comparison value. • Load initial values to Timer1(TMR1H:TMR1L) and CCPRxH:CCPRxL. • Timer1(TMR1H:TMR1L) value counts up and constantly compared with CCPRxH:CCPRxL. • When match occurs CCP will perform(Event trigger Options): • Low to High Transition • High to Low Transitions • Interrupt Request Without Pin Change • Trigger Special Event

  26. Compare input • Example • If CCP1CON=0b00001000; • Compare mode • Set output (CCP1IF bit is set) upon match. • Timer1 must be turned on as continuous clock source, T1CON= 0b00000001;

  27. Programming in compare mode • Initialize CCP1CON for compare. CCP1CON=0b00001000; //Compare mode • CCP1 as input pin. TRISC=0b00000100; //Portc bit 2 as input • Initialize T1CON for compare. T1CON=0b00000000; //T1CON for compare • Start Timer1. T1CON=0b00000001; //Start Timer1 • Monitor the CCP1IF flag.

  28. Example 2 • Write a program to compare the period of the pulse being fed to CCP pin. LED CCP1(RC2) T1CKI(RC0) PIC16F877A Figure 13

  29. Example 2: Program #include <htc.h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED0 RD0 void pic_init(void); void timer_init(void); void ccp_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module ccp_init(); while(1)

  30. Example 2: Program { T1CON=0b0000011; //start Timer1 if(PIR1bits.CCP1IF==1) { T1CON=0b00000010; //stop Timer1 PIR1bits.CCP1IF=0; TMR1H=0; TMR1L=0; } } } void pic_init(void)

  31. Example 2: Program { TRISB=0b00000000; PORTB=0b00000000; TRISC=0b00000001; PORTC=0b00000000; TRISD=0b00000000; PORTD=0b00000000; } void timer_init(void) { T1CON=0b00000010; PIR1=0b00000000;

  32. Example 2: Program TMR1H=0x00; TMR1L=0x00; } void ccp_init(void) { CCP1CON=0b00001000; //compare mode CCPR1H=0x00; CCPR1L=0x08; }

  33. Example 2: Simulation Result CCP1 high after 8 pulse Figure 14

  34. Example 2: Simulation Result • CCPR1H and CCPR1L are loaded with 0008H. • When timer1 starts count, TMR1L and TMR1H will increments. These values will be constantly compared with CCPR1H and CCPR1L. • If match, CCP1IF will be set to ‘1’, thus making CCP1(RC2) High.

  35. PWM • Create pulses with variable widths. • A PWM output is basically a square waveform with a specified period and duty cycle. • The pulse width modulation (PWM) mode produces a PWM output. • CCPx pin produces up to a 10-bit resolution PWM output. Figure 15

  36. PWM - Period • The PWM period is specified by writing to the PR2 register. • The PWM period can be calculated using the following formula: • PWM Period = [(PR2) + 1] • 4 • TOSC •(TMR2 Prescale Value) • Where: • PR2 is the value loaded into Timer 2 register • TMR2PS is the Timer 2 prescaler value • TOSC is the clock oscillator period (seconds) • The PWM frequency is defined as 1/(PWM period).

  37. PWM - Period • Example: • If XTAL=20MHz and FPWM = 1.22kHz; • PR2 = 4,097 – which is larger than 256(8bit) • Reducing the FOSC through prescaler value (set to 16) • PR2 = 255

  38. PWM - Duty Cycle • Portion of pulse that stays HIGH relative to the entire period. • Supports up to 10 bit resolution. • CCPx pin TRIS bit must be cleared to configure the pin as output. • PWM mode is enabled by placing CCPxM3:CCPxM0 = b’11xx. • Specified by writing to the CCPR1L register and to the CCP1CON<5:4> bits. • The CCPR1L contains the eight LSbs and the CCP1CON<5:4> contains the two MSbs.

  39. PWM - Duty Cycle • The following equation is used to calculate the PWM duty cycle in time: • PWM Duty Cycle =(CCPR1L:CCP1CON<5:4>) •TOSC • (TMR2 Prescale Value)

  40. Programming in PWM mode • Set PWM period through PR2 register. PR2=0x37; • Set PWM duty cycle through CCPR1L for the first 8-bits. CCPR1L=0b00011011; • Set CCP pin as an output. TRISCbits.CCP1=0; • Set prescale value through T2CON. T2CON= 0b00000000; • Clear TMR2 register. TMR2=0;

  41. Programming in PWM mode • Configure CCP1CON for PWM, CCPxX and CCPxY for the remaining 10-bits CCP1CON=0b00001100; • Start Timer2. T2CON=0b00000101;

  42. Example 3 • PWM pulses must be generated from pin CCP1 of a PIC16F877A microcontroller. The required pulse period is 44s and the required duty cycle is 50%. Assuming that the microcontroller operates with a 20MHz crystal, calculate the values to be loaded into the various registers • TOSC = 1/20MHz = 5x10-8s • PWM duty cycle is 44s/2 = 22s.

  43. Example 3 CCPR1L:CCP1CON<5:4> • Which is equivalent to 10-bit binary : “00 01101 110” PWM Mode XX101100 CCP1CON=0b00101100; CCPR1L=0b00011011

  44. Example 3 - Program #include <htc.h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 20000000 void pic_init(void); void timer_init(void); void ccp_init(void); main() { pic_init(); //initialize PIC timer_init(); //initializeTimer Module ccp_init(); while(1)

  45. Example 3 - Program { T2CON=0b00000101; //start Timer2 while(PIR1bits.TMR2IF==0) { PIR1bits.TMR2IF=0; } } } void pic_init(void) { TRISC=0b00000000; PORTC=0b00000000; }

  46. Example 3 - Program void timer_init(void) { T2CON=0b00000000; PR2=0x36; TMR2=0; } void ccp_init(void) { CCPR1L=0b00011011; CCP1CON=0b00101100; //PWM mode }

  47. Example 3 - Simulation Result Figure 17

  48. Example 3 - Simulation Result Figure 18

  49. References • PIC16F87XA Data Sheet, Microchip Technology Incorporated, 2003. • PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. McKinlay and D. Causey, Prentice Fall, 2008. • Advanced Pic Micro Controller Projects in c: From Usb to Rtos With the Pic 18f Series, Dogan Ibrahim, Elsevier Ltd, 2008.

More Related