Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Multiple Timer interrupt problem

Status
Not open for further replies.

sangeo

Member level 2
Joined
Jun 2, 2013
Messages
44
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,647
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..


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
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 in main program (PIE1.TMR2IE = 1) ,both of two interrupts(cnt and cnt2) get incremented ...why ?/
Initialization of Registers are like this..


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
T1CON = 1;
  T2CON = 0xFF;               // Timer2 settings
  TMR2  =   0;  
TMR1H = 0x80   TMR1L = 0x00;
  INTCON = 0xC0;
  PIE1.TMR2IE = 0;
  PIE1.TMR1IE  = 0;
   PIR1.TMR1IF=0;
PIR1.TMR2IF=0;
cnt=0;
  cnt2 = 0;

 
Last edited by a moderator:

Post the whole source code
 

Post the whole source code

This is the whole source code



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
    }
     
 }
}

 
Last edited by a moderator:

Flags are set without concerning interrupt enables (PIE1.TMR2IE = 0;)
Please post your code under CODE tags
 
Last edited:
  • Like
Reactions: sangeo

    sangeo

    Points: 2
    Helpful Answer Positive Rating
Most likely the other interrupt is pending and actioned as soon as the first one finishes. Try removing the 'else if' and replacing it with 'if' so that both can be processed in the same pass of the ISR. At the moment, both interrupts can occur but you exclude processing TMR2 if TMR1 is also active.

Brian.
 
  • Like
Reactions: sangeo

    sangeo

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top