1 / 34

ECT 357

ECT 357. Ch 13 Timers. Today’s Quote: Some people complain because God put thorns on roses, while others praise Him for putting roses among thorns.

lada
Download Presentation

ECT 357

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. ECT 357 Ch 13 Timers

  2. Today’s Quote: Some people complain because God put thorns on roses, while others praise Him for putting roses among thorns. Finally, brethren, whatsoever things are true, whatsoever things are honest, whatsoever things are just, whatsoever things are pure, whatsoever things are lovely, whatsoever things are of good report; if there be any virtue, and if there be any praise, think on these things. Philippians 4:8

  3. Timer Properties • 3 – 16 bit timers (Timer 0, 1, 3) • 1 – 8 bit timer (Timer 2) • Software extendable to any size • Interrupt capable • CCP facility • Internal or external measurements • Synchronized read and write • Prescaling

  4. Timer0 Initialization • Program T0CON register (pg 53) • MOVLF “10000000”, T0CON bit 7 Enable Timer0 operation bit 6 8 bit counter enable bit 5 internal/external clock source bit 4 rising/falling clock edge bit 3 Prescaler on bits 2, 1 and 0 Prescaler

  5. Timer1 Initialization • Program T1CON register (pg 170) • MOVLF “10001101”, T1CON bit 7 TMR1H buffer enable bit 6 “0” not used bits 5 and 4 Prescaler bit 3 Timer 1 oscillator enable bit 2 External synchronization bit 1 External/Internal clock select bit 0 Enable Timer1 operation

  6. Timer2 Initialization • Program T2CON register • MOVLF “00000100”, T2CON bit 7 “0” not used bits 6, 5, 4, and 3 output postscale bit 2 Enable Timer2 operation bits 1 and 0 Prescaler

  7. Timer3 Initialization • Program T3CON register (pg 181) • MOVLF “10001001”, T3CON bit 7 TMR3H buffer enable bits 6 and 3 CCP options bits 5 and 4 Prescaler bit 2 External synchronization bit 1 External/Internal clock select bit 0 Enable Timer3 operation

  8. Timer0 Interval Measurements Initial save: movff TMR0L,TIMEL movff TMR0H,TIMEH Determine time: movf TIMEL,W subwf TMR0L,W movwf TIMEL movf TIMEH,W subwfb TMR0H,W movwf TIMEH movlw 0x05 subwf TIMEL,F btfss STATUS,C decf TIMEH,F

  9. Timer2 Interval Measurements Initial save: movff TMR2,TIMEL Determine time: movf TIMEL,W subwf TMR2,W movwf TIMEL movlw 0x03 subwf TIMEL,F

  10. Capture and Compare • Used for external time measurements • 2 CCP modules can be used with timer 1 and 3 or both on the same timer. • Separate prescalers • External triggering options (pg 179 & 182)

  11. CCP Modules 1 and 2 Each CCP (Capture/Compare/PWM) module contains a 16-bit register which can operate as a 16-bit Capture register, as a 16-bit Compare register or as a PWM Master/Slave Duty Cycle register. The operation of CCP1 is identical to that of CCP2, with the exception of the special event trigger.

  12. CCP1CON/CCP2CON

  13. CCP1 Module Capture/Compare/PWM Register 1 (CCPR1) is comprised of two 8-bit registers: CCPR1L (low byte) and CCPR1H (high byte). The CCP1CON register controls the operation of CCP1. All are readable and writable.

  14. CCP2 Module Capture/Compare/PWM Register2 (CCPR2) is comprised of two 8-bit registers: CCPR2L (low byte) and CCPR2H (high byte). The CCP2CON register controls the operation of CCP2. All are readable and writable.

  15. CCP Mode – Timer Resource

  16. Interaction of 2 CCP Modules

  17. Capture Mode In Capture mode, CCPR1H:CCPR1L captures the 16-bit value of the TMR1 or TMR3 registers when an event occurs on pin RC2/CCP1. An event is defined as one of the following: • • every falling edge • • every rising edge • • every 4th rising edge • • every 16th rising edge • The event is selected by control bits CCP1M3:CCP1M0 (CCP1CON<3:0>). When a capture is made, the interrupt request flag bit CCP1IF (PIR1<2>) is set; it must be cleared in software. If another capture occurs before the value in register CCPR1 is read, the old captured value is overwritten by the new captured value.

  18. CCP Pin Configuration In Capture mode, the RC2/CCP1 pin should be configured as an input by setting the TRISC<2> bit. Note: If the RC2/CCP1 is configured as an output, a write to the port can cause a capture condition.

  19. TIMER1/TIMER3 MODE SELECTION The timers that are to be used with the capture feature (either Timer1 and/or Timer3) must be running in Timer mode or Synchronized Counter mode. In Asynchronous Counter mode, the capture operation may not work. The timer to be used with each CCP module is selected in the T3CON register.

  20. Software Interrupt When the Capture mode is changed, a false capture interrupt may be generated. The user should keep bit CCP1IE (PIE1<2>) clear to avoid false interrupts and should clear the flag bit, CCP1IF, following any such change in Operating mode.

  21. CCP Prescaler There are four prescaler settings, specified by bits CCP1M3:CCP1M0. Whenever the CCP module is turned off or the CCP module is not in Capture mode, the prescaler counter is cleared. This means that any RESET will clear the prescaler counter. Switching from one capture prescaler to another may generate an interrupt. Also, the prescaler counter will not be cleared, therefore, the first capture may be from a non-zero prescaler.

  22. Capture Block Diagram

  23. Compare Mode In Compare mode, the 16-bit CCPR1 (CCPR2) register value is constantly compared against either the TMR1 register pair value, or the TMR3 register pair value. When a match occurs, the RC2/CCP1 (RC1/CCP2) pin is: • driven High • driven Low • toggle output (High to Low or Low to High) • remains unchanged The action on the pin is based on the value of control bits CCP1M3:CCP1M0 (CCP2M3:CCP2M0). At the same time, interrupt flag bit CCP1IF (CCP2IF) is set.

  24. Special Event Trigger The special event trigger output of CCP1 resets the TMR1 register pair. This allows the CCPR1 register to effectively be a 16-bit programmable period register for Timer1. The special trigger output of CCPx resets either the TMR1 or TMR3 register pair. Additionally, the CCP2 Special Event Trigger will start an A/D conversion if the A/D module is enabled.

  25. PWM Mode In Pulse Width Modulation (PWM) mode, the CCP1 pin produces up to a 10-bit resolution PWM output. Since the CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2> bit must be cleared to make the CCP1 pin an output.

  26. PWM Block Diagram

  27. PWM Output A PWM output has a time-base (period) and a time that the output stays high (duty cycle). The frequency of the PWM is the inverse of the period (1/period).

  28. 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) PWM frequency is defined as 1 / [PWM period]. When TMR2 is equal to PR2, the following three events occur on the next increment cycle: • TMR2 is cleared • The CCP1 pin is set (exception: if PWM duty cycle = 0%, the CCP1 pin will not be set) • The PWM duty cycle is latched from CCPR1L into CCPR1H

  29. PWM Duty Cycle The PWM duty cycle is specified by writing to the CCPR1L register and to the CCP1CON<5:4> bits. Up to 10-bit resolution is available. The CCPR1L contains the eight MSbs and the CCP1CON<5:4> contains the two LSbs. This 10-bit value is represented by CCPR1L:CCP1CON<5:4>.

  30. PWM Duty Cycle Calculations The following equation is used to calculate the PWM duty cycle in time: PWM duty cycle = (CCPR1L:CCP1CON<5:4>) • TOSC • (TMR2 prescale value)

  31. Duty Cycle Updates CCPR1L and CCP1CON<5:4> can be written to at any time, but the duty cycle value is not latched into CCPR1H until after a match between PR2 and TMR2 occurs (i.e., the period is complete). In PWM mode, CCPR1H is a read only register. The CCPR1H register and a 2-bit internal latch are used to double buffer the PWM duty cycle. This double buffering is essential for glitchless PWM operation. When the CCPR1H and 2-bit latch match TMR2 concatenated with an internal 2-bit Q clock or 2 bits of the TMR2 prescaler, the CCP1 pin is cleared.

  32. PWM Resolution The maximum PWM resolution (bits) for a given PWM frequency is given by the equation:

  33. PWM Setup The following steps should be taken when configuring the CCP module for PWM operation: 1. Set the PWM period by writing to the PR2 register. 2. Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits. 3. Make the CCP1 pin an output by clearing the TRISC<2> bit. 4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON. 5. Configure the CCP1 module for PWM operation.

More Related