View previous topic :: View next topic |
Author |
Message |
fvnktion
Joined: 27 Jun 2006 Posts: 39
|
ISR always entered at startup |
Posted: Thu May 14, 2009 4:17 pm |
|
|
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
|
|
Posted: Thu May 14, 2009 4:28 pm |
|
|
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
|
|
Posted: Thu May 14, 2009 4:40 pm |
|
|
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
|
|
Posted: Thu May 14, 2009 4:46 pm |
|
|
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
|
|
Posted: Thu May 14, 2009 5:08 pm |
|
|
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
|
|
Posted: Fri May 15, 2009 2:17 am |
|
|
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: 1634 Location: Perth, Australia
|
|
Posted: Fri May 15, 2009 8:00 am |
|
|
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
|
|
Posted: Fri May 15, 2009 9:55 am |
|
|
Thanks for all the great input. Wish I could keep that dang data sheet memorized .
Cheers |
|
|
|