BD
Member level 1
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
}
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
}