Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Multiple Timer Interrupt Problem

Status
Not open for further replies.

saneesh george

New Member
Haii all, I have a problem related with Microc PIC IDE (v8.2)
I have 2 timer interrupts(tmr2,tmr1) in my program..like this..
void interrupt(){
if (PIR1.TMR1IF) {
PIR1.TMR1IF=0;
cnt++ ;
TMR1H = 0x80;
TMR1L = 0x00;
}
else if(PIR1.TMR2IF)
{
PIR1.TMR2IF=0;
cnt2++ ;
TMR2 = 0;
} }
But when Timer 2 Enables (PIE1.TMR2IE = 1) ,both of two interrupts(cnt and cnt2) get incremented ...why ?/

MY WHOLE PROGRAM IS THE BELOW ONE


unsigned short cnt;
unsigned short cnt2;


void interrupt(){

if (PIR1.TMR1IF) {

PIR1.TMR1IF=0;
cnt++ ;
TMR1H = 0x80;
TMR1L = 0x00;

}

else if(PIR1.TMR2IF)

{
PIR1.TMR2IF=0;
cnt2++ ;
TMR2 = 0;

}

}

void main()
{
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
TRISB = 0xFF; // set PORTB to be input
TRISE = 0;
TRISD = 0; // set PORTD to be output
PORTD = 0x00;
PORTE=0X00; // initialize PORTD


T1CON = 1;
T2CON = 0xFF; // Timer2 settings
TMR2 = 0; // Initialize Timer2 register
TMR1H = 0x80; // Initialize Timer1 register
TMR1L = 0x00;
INTCON = 0xC0;
PIE1.TMR2IE = 1;
PIE1.TMR1IE = 0;
PIR1.TMR1IF=0;
cnt=0;
cnt2 = 0;
PIR1.TMR2IF=0;

for(;;)
{

if(cnt==61)

{
PORTE=~PORTE;
cnt=0;
}

else if (cnt2==160)
{
PIE1.TMR2IE = 0;
PIE1.TMR1IE = 1;
cnt2=0;
cnt=0;

PORTD=~PORTD;
}

}
}
 
Haii all, I have a problem related with Microc PIC IDE (v8.2)
I have 2 timer interrupts(tmr2,tmr1) in my program..like this..
void interrupt(){
if (PIR1.TMR1IF) {
PIR1.TMR1IF=0;
cnt++ ;
TMR1H = 0x80;
TMR1L = 0x00;
}
else if(PIR1.TMR2IF)
{
PIR1.TMR2IF=0;
cnt2++ ;
TMR2 = 0;
} }
But when Timer 2 Enables (PIE1.TMR2IE = 1) ,both of two interrupts(cnt and cnt2) get incremented ...why ?/

MY WHOLE PROGRAM IS THE BELOW ONE


unsigned short cnt;
unsigned short cnt2;


void interrupt(){

if (PIR1.TMR1IF) {

PIR1.TMR1IF=0;
cnt++ ;
TMR1H = 0x80;
TMR1L = 0x00;

}

else if(PIR1.TMR2IF)

{
PIR1.TMR2IF=0;
cnt2++ ;
TMR2 = 0;

}

}

void main()
{
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
TRISB = 0xFF; // set PORTB to be input
TRISE = 0;
TRISD = 0; // set PORTD to be output
PORTD = 0x00;
PORTE=0X00; // initialize PORTD


T1CON = 1;
T2CON = 0xFF; // Timer2 settings
TMR2 = 0; // Initialize Timer2 register
TMR1H = 0x80; // Initialize Timer1 register
TMR1L = 0x00;
INTCON = 0xC0;
PIE1.TMR2IE = 1;
PIE1.TMR1IE = 0;
PIR1.TMR1IF=0;
cnt=0;
cnt2 = 0;
PIR1.TMR2IF=0;

for(;;)
{

if(cnt==61)

{
PORTE=~PORTE;
cnt=0;
}

else if (cnt2==160)
{
PIE1.TMR2IE = 0;
PIE1.TMR1IE = 1;
cnt2=0;
cnt=0;

PORTD=~PORTD;
}

}
}
 
The reason they count together is because they are both running!!!! When timer 2's flag is set ( when it gets to 255)
Timer 1's flag is also set because timer 1 has also overflowed... Instead of setting the interrupt... Keep them both on and turn the timer on and off instead.... If you just clear both flags together at the end of the interrupt it will be a workround..

But then your code is wrong anyway!!! In the forever loop you count until 61, but don't enable the timer so cnt2 never get incremented...
 
I would replace the "else if" with "if" in the ISR as it will cause two interrupts to fire if the two timers roll over at the same time.

I.E.
Code:
void interrupt(){
   if (PIR1.TMR1IF) {
      PIR1.TMR1IF=0;
      cnt++ ;
      TMR1H = 0x80;
      TMR1L = 0x00;
     }
   if (PIR1.TMR2IF){
      PIR1.TMR2IF=0;
      cnt2++ ;
      TMR2 = 0;
     }
}

Edit, With Ian's code an interrupt will be missed if the two timers roll over at the same time.
Ian Rogers , how do you get the colours in the code box?

Mike.
 
Pommie
Hi mike.... First!! The timers will be on at two different times so It shouldn't need any more than what I have done..

Secondly I''ll do it as a picture as showing it doesn't work..

C:
#define somethingelse 12
char * variable;
void main()
   {
   doSomething();
   }
 

Attachments

  • code.bmp
    89.7 KB · Views: 287
Thank you very much for your consideration and time Ian Rogers and pommie....I modified program,with
T1CON.TMR1ON
T2CON.TMR2ON settings
Now it is working in IF statement and also in Ifelse on interrupt loop.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top