Timers
Timers
Timers
INTRO
Four Timers in PIC18f452
Timer 0
Timer 1
Timer 2
Timer 3
TIMER0
TIMER 0 Timer0 can operate as a timer or as a
counter.
The Timer0 module has the following features: 1. When the clock source is the instruction
1. Software selectable as an 8-bit or 16-bit timer/counter cycle clock(external oscillator), it
2. Readable and writable operates as a timer.
2. When the clock source is the T0CKI pin,
3. Dedicated 8-bit software programmable prescaler
it operates a counter
4. Clock source selectable to be external or internal
5. Interrupt-on-overflow from FFh to 00h in 8-bitmode and FFFFh to 0000h in 16-bit mode
6. Edge select for external clock
Registers of Timer0
1. T0CON (Timer0 control register)
2. TMR0L (Timer0 low byte)
3. TMR0H (Timer0 high byte)
INSIDE T0CON REGISTER
Step 2: Step 2:
Value= 256 – n Value= 65536 – n
xx= binary of “Value” yyxx= binary of “Value”
TMR0L=xx TMR0L=xx
TMROH=yy
void main( )
void delayTimer0( )
{
{
ADCON1 = 0x07;
T0CON = 0x01; // TMR0 in 16-bit mode with
TRISEbits.TRISE0=0;
internal clk, prescaler set to 4
while(1)
TMR0H =0x85; // values for 50ms
{
TMR0L =0xEE;
PORTEbits.RE0=~PORTEbits.RE0;
T0CONbits.TMR0ON=1; //turn on timer0
delayTImer0();
while(INTCONbits.TMR0IF==0); // wait until TMR0 overflows
}
T0CONbits.TMR0ON=1; //turn on timer0
}
INTCONbits.TMR0IF=0
}
TIMER0 as COUNTER
TRICK : For ease of calculation try to avoid using prescaler when using timer as a counter……
Problem: TOCKI/RA4 is connected with a weight sensor which gives us 10 pulses whenever weight
>=5kg is applied to it . We want to toggle a LED connected to RD0 whenever 5kg above weight is
applied on it?
SOLUTION:
void main( )
{
TRISDbits.TRISD0=0;
TRISAbits.TRISA4=1; // make RA4 as input
T0CON = 0x68; // TMR0 as counter in 8-bit mode with external clk@RA4, no prescaler
while(1)
{
TMR0L =246;
T0CONbits.TMR0ON=1; //turn on timer0
while(INTCONbits.TMR0IF==0); // wait until TMR0 overflows
T0CONbits.TMR0ON=1; //turn on timer0
INTCONbits.TMR0IF=0
PORTDbits.RD0=~PORTDbits.RD0;
}
}
Another way of writing code for previous scenario
TOCKI/RA4 is connected with a weight sensor which gives us 10 pulses whenever weight >=5kg is
applied to it . We want to toggle a LED connected to RD0 whenever 5kg above weight is applied on it?
SOLUTION:
void main( )
void count10pulses( )
{
{
TRISDbits.TRISD0=0;
T0CON = 0x68; // TMR0 as counter in 8-bit mode with
while(1)
external clk@RA4, with no prescaler
{
TMR0L =246;
PORTDbits.RD0=~PORTDbits.RD0;
T0CONbits.TMR0ON=1; //turn on timer0
count10pulses( )
while(INTCONbits.TMR0IF==0); // wait until TMR0 overflows
}
T0CONbits.TMR0ON=1; //turn on timer0
}
INTCONbits.TMR0IF=0
}
TIMER1
TIMER 1 Timer1 can operate as a timer or as a
counter.
The Timer1 module has the following features: 1. When the clock source is the instruction
1. 16-bit timer/counter cycle clock(external oscillator), it
operates as a timer.
2. Readable and writable 2. When the clock source is the
RC0/T1OSO/T13CKI pin, it operates a
3. Internal or external clock select counter
4. Interrupt-on-overflow from FFFFh to 0000h
5. RESET from CCP module special event trigger
Registers of Timer1
1. T1CON (Timer1 control register)
2. TMR1L (Timer1 low byte)
3. TMR1H (Timer1 high byte)
INSIDE T1CON REGISTER
Step 2:
Value= 65536 – n
yyxx= binary of “Value”
TMR1L=xx
TMR1H=yy
Load
Load T1CON reg Wait till TMR1IF
TMR1H/TMR1L
according to Start timer using flag becomes
according to
required settings TMR1ON bit high
required delay or
# of counts
void main( )
void delayTimer1( )
{
{
ADCON1 = 0x07;
T1CON = 0x00; // TMR1 in 16-bit mode with
TRISBbits.TRISB0=0;
internal clk, no prescaler
while(1)
TMR1H =0xFE; // values for 200us (50% duty cycle, f=2500hz)
{
TMR1L =0x0C;
PORTBbits.RB0=~PORTBbits.RB0;
T1CONbits.TMR1ON=1; //turn on timer1
delayTImer1();
while(PIR1bits.TMR1IF==0); // wait until TMR1 overflows
}
T1CONbits.TMR1ON=0; //turn off timer1
}
PIR1bits.TMR1IF=0
}
TIMER1 as COUNTER
TRICK : For ease of calculation try to avoid using prescaler when using timer as a counter……
Problem: RC0/T1OSO/T13CKI is connected with a weight sensor which gives us 10 pulses whenever
weight >=5kg is applied to it . We want to toggle a LED connected to RE0 whenever 5kg above weight
is applied on it?
SOLUTION:
void main( )
{
ADCON1=0x07;
TRISEbits.TRISE0=0;
TRISCbits.TRISC0=1; // make RA4 as input
T1CON = 0x02; // TMR1 as counter in 16-bit mode with external clk@RC0, no prescaler
while(1)
{
TMR1H =0xFF; // 65526 = 0xFFF6 moved so that they overflow after 10 counts
TMR1L =0xF6;
T1CONbits.TMR1ON=1; //turn on timer0
while(PIR1bits.TMR1IF==0); // wait until TMR1 overflows
T1CONbits.TMR1ON=0; //turn off timer1
PIR1bits.TMR1IF=0
PORTEbits.RE0=~PORTEbits.RE0;
}
For practice do examples from Mazidi’s book 9.28 – 9.37
TIMER2
TIMER3