Problem with TIMER0 & INT2 in 18F452

Status
Not open for further replies.

garagedog

Newbie level 5
Joined
Jan 6, 2014
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
88
Hi,

I'm currently trying to use timer0 and interrupts at the same time, what I basically needed was when I press the push button attached to INT2 pin of 18F452 and light an LED connected to DO pin for 1 second, I can do this but when I try to make TMR0IE_bit = 1; the LED keeps lit all the time. Here is my 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
void interrupt(){
 
     if(INTCON3.INT2IF = 1)
     {
          PORTD.LATD0 = 1;
          Delay_ms(1000);
          PORTD.LATD0 = 0;
          INTCON3.INT2IF = 0;
     }
     
}
void main() {
 
 
     T0CON         = 0x82;
     TMR0H         = 0x0B;
     TMR0L         = 0xDC;
     GIE_bit       = 1;
     PEIE_bit      = 1;
     //TMR0IE_bit    = 1; //when I uncomment this RD0 keeps high all the time
     INT2IE_bit = 1;
     TMR0IP_bit = 0;
 
     TRISD.LATD0 = 0;
}


It will be a great help if anybody could tell me how to get this right.

Thanks.
 
Last edited by a moderator:

This wont help?

if(INTCON3.INT2IF = 1) ???

if(INTCON3.INT2IF == 1)

You might also need a loop at the end of main to stop the program from resetting?

while(1){}
 
Try this 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
sbit LED at LATD0_bit;
 
unsigned char time = 0;
 
//Timer0
//Prescaler 1:8; TMR0 Preload = 3035; Actual Interrupt Time : 500 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  T0CON        = 0x82;
  TMR0H        = 0x0B;
  TMR0L        = 0xDB;
}
 
void Interrupt(){
      if (INT2IF_bit) {
            //Enter your code here
            LED = 1;
            TMR0H = 0x0B;
            TMR0L = 0xDB;
            TMR0IE_bit = 1;
            INT2IF_bit = 0;
      }
 
      if (TMR0IF_bit) {
            //Enter your code here
            if(++time >= 2) {       // 2 interrupts = 1000 ms
                   LED = 0;
                   TMR0IE_bit = 0;
                   time = 0;
            }
 
            TMR0IF_bit = 0;
            TMR0H = 0x0B;
            TMR0L = 0xDB;
      }
}
 
 
void main() {
 
     TRISA = 0xFF;
     PORTA = 0x00;
     TRISB = 0x04;
     PORTB = 0x00;
     LATB = 0x00;
     TRISD = 0x00;
     PORTD = 0x00;
     LATD = 0x00;
 
     InitTimer0();
     INTCON = 0xC0;
     INTCON2 = 0x48;
 
     while(1) {
 
            ;
     }
}

 

Attachments

  • 18F452 INT2 and TMR0.rar
    33.8 KB · Views: 107
Last edited:

Hi what I now want is to get rid of the 1000ms delay so I tried triggering timer 1 at the at occurrence of interrupt INT2 as follows, (timer 1) is set to 100ms and using mikro c code calculator utility.

Code:
unsigned int CNT;

void InitTimer1(){
  T1CON	 = 0x31;
  TMR1IF_bit	 = 0;
  TMR1H	 = 0x0B;
  TMR1L	 = 0xDC;
  TMR1IE_bit	 = 1;
  INTCON	 = 0xC0;
}

void interrupt(){


     if (TMR1IF_bit){
          TMR1IF_bit = 0;
          TMR1H	 = 0x0B;
          TMR1L	 = 0xDC;
          CNT++;

    }
    
          if(INTCON3.INT2IF == 1)
     {
          PORTD.LATD0 = 1;
          INTCON3.INT2IF = 0; //Clear INT2 Flag
          TMR1IF_bit = 1;//
          //PORTD.LATD0 = 0;

     }
}

void main() {

     InitTimer1();
     INT2IE_bit = 1;
     TMR0IP_bit = 0;

     TRISD.LATD0 = 0;
     
     do{
     

          if(CNT>=10){
          BKLIGHT=0;
          PORTD.LATD0 = 0;
          }
          
     }while(1);
}

But the problem is I cant get this work, when I press the button LED doesn't go off. If you could let me know what I'm doing wrong in this code, it will be a big help.
Thanks
 
Last edited:

Not tested 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
unsigned char CNT = 0;
 
void InitPorts(){
     TRISA = 0xFF;
     TRISB = 0x04;
     TRISD = 0x00;
     PORTA = 0x00;
     PORTB = 0x00;
     PORTD = 0x00;
}
 
void InitTimer1(){
    T1CON = 0x31;
    TMR1IF_bit = 0;
    TMR1H = 0x0B;
    TMR1L = 0xDC;
    INTCON = 0xC0;
}
 
void InitINT2(){
    INTCON = 0xC0;
    INTCON2 = 0x90;
    INTCON3 = 0x90; 
}
 
void interrupt(){
 
     if (TMR1IF_bit){
      if(++CNT == 10) {
        LAD0_bit = 0;
        TMR1IE_bit = 0;
        CNT = 0;
      }
 
          TMR1IF_bit = 0;
          TMR1H = 0x0B;
          TMR1L = 0xDC;
     }
     
     if(INT2IF_bit == 1)
     {
          LATD0_bit = 1;
      TMR1IE_bit = 1;
      TMR1H = 0x0B;
          TMR1L = 0xDC
          INT2IF_bit = 0;
          TMR1IF_bit = 0;
     }
}
 
void main() {
 
     InitPorts();
     InitINT2();     
     InitTimer1();     
     
     do{
         ;
         
     } 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…