View previous topic :: View next topic |
Author |
Message |
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
Odd problem with int_ext1 and int_timer1 cross triggering |
Posted: Tue Sep 16, 2008 3:23 am |
|
|
Hi all,
I'm using compiler version 3.249 for a PIC18f6723, this part is not supported normally so I have modified the header file for the PIC18f6722 to account for the minor changes between the two parts. I have been having no problems until today...
I have written a bit of code that uses timer 1 to control the operation of a stepper motor. This code is activated using:
Code: |
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(65535);
disable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
clear_interrupt(INT_TIMER1);
enable_interrupts(GLOBAL);
|
This is done in the int_ext1 interrupt by a state machine depending on what should happen next. Both these routines run fine independently. I can control the stepper and move through my state machine when the int_ext1 is triggered.
However when I enable timer1 int from the from the int_ext1 state machine the int_ext1 interrupt is then triggered repeatedly and rapidly like it is being triggered by the int_timer1.
I have checked the int_ext1 pin and there is no noise or anything that I would expect to cause these incorrect triggers. Does anyone know a reason why they would affect each other in this way? Am I incorrectly enabling the interrupt or should I not enable one interrupt inside another?
thanks for your time
ed |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
fixed? |
Posted: Tue Sep 16, 2008 4:19 am |
|
|
Hi all,
I seem to have fixed my problem after a bit of thinking. I decided that enabling/disabling interrupts inside an interrupt might be a bit dodgy. I still need to do it tho but I removed the enabling and disabling of the global interrupt and reordered the enable/clear of the int_timer1.
This seems to work. If anyone can shed any light on the mechanics of how this works that would be great.
cheers
ed
Code: |
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(65535);
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Sep 16, 2008 5:05 am |
|
|
Enabling / disabling interrupts within an interrupt handler is no problem but _never_ enable the GLOBAL interrupt inside an interrupt handler.
The PIC hardware protects against recursive interrupt calls by disabling the global interrupt flag at the start of an interrupt and enabling it again at the end of the interrupt. If you manually set the global interrupt flag while inside an interrupt you override this mechanism and recursive interrupt calls will create a mess. |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
cheers |
Posted: Tue Sep 16, 2008 5:20 am |
|
|
cheers, ckielstra that's a good answer... I normally disable and renable the global interrupt as a matter of course when adding or removing an individual interrupt from the mix. For some reason I have it in my head that this is good practice. Do you think it is worth doing or am I wasting my time?
cheers
ed |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: cheers |
Posted: Tue Sep 16, 2008 5:35 am |
|
|
EdWaugh wrote: | cheers, ckielstra that's a good answer... I normally disable and renable the global interrupt as a matter of course when adding or removing an individual interrupt from the mix. For some reason I have it in my head that this is good practice. Do you think it is worth doing or am I wasting my time?
|
It is worse than a waste of time. It is downright destructive to the proper functioning of your code, as you already discovered. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Tue Sep 16, 2008 5:41 am |
|
|
That's strange, I definitely read somewhere that is was good. Can you get spurious interrupts without disabling the global? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Sep 16, 2008 1:25 pm |
|
|
Enabling the global interrupt flag within an interrupt handler is a big 'don't do that'. Outside the interrupt handler it is another story, you can enable and disable the global interrupt as often as you want. It will create some extra overhead but that's it.
I don't know about the 'spurious interrupts'. There might have been processors with problems in the past, but nothing I can remember. The overhead created by disabling/enabling the global interrupt is very small, so if it makes you feel better don't change it. |
|
|
|