PIC16F877A-HITECH C– TIMER 0,1,2

Photo of author

By Jackson Taylor

TIMER 0 interrupt
Here i am blinking PORTB using timer interrupt
Basic Steps
1)Enable global interrupt (GIE)
2)EnableTIMER0 interrupt (TMR0IE)
3)select internal clock source (TOCS)
4)select prescaler in timer mode (PSA)
5)Select prescaler value if nedded (PS2, PS1, PS0)
6)Load the desired timer value to TMRO
7)clear the tmer flag in interrupt service routine
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1)
{
}                                                                
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
 TMR0IF=0;
 }
Timer0 as Counter
External clock is at RA4 pin
when 2 external pulses are given  to RA4 pin, PORTB  will Negate
please pull down the RA4 pin
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
TMR0IE=1;
T0CS=1; // not needed this step (this register normally is at 1)
TRISB=0;
PORTB=0;  
TMR0 = 0xfe;
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR0IF=0;
TMR0 = 0xfe;
}
TIMER 1 INTERRUPT
PORT B WILL BLINK ON TIMER INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfff;
TMR1CS=0;
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
TMR1IF=0;
}
TIMER 1 AS COUNTER
clock is given at the pin RC0
HERE COUNT = 4
PLEASE PULL DOWN
PORTB WILL NEGATE
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfffC;
TMR1CS=1;
TRISB=0;
PORTB=0;  
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR1IF=0;
TMR1=0xfffc;
}
1 SECOND TIMER using timer1 (16 bit)
clock frequency = 16MHz
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1CS=0;
TMR1 = 0XFFFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR1IF=0;
 c=c+1;
if(c==61)
{
PORTB= ~PORTB;
c=0;
}
}
1 SECOND TIMER using timer0 (8 bit)
Note
I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ”0” only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
TIMER 2 INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

Here i am blinking PORTB using timer interrupt
Basic Steps
1)Enable global interrupt (GIE)
2)EnableTIMER0 interrupt (TMR0IE)
3)select internal clock source (TOCS)
4)select prescaler in timer mode (PSA)
5)Select prescaler value if nedded (PS2, PS1, PS0)
6)Load the desired timer value to TMRO
7)clear the tmer flag in interrupt service routine
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1)
{
}                                                                
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
 TMR0IF=0;
 }
Timer0 as Counter
External clock is at RA4 pin
when 2 external pulses are given  to RA4 pin, PORTB  will Negate
please pull down the RA4 pin
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
TMR0IE=1;
T0CS=1; // not needed this step (this register normally is at 1)
TRISB=0;
PORTB=0;  
TMR0 = 0xfe;
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR0IF=0;
TMR0 = 0xfe;
}
TIMER 1 INTERRUPT
PORT B WILL BLINK ON TIMER INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfff;
TMR1CS=0;
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
TMR1IF=0;
}
TIMER 1 AS COUNTER
clock is given at the pin RC0
HERE COUNT = 4
PLEASE PULL DOWN
PORTB WILL NEGATE
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfffC;
TMR1CS=1;
TRISB=0;
PORTB=0;  
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR1IF=0;
TMR1=0xfffc;
}
1 SECOND TIMER using timer1 (16 bit)
clock frequency = 16MHz
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1CS=0;
TMR1 = 0XFFFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR1IF=0;
 c=c+1;
if(c==61)
{
PORTB= ~PORTB;
c=0;
}
}
1 SECOND TIMER using timer0 (8 bit)
Note
I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ”0” only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
TIMER 2 INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

when 2 external pulses are given  to RA4 pin, PORTB  will Negate
please pull down the RA4 pin
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
TMR0IE=1;
T0CS=1; // not needed this step (this register normally is at 1)
TRISB=0;
PORTB=0;  
TMR0 = 0xfe;
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR0IF=0;
TMR0 = 0xfe;
}
TIMER 1 INTERRUPT
PORT B WILL BLINK ON TIMER INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfff;
TMR1CS=0;
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
TMR1IF=0;
}
TIMER 1 AS COUNTER
clock is given at the pin RC0
HERE COUNT = 4
PLEASE PULL DOWN
PORTB WILL NEGATE
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfffC;
TMR1CS=1;
TRISB=0;
PORTB=0;  
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR1IF=0;
TMR1=0xfffc;
}
1 SECOND TIMER using timer1 (16 bit)
clock frequency = 16MHz
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1CS=0;
TMR1 = 0XFFFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR1IF=0;
 c=c+1;
if(c==61)
{
PORTB= ~PORTB;
c=0;
}
}
1 SECOND TIMER using timer0 (8 bit)
Note
I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ”0” only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
TIMER 2 INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

PORT B WILL BLINK ON TIMER INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfff;
TMR1CS=0;
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
TMR1IF=0;
}
TIMER 1 AS COUNTER
clock is given at the pin RC0
HERE COUNT = 4
PLEASE PULL DOWN
PORTB WILL NEGATE
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfffC;
TMR1CS=1;
TRISB=0;
PORTB=0;  
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR1IF=0;
TMR1=0xfffc;
}
1 SECOND TIMER using timer1 (16 bit)
clock frequency = 16MHz
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1CS=0;
TMR1 = 0XFFFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR1IF=0;
 c=c+1;
if(c==61)
{
PORTB= ~PORTB;
c=0;
}
}
1 SECOND TIMER using timer0 (8 bit)
Note
I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ”0” only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
TIMER 2 INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

clock is given at the pin RC0
HERE COUNT = 4
PLEASE PULL DOWN
PORTB WILL NEGATE
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfffC;
TMR1CS=1;
TRISB=0;
PORTB=0;  
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR1IF=0;
TMR1=0xfffc;
}
1 SECOND TIMER using timer1 (16 bit)
clock frequency = 16MHz
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1CS=0;
TMR1 = 0XFFFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR1IF=0;
 c=c+1;
if(c==61)
{
PORTB= ~PORTB;
c=0;
}
}
1 SECOND TIMER using timer0 (8 bit)
Note
I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ”0” only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
TIMER 2 INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

clock frequency = 16MHz
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1CS=0;
TMR1 = 0XFFFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR1IF=0;
 c=c+1;
if(c==61)
{
PORTB= ~PORTB;
c=0;
}
}
1 SECOND TIMER using timer0 (8 bit)
Note
I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ”0” only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
TIMER 2 INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ”0” only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
TIMER 2 INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
1 SECOND TIMER using timer2 (8 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}

calculated frequency = 7843.1372549019607843137254901961
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}