This preview shows page 1-2-3-18-19-36-37-38 out of 38 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

TimersPeriod RegisterPostscalerPIC18 Timer SummarySo....what good are timers?Computing the Timer2 Interrupt Interval (Chap. 10.8, 10.9)Solutions for Timer2 Interrupt Frequency of 4 KHz (Period = 250 us)Timer2 ConfigurationSwitch Debounce Using Timer2 (Chap. 10.9)ISR for Switch Debouncemain() for Switch DebounceCapture/Compare/PWM Module (Chap 13.7)Pulse Width ModulationPWM DACPWM Motor ControlPIC18Fxx2 PWMPIC18Fxx2 PWM PeriodPIC18Fxx2 PWM Duty CyclePIC18Fxx2 PWM Duty CyclePWM Test: sqwave.csqwave.c (cont)PWM test: mtrpwm.cmtrpwm.c (cont)Other Timers on the PIC18F242Timer1 Block DiagramReading/Writing Timer1Measuring Time with Timer1/Timer3Sample ComputationsTimer1 ConfigurationCompare ModeExample: Wait for 1 ms to ElapseCompare Mode ConfigurationServosControlling servosControlling servos (cont.)Controlling servos (cont.)pcmgen.cpcmgen.c (cont.)V 0.7 1Timers•A timer on a µC is simply a counter• The input clock frequency to a timer can be prescaledso that it is some fraction of the system clock frequency.– This will slow down how fast the timer countsTimer2 on the PIC18 is an 8-bit counter.Prescaler for TMR2 is 1:1, 1:4, 1:16 of FOSC/4V 0.7 2Period Register• A timer can be programmed to roll over at any point using the period register.– An 8-bit timer would typically roll over to 0x00 once it reached 0xFF. – However, if the period register is set to 0x7F, then timer will roll over to 0x00 once it increments past 0x7F.PR2 is the period register for TMR2V 0.7 3Postscaler• Would like to generate an interrupt when a timer rolls over•APostscaler is used as a second counter – only generate an interrupt after timer rolls over N times.Postscaler – all values from 1:1 to 1:16.V 0.7 4PIC18 Timer Summary• Timer0: software selectable as either 8-bit or 16-bit, has a prescaler, clocked by FOSC/4 or external clock.• Timer1: 16-bit, has a prescaler, used with capture/compare module, clocked by FOSC/4 or external clock (has dedicated oscillator circuit, so can support a second external crystal).• Timer2: 8-bit, has prescaler/period register /postscaler, used with pulse-width modulation module• Timer3: A duplicate of Timer 1, shares Timer1’s dedicated oscillator circuit.• Capture/Compare module– 16-bit capture register, 16-bit compare register– Used with timer1 or timer3 to provide additional time measurement capabilitiesThe remainder of these notes concentrate on capability associated with Timer2V 0.7 5So....what good are timers?• Switch Debouncing• Waveform Generation• Sampling an input signal with ADC at a fixed frequency• Pulse Width Measurement• Pulse Width Modulation• Many other usesV 0.7 6Computing the Timer2 Interrupt Interval (Chap. 10.8, 10.9)The equation:Timer2 interrupt interval = PRE * (PR2+1) * POST * (1/(Fosc/4))has 3 unknowns: PRE, POST, and PR2.Use a spreadsheet to calculate. Assume we want a interrupt frequency of 4 KHz, Interrupt Period = 1/ 4 KHz = 1/ 4000 = 0.00025 = 250 us.Pick PRE and POST values, and solve the equation for PR2:PR2 = [ (Interrupt Period)/(4 * TOSC * PRE *POST)] - 1V 0.7 7Solutions for Timer2 Interrupt Frequency of 4 KHz (Period = 250 us)If PR2 > 255, then invalid solution as PR2 is an 8-bit registerV 0.7 8Timer2 ConfigurationV 0.7 9Switch Debounce Using Timer2 (Chap. 10.9)Use Timer2 periodic interrupt to allow switch to settleV 0.7 10ISR for Switch DebounceAssume Timer2 configured for 6 ms interrupt period.V 0.7 11main() for Switch DebounceV 0.7 12Capture/Compare/PWM Module (Chap 13.7)• Each CCP Module contains• 16-bit Capture Register, 16-bit Compare Register• PWM Master/Slave Duty Cycle Register• Pulse Width Modulation (PWM) mode is used to produce a square wave without processor intervention • Uses timer2 resource, and Compare register• Square wave on output pin 100% hardware generated, no software intervention• Can vary the duty cycle via the Timer2 PR2 register The remainder of these nodes discuss PWM mode, waveform generation using period interrupts. Capture/Compare is discussedlater.V 0.7 13Pulse Width ModulationPulse Width Modulation (PWM) is a common technique for controlling average current to a device such as a motor.PICRBxUses a square wave with fixed frequency, varying duty cycle.RBxHigh Pulse width (PWH)For a fixed frequency, the brightness of the LED will vary directly with duty cycle. The higher the duty cycle (the longerthe high pulse width), the brighter the LED because it stays on longer (more current delivered to the LED)V 0.7 14PWM DACCan build a low-component count DAC by using PWM to control the voltage across a capacitor. The capacitor voltage varies linearly with the duty cycle.V 0.7 15PWM Motor ControlPIC H-bridgeBrake111Coast0xxReverse101Forward110DirectionEN2A1ARB1RB0RC2/CCP1ENM2A1A2Y1YRB1 and RB0 control the motor direction. A logic ‘1’ at the H-bridge’s input becomes a high-current voltage applied to the motor while a logic ‘0’ becomes a high-current ground to the motor. Pulsing the enable pin with a PWM signal alternates applying current to the motor with periods in which no current is applied, causing the speed of the motor to vary with the PWM duty cycle.V 0.7 16PIC18Fxx2 PWMV 0.7 17PIC18Fxx2 PWM PeriodPeriod = (PR2+1) * 4 * (1/Fosc) * TMR2_PrescaleNote that when TMR2 is used for PWM, the postscaler is NOT used.V 0.7 18PIC18Fxx2 PWM Duty CycleDuty cycle has 10-bit resolution, upper 8-bits in CCPR1L, lower two bits are CCP1CON<5:4>CCPR1H used to double buffer the PWM operation.When TMR2=PR2, output SET, TMR2 reset to 0.When TMR2 = CCPR1H, then output RESETV 0.7 19PIC18Fxx2 PWM Duty CycleDuty Cycle = CCPR1L:CCPCON<5:4> * (1/Fosc) * TMR2_prescale10 bits.Recap: Period defined by PR2, duty cycle by CCPR1L + 2 bitsThe duty cycle time should be less than the period, but this is NOT enforced in hardware.If duty cycle > Period, then the output will always be a high (will never be cleared).In our calculations, will ignore lower 2-bits of duty cycle and only use 8-bits that are in CCPR1L.V 0.7 20PWM Test: sqwave.cGenerate a square wave using TMR2 and the PWM capability. // configure timer 2// post scale of 1TOUTPS3 = 0; TOUTPS2 = 0; TOUTPS1 = 0; TOUTPS0 = 0;// pre scale of 4T2CKPS1 = 0; T2CKPS0 = 1;// start timer 2TMR2ON = 1 ;PR2 = 255; // set timer2 PR register CCPR1L = (255 >> 1); // 255/2 = 50% duty cycle bitclr(CCP1CON, 5); // lower two bits are 00bitclr(CCP1CON, 4);PIC18RC2/CCP1generate square wave with varying duty cycle using PWM moduleV 0.7


View Full Document

MSU ECE 3724 - Timers

Documents in this Course
TEST 4

TEST 4

9 pages

Flags

Flags

6 pages

Timers

Timers

6 pages

Timers

Timers

54 pages

TEST2

TEST2

8 pages

Load more
Download Timers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Timers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Timers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?