CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ISR always entered at startup

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
fvnktion



Joined: 27 Jun 2006
Posts: 39

View user's profile Send private message

ISR always entered at startup
PostPosted: Thu May 14, 2009 4:17 pm     Reply with quote

The following code is always entered at startup. I though it may because of the initialized value of f_int, but found that it enters whether it is a 1 or 0. Anything else i should be looking into?

Code:
#include <18F2520.h>
#fuses INTRC,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

#define LED pin_a2
#define DELAY_LED 100
#define DELAY_CMD 1
#define DELAY_LOOP 50

//global flag to be used
int F_INT = 1;

#int_ext1
void isr(){

delay_cycles(1);
delay_cycles(1);
if(F_INT ==0){
   F_INT = 1;
}
else if (F_INT == 1){
   F_INT = 0;
}
else {
   F_INT = 0;
}//IF


}

//function prototypes
void led_blink();


//__________________________________________
void main(){


ENABLE_INTERRUPTS(INT_EXT1);
EXT_INT_EDGE(H_TO_L );
ENABLE_INTERRUPTS(GLOBAL);


led_blink();


delay_cycles(1);

//________________________________________________
while(1){


delay_cycles(1);
//check interrupt flag here
if(F_INT == 1){
led_blink();
}


delay_ms(DELAY_LOOP);
}//while
}//main

//blink led function
//________________________________________________________________________
void led_blink(){
output_high(LED);
delay_ms(DELAY_LED);
output_low(LED);
delay_ms(DELAY_LED);
}//FUNC}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 14, 2009 4:28 pm     Reply with quote

What code is always entered at startup ? Do you mean the isr ?
If so, it's possible that the interrupt flag is already set. Clear it like
this, before you enable global interrupts:

Code:
clear_interrupt(INT_EXT1);
fvnktion



Joined: 27 Jun 2006
Posts: 39

View user's profile Send private message

PostPosted: Thu May 14, 2009 4:40 pm     Reply with quote

Thanks PCM. Yes the ISR is entered at startup. What you suggested did the trick. It seems strange that by default the interrupt would be set? Is this standard? I'll have a look at the literature.

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 14, 2009 4:46 pm     Reply with quote

It's best to clear the interrupt flag before you enable interrupts.
That way, the interrupts that you handle are ones that are expected,
and not some previous random interrupt that occurred before your
initialization was complete.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 14, 2009 5:08 pm     Reply with quote

I remember reading somewhere that changing the sensitive interrupt edge can trigger an interrupt. So make sure to clear the interrupt flag after calling ext_int_edge() and before enabling the interrupt, making the correct sequence:
Code:
EXT_INT_EDGE(H_TO_L );        // 1
CLEAR_INTERRUPT(INT_EXT1)     // 2
ENABLE_INTERRUPTS(INT_EXT1);  // 3
ENABLE_INTERRUPTS(GLOBAL);    // 4 
Where 2 and 3 can be exchanged.
Ttelmah
Guest







PostPosted: Fri May 15, 2009 2:17 am     Reply with quote

Ckielstra is spot on. It is in some of the data sheets, that changing the edge can trigger the interrupt. Also though, you have to remember that depending on how the supply actually sequences during power on, and what the interrupt actually comes from, spurious triggers during power on, are totally expected.
The chip itself, does not set the flag, unless a trigger is seen, but during the first few uSec, 'anything can happen'....

Best Wishes
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri May 15, 2009 8:00 am     Reply with quote

fvnktion wrote:
Thanks PCM. Yes the ISR is entered at startup. What you suggested did the trick. It seems strange that by default the interrupt would be set? Is this standard? I'll have a look at the literature.

Thanks.


It is good practice to clear the interrupt flag for any interrupt immediately before the interrupt is enabled for the first time.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
fvnktion



Joined: 27 Jun 2006
Posts: 39

View user's profile Send private message

PostPosted: Fri May 15, 2009 9:55 am     Reply with quote

Thanks for all the great input. Wish I could keep that dang data sheet memorized Smile.

Cheers
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group