How does the timer0 interrupt work on PIC16f876a

Status
Not open for further replies.

BD

Member level 1
Joined
Oct 14, 2004
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
321
I am wondering how to get the Timer0 interrupt working on the PIC 16f876a.

this routine work the timer but not using the interrupt.
what should i do to get the interrupt working

/****************************************************/
/* Example code for using timer0 on a 16f876a
/* Just sets up an timer and toggles a port
/****************************************************/

/******************************************************/
/* Calculate preload value for timer
/******************************************************/

#include <pic.h>
#include <pic168xa.h>
#define PERIOD 10 // period in uS - one second here
#define XTAL 16000000 // crystal frequency - 16MHz
#define ICLK (XTAL/4) // crystal is divided by four
#define SCALE 2 // prescale by 2 - check for overflow!

#define PRELOAD PERIOD*ICLK/SCALE/1000000

unsigned int seconds; // second count


/*******************************************************/
/* service routine for timer 0 interrupt */
/*******************************************************/
void interrupt
timer0_isr(void)
{
// seconds++;
// PORTB = (PORTB&&0x01)? 0:1 ;// toggle port pin 0
// TMR0IE = 1; // set interrupt overflow flag
// TMRO = 1; // enable timer interrupt ??

}

main()
{
// initialize timer 0;
di(); // disable all interrupts
PORTB =0; // clear port b
TRISB =0xF7; // RB3 is an op
T0CS =0; //internal instruction cycle
PEIE =0;
PIR1 =0;
GIE =0;
PSA =1; //prescaler assigned to wdt to get a 1:1
TMR0IE = 0; // enable timer0 interrupt
ei(); // enble all interrupts
for(;
{
if (TMR0IF) //check flag
{
TMR0IF =0; // set interrupt overflow flag
TMR0 = 0xEF; //load timer with value
RB3 = (RB3&&0x01)? 0:1 ;// toggle port pin 0
}
continue;
} // let interrupt do its job
}
 

just incomment these :
timer0_isr(void)
{
seconds++;
PORTB = (PORTB&&0x01)? 0:1 ;// toggle port pin 0
TMR0IE = 1; // set interrupt overflow flag
TMRO = 1; // enable timer interrupt ??
}
and recompile
 

/****************************************************/
/* Example code for using timer0 on a 16f876a
/* Just sets up an timer and toggles a port
/****************************************************/

/******************************************************/
/* Calculate preload value for timer
/******************************************************/

#include <pic.h>
#include <pic168xa.h>
#define PERIOD 10 // period in uS - one second here
#define XTAL 16000000 // crystal frequency - 16MHz
#define ICLK (XTAL/4) // crystal is divided by four
#define SCALE 2 // prescale by 2 - check for overflow!

#define PRELOAD PERIOD*ICLK/SCALE/1000000

unsigned int seconds,some_usecond,value_for_one_second; // second count


/*******************************************************/
/* service routine for timer 0 interrupt */
/*******************************************************/
void interrupt
timer0_isr(void)
{

//if (TMR0IF) // if you use diferent int incomment these
//{
TMR0IF =0; // set interrupt overflow flag
TMR0 = 0xEF; //load timer with value (not sure, 239 check it)
some_usecond++;
//}


if(usecond==value_for_one_second){
useconds=0;
seconds++;

RB3 = (RB3&&0x01)? 0:1 ;// toggle port pin 0

}
}

main()
{

some_useconds=0;
seconds=0;
value_for_one_second=40;//not sure for value of 40
// initialize timer 0;
//di(); // disable all interrupts
PORTB =0; // clear port b
TRISB =0xF7; // RB3 is an op
T0CS =0; //internal instruction cycle
PEIE =0;
PIR1 =0;
GIE =1;
PSA =1; //prescaler assigned to wdt to get a 1:1
TMR0=0;

TMR0IE = 1; // enable timer0 interrupt

//ei(); // enble all interrupts

while(1){
}



{
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…