This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 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 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1V 0.1 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 PIC16 is an 8-bit counter.Prescaler for TMR2 is 1:1, 1:4, 1:16 of Fosc/4V 0.1 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.1 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.1 4PIC16 Timer Summary• Timer0: 8-bit, has a prescaler, used by watchdog timer• Timer1: 16-bit, has a prescaler, used with capture/compare module• Timer2: 8-bit, has prescaler/period register /postscaler, used with pulse-width modulation module• Capture/Compare module– 16-bit capture register, 16-bit compare register– Used with timer1 to provide additional time measurement capabilitiesV 0.1 5So....what good are timers?• Better than Ginzu knives – a 1001 uses!• Usage #1 – generate an interrupt at fixed frequency– Square wave generation– Sampling an input signal with A/DI bought a set of Ginzu knives for only three easy payments of $29.95 and they came with a lifetime guarantee. When the handles fell off, I returned the knives with my lifetime guarantee asking for a refund. They wrote back saying, "The guarantee was for the lifetime of the knives. Obviously, the knives are dead, so the guarantee is no longer valid." (internet humor)V 0.1 6Square Wave GenerationOne method: Generate a periodic interrupt at ½ the period time.Complement an output port each interrupt time. This will generate a square wave with a 50% duty cycle. PeriodLow Pulse width (LPW)interruptinterruptinterruptHigh Pulse width (HPW)Frequency = 1/Periodduty cycle = HPW/Period * 100%RBx2V 0.1 7ExampleAssume a 10MHz oscillator frequency. Generate a square wave with a 1KHz frequency.Period = 1.0 /1KHz = 1/1000 = 0.001 = 1 ms (1 millisecond)Interrupt every 0.5 ms or 500 µs (interrupt frequency = 2 KHz)Timer2 interrupt interval = Pre * (PR2+1) * Post * (1/(Fosc/4))IF (interrupt freq = 1/[Pre * (PR2+1) * Post * (1/(Fosc/4))]PR2 = ( 1/[Pre * IF * Post * (1/(Fosc/4))]) - 1where Pre is prescaler (1, 4, 16), PR2 is period match register, Post is postscalerThis is an equation with THREE unknowns! Multiple solutions.Use a spreadsheet!V 0.1 8SpreadsheetUsing Ti mer2, sqwaveCrysal Frequency 10000000 10000000 10000000 10000000 10000000prescale 1 1 4 4 16desired freq 2000 2000 2000 2000 2000Postscale 1 5 1 3 1PR2 1249 249 311.5 100 77.125Actual freq 2000.00 2000.00 1996.81 2062.71 2003.21%diff 0.00% 0.00% -0.16% 3.14% 0.16%No solution for Postscale=Prescale =1, PR2 too large.No solution for Postscale=1, Prescale =4, PR2 too large.V 0.1 9Pulse Width ModulationPulse Width Modulation (PWM) is a common technique for controlling average current to a device such as a motor.16F873RBxHigh Pulse width (HPW)RBxFor 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.1 10PWM Motor Control16F873RBx+30VDC motorNPN BJT Power TransistorBaseCollectorEmitterDuty cycle applied to Base terminal controls current. The base-to-emitter circuit looks like a diode to the PIC. Gain of the NPN and base current determines maximum current delivered.V 0.1 11Capture/Compare/PWM Module• Each CCP Module contains• 16-bit Capture Register, 16-bit Compare Register• PWM Master/Slave Duty Cycle Register• 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 V 0.1 12PIC16F873 PWMCCPR1H, CCPR1L are the upper/lower bytes of compare registerSquare wave appears here.3V 0.1 13PIC16F873 PWM PeriodPeriod = (PR2+1) * 4 * (1/Fosc) * TMR2_PrescaleNote that when TMR2 is used for PWM, the postscaler is NOT used.V 0.1 14PIC16F873 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.1 15PIC16F873 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)V 0.1 16sqwave.cGenerate a square wave using TMR2 and the PWM. bitclr(T2CON, 6); /* TMR2 post scale =1 */| bitclr(T2CON, 5);bitclr(T2CON, 4);bitclr(T2CON, 3);bitclr(T2CON, 1); /* TMR2 pre scale of 4 */bitset(T2CON, 0);bitset(T2CON, 2); /* start timer 2 */ /* set up PWM */PR2 = 255; /* set timer2 PR register */CCPR1L = (255 >> 1); /* 50% duty cycle */ bitclr(CCP1CON, 5); /* lower 2 bit= 00 */bitclr(CCP1CON, 4);V 0.1 17sqwave.c (cont)/* set CCP1 output */bitclr(TRISC,2); /* output mode *//* PWM Mode */bitset(CCP1CON, 3);bitset(CCP1CON, 2);bitclr(CCP1CON, 1);bitclr(CCP1CON, 0);while (1) {/* prompt user, allow user to change period by changing PR2) */}At this point, the square wave is active, no other intervention necessary.V 0.1 18ledpwm.cUse potentiometer and PIC A/D to adjust duty cycle for PWM to LED16F873CCP1AN0Vdd10K Pot.Analog inputUse this adjust duty cycleWill initialize PWM module in the same way as before, except TMR interrupt will be enabled. In ISR, read A/D value and update duty cycle.4V 0.1 19ledpwm.c (cont)void interrupt timer2_isr(void){update_pwm();// TMR2IF = 0; clear timer interrupt flagbitclr(PIR1,1);}This subroutine does work of reading A/D and changing the duty cycle.V 0.1 20ledpwm.c (cont)update_pwm(){unsigned char rval1;rval1 = ADRESH; /* A/D value left justified*/CCPR1L = rval1; /* upper 8 bits of duty cycle */rval1 = ADRESL;/* update lower two bits of duty cycle */if (bittst(rval1,7)) bitset(CCP1CON, 5);else


View Full Document

MSU ECE 3724 - Timers

Documents in this Course
Timers

Timers

38 pages

TEST 4

TEST 4

9 pages

Flags

Flags

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?