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.

Give some suggesstion on background timer

Status
Not open for further replies.

Mani Yuvan

Member level 1
Joined
Jan 18, 2015
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Tamilnadu , India
Activity points
321
I successfully executed the external interrupt to PIC18f452. But i want certain time delay.Means initially the interrupt occurred it perform some actions then it wait for 30 mins after that only it again execute the interrupt routine how to i do it????
Plz give some suggesstion
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,275
Helped
4,970
Reputation
9,962
Reaction score
4,803
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
129,675
Use one of the timer interupts to count down a software variable at a regular rate. When you service the external interrupt, disable it then load the variable with a value suitable to create the delay you want. When the variable reaches zero the delay will be finished and you can re-enable the interupt again.

This method is reliable, gives accurate timing and during the period you are waiting, the processor can still do other things (it isn't a blocking routine).

Brian.
 

Mani Yuvan

Member level 1
Joined
Jan 18, 2015
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Tamilnadu , India
Activity points
321
Use one of the timer interupts to count down a software variable at a regular rate. When you service the external interrupt, disable it then load the variable with a value suitable to create the delay you want. When the variable reaches zero the delay will be finished and you can re-enable the interupt again.

This method is reliable, gives accurate timing and during the period you are waiting, the processor can still do other things (it isn't a blocking routine).

Brian.

Code:
#include<p18f452.h>
#define LED PORTBbits.RB7
void chk_isr(void);
void INT0_ISR(void);
void TO_ISR(void);
unsigned char EN=1,a=0;
#pragma interrupt chk_isr
void chk_isr(void)
{
if((EN&&INTCONbits.INT0IF))
INT0_ISR();
if(INTCONbits.TMR0IF==1)
TO_ISR();
}
#pragma code My_Hiprio_Int=0X08
void My_Hiprio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code

void TO_ISR(void)
{
if(a==20)
INTCONbits.INT0IE=1;
a++;
TMR0H=0;
TMR0L=0;
INTCONbits.TMR0IF=0;
}
void main(void)
{
TRISBbits.TRISB0=1;
TRISBbits.TRISB7=0;
T0CON=0X08;
TMR0H=0;
TMR0L=0;
INTCONbits.INT0IF=0;
INTCONbits.INT0IE=1;
INTCONbits.TMR0IF=0;
INTCONbits.TMR0IE=1;
T0CONbits.TMR0ON=0;
INTCONbits.GIE=1;
LED=0;
while(1)
{

}
}
void INT0_ISR(void)//Interrupt Service Routine
{
LED=1;
INTCONbits.INT0IF=0;
EN=0;
T0CONbits.TMR0ON=1;
INTCONbits.INT0IE=0;
}
I tried for your istruction when external interrupt occur i ON the timer0 and particular interval and again enable the interupt but nothing to happened. what to do??
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Try attached mikroC PRO PIC 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
char halfSecond = 0, seconds = 0, minutes = 0, hours = 0;
 
sbit Trigger at RB0_bit;
sbit Trigger_Direction at TRISB0_bit;
 
sbit Ext_Int_LED at LATD0_bit;
sbit Ext_Int_LED_Direction at TRISD0_bit;
 
sbit Timer_LED at LATD1_bit;
sbit Timer_LED_Direction at TRISD1_bit;
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms
 
//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x31;
    TMR1IF_bit = 0;
    TMR1H = 0x0B;
    TMR1L = 0xDC;
    TMR1IE_bit = 1;    
}
 
void interrupt() {
 
    if(INT0IF_bit) {
         INT0IF_bit = 0;
         Ext_Int_LED = 1;
         Timer_LED = 0;
         InitTimer1();
         INT0IE_bit = 0;   
    }
    
    if (TMR1IF_bit) { 
        TMR1IF_bit = 0;
        TMR1H = 0x0B;
        TMR1L = 0xDC;
        //Enter your code here
        if(++halfSecond == 2) {
               if(++seconds == 60) {
                   if(++minutes == 30) {
                        Timer_LED = 1;
                        Ext_Int_LED = 0;
                        TMR1ON_bit = 0;                        
                        INT1IE_bit = 1;                   
                        minutes = 0;                   
                   }               
                   seconds = 0;         
               }
               halfSecond = 0;        
        }
    }
}
 
void main() {
 
     ADCON1 = 0x87;
     
     TRISA = 0xC0;
     TRISB = 0x00;
     TRISC = 0x00;
     TRISD = 0x00;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     
     LATA = 0x00;
     LATB = 0x00;
     LATC = 0x00;
     LATD = 0x00;
     
     Trigger_Direction = 1;
     Trigger = 0;
     
     Ext_Int_LED_Direction = 0;
     Ext_Int_LED = 0;
     
     Timer_LED_Direction = 0;
     Timer_LED = 0;     
 
     INTEDG0_bit = 1;
     INT0IF_bit = 0;
     INT0IE_bit = 1;
     INTCON |= 0xC0;
     
     while(1) {    
     
     }
}

 

Attachments

  • PIC18F452 Ext Int + Timer.rar
    64.4 KB · Views: 31
  • ext_int_timer1.png
    ext_int_timer1.png
    29.1 KB · Views: 51

Mani Yuvan

Member level 1
Joined
Jan 18, 2015
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Tamilnadu , India
Activity points
321
Try attached mikroC PRO PIC 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
char halfSecond = 0, seconds = 0, minutes = 0, hours = 0;
 
sbit Trigger at RB0_bit;
sbit Trigger_Direction at TRISB0_bit;
 
sbit Ext_Int_LED at LATD0_bit;
sbit Ext_Int_LED_Direction at TRISD0_bit;
 
sbit Timer_LED at LATD1_bit;
sbit Timer_LED_Direction at TRISD1_bit;
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms
 
//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x31;
    TMR1IF_bit = 0;
    TMR1H = 0x0B;
    TMR1L = 0xDC;
    TMR1IE_bit = 1;    
}
 
void interrupt() {
 
    if(INT0IF_bit) {
         INT0IF_bit = 0;
         Ext_Int_LED = 1;
         Timer_LED = 0;
         InitTimer1();
         INT0IE_bit = 0;   
    }
    
    if (TMR1IF_bit) { 
        TMR1IF_bit = 0;
        TMR1H = 0x0B;
        TMR1L = 0xDC;
        //Enter your code here
        if(++halfSecond == 2) {
               if(++seconds == 60) {
                   if(++minutes == 30) {
                        Timer_LED = 1;
                        Ext_Int_LED = 0;
                        TMR1ON_bit = 0;                        
                        INT1IE_bit = 1;                   
                        minutes = 0;                   
                   }               
                   seconds = 0;         
               }
               halfSecond = 0;        
        }
    }
}
 
void main() {
 
     ADCON1 = 0x87;
     
     TRISA = 0xC0;
     TRISB = 0x00;
     TRISC = 0x00;
     TRISD = 0x00;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     
     LATA = 0x00;
     LATB = 0x00;
     LATC = 0x00;
     LATD = 0x00;
     
     Trigger_Direction = 1;
     Trigger = 0;
     
     Ext_Int_LED_Direction = 0;
     Ext_Int_LED = 0;
     
     Timer_LED_Direction = 0;
     Timer_LED = 0;     
 
     INTEDG0_bit = 1;
     INT0IF_bit = 0;
     INT0IE_bit = 1;
     INTCON |= 0xC0;
     
     while(1) {    
     
     }
}

hello milan i try your code with slight modification but nothing to happened external interrupt doest not stop for some delay. i posted the slight modification code here. what to do why the external interrupt behave like this as saw the code when interrupt occur it disable the external interrupt and on the timer and inside the timer routine enable the external interrupt but running proteus its not working like that coding ....
Code:
 if(INT0IF_bit) {
         INT0IF_bit = 0;
         Ext_Int_LED = 1;
         Timer_LED = 0;
         InitTimer1();
         INT0IE_bit =0;
          Delay_ms(2000);
          Ext_Int_LED = 0;

    }
and in your code inside the timer routine u mentioned INT1IE_bit = 1; why we put this ?i changed INT0IE_bit = 1;
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Don't use delays inside ISR. I used INT0IE_bit = 1 in

Code C - [expand]
1
if(++minutes == 30) {

because INT0 was disabled when it was detected and so after 30 minutes it has to be enabled so that you can use it again.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top