Documente Academic
Documente Profesional
Documente Cultură
20-Aug-2010
Paranz
Timers
DAY 2 Afternoon Session August 20, 2010
20-Aug-2010 Paranz 2
Timer 1 (TMR1)
Can be used with the CCP module to perform capture or compare operations
Timer 2 (TMR2)
Can be used with the CCP module to perform PWM operations
20-Aug-2010 Paranz 3
TMR0 MODULE
8-bit timer/counter module
count up from 0x00 (0) 0xFF (255) overflow back to 0x00, can generate an interrupt
20-Aug-2010
Paranz
OVERVIEW
TMR0 architecture
SFRs
TMR0 module
20-Aug-2010 Paranz 6
TMR0 MODULE
Operating Mode:
Timer mode Counter mode
Associated SFRs
TMR0 OPTION INTCON
20-Aug-2010 Paranz 7
20-Aug-2010
Paranz
20-Aug-2010
Paranz
TMR0
1
No prescaler
Paranz
10
TMR0
#include <pic.h> void main() { TRISB0 = 0; RB0 = 0; T0CS = 0; PSA = 1; T0IE = 0; GIE = 0; while(1) { while(!T0IF); T0IF = 0; RB0 ^= 1; } }
20-Aug-2010 Paranz
Ex am ple
//RB0 pin is output, LED off //TMR0 uses the internal clock, Fosc/4 //No prescaler //Disable TMR0 interrupt
#1
TMR0: Interrupt
0 1 1
ISR is executed
20-Aug-2010
Paranz
12
TMR0: Interrupt
#include <pic.h>
void interrupt isr(void) { RB0 ^= 1; //toggle RB0 T0IF = 0; //clear TMR0 interrupt flag } void main() { .. .. .. }
20-Aug-2010 Paranz
Ex am ple
#2
13
TMR0: Interrupt
void main() { TRISB0 = 0; RB0 = 0; T0CS = 0; PSA = 1; T0IF = 0; T0IE = 1; GIE = 1; while(1); }
20-Aug-2010 Paranz
Ex am ple
#2
//Timer mode. //No prescaler //Clear TMR0 interrupt flag. //Enable TMR0 interrupt. //Enable all interrupts. //Infinite loop, do nothing. //Wait for ISR to be executed.
14
TMR0: Prescaler
Why use prescaler?
to slow down the TMR0
0
20-Aug-2010 Paranz 15
TMR0: Prescaler
with no prescaler (PSA=1)
TMR0 register increment every instruction clock TMR0 overflows period = 51.2 s
TMR0: Prescaler
with prescaler = 8
TMR0 register increment every 8 instruction clock TMR0 overflows period = 409.6 s
TMR0: Prescaler
20-Aug-2010
Paranz
18
TMR0: Example #3
#include <pic.h>
void interrupt isr(void) { RB0 ^= 1; //Toggle RB0 T0IF = 0; //Clear TMR0 interrupt flag } void main() { .. .. .. }
20-Aug-2010 Paranz
Ex am ple
#3
19
TMR0
void main() { TRISB0 = 0; RB0 = 0; T0CS = 0; PSA = 0; PS2 = 1; PS1 = 1; PS0 = 1; TMR0IF = 0; TMR0IE = 1; GIE = 1; while(1); }
20-Aug-2010 Paranz
Ex am ple
//RB0 pin is output //LED is off
#3
//TMR0 uses the instruction clock, Fosc/4 //prescaler is assigned to TMR0 //prescaler = 256
//clear TMR0 interrupt flag bit //enable TMR0 as an interrupt source //enable all interrupts //infinite loop, do nothing //but wait for ISR to be executed
20
TMR0: Prescaler
1 1 1
Prescaler = 256
20-Aug-2010 Paranz
Ex am ple
//skip timer variable
#4
void interrupt isr(void) { if(skptmr1) //If skip timer variable is not yet 0, --skptmr1; // decrement it. T0IF = 0; //Clear TMR0 interrupt flag. } void main() { .. .. .. }
20-Aug-2010 Paranz 23
Ex am ple
#4
//Clear TMR0 interrupt flag. //Enable TMR0 interrupt. //Enable all interrupts. //Approximately 1 second time delay.
//if skip timer variable is 0 //reset skip timer variable //toggle RB0 Paranz 24
Ex am ple
#4
Skptmr1 is decremented each time ISR is executed If skptmr1==0, ISR has execute 610x already
TMR0 overflow period
Why 610?
(4/20Mhz)x256x32 = 1.6384x10-3 or 610.35 Hz (rounded off to 610)
20-Aug-2010 Paranz 25
TMR0: Preset
To generate an accurate time delay using TMR0, use a preset value
TMR0 starts incrementing not from 0x00
Preset Value range from (0x00-0xFF) If Preset Value = 100, TMR0 increment 156x only before TMR0IF is set (overflow)
100 101 102 .. 254 255 0 (overflow)
TMR0: Preset
TMR0 Preset Value = 256 Period FOSC 4 Prescaler
Example:
TMR0 interrupt frequency = 5000 Hz
Period = 0.2 s
TMR0: Preset
TMR0 Period
FOS C
Prescaler
Calculated Preset
Actual Preset
200 s
20 Mhz
2 4 8 16 32 64 128 256
20-Aug-2010
TMR0: Preset
Calculated Preset should be adjust (TMR0 Preset + 2) as advised in the device datasheet
20-Aug-2010
Paranz
29
TMR0: Preset
#include <pic.h> volatile unsigned int skptmr1 = 0;
Ex am ple
#5
void interrupt isr(void) { TMR0 = 0x08; //Write preset value to TMR0 if(skptmr1) //If skip timer variable is not yet 0, --skptmr1; // decrement it. T0IF = 0; //Clear TMR0 interrupt flag } void main() { .. .. .. }
20-Aug-2010 Paranz 30
10
TMR0: Preset
void main() { TRISB0 = 0; RB0 = 0; T0CS = 0; PSA = 0; PS2 = 0; PS1 = 0; PS0 = 1; TMR0IF = 0; TMR0IE = 1; GIE = 1; while(1) { ..... } 20-Aug-2010
Paranz
Ex am ple
//RB0 pin is output //LED is off //TMR0 timer mode //Prescaler is assigned to TMR0 //Prescaler = 4
#5
//Clear TMR0 interrupt flag bit //Enable TMR0 as an interrupt source //Enable all interrupts
31
TMR0: Preset
void main() { //Initializations //... while(1) { if(skptmr1==0) { skptmr1 = 5000; RB0 ^= 1; } } }
20-Aug-2010 Paranz
Ex am ple
#5
32
20-Aug-2010
Paranz
33
11
Timer mode or Counter mode SFRs: T1CON, INTCON, PIR1, PIE1 Prescalers: 8,4,2,1 Use Preset to generate accurate time
Period FOSC TMR1 Preset Value = 65536 - 4 Prescaler
20-Aug-2010 Paranz 34
12