View previous topic :: View next topic |
Author |
Message |
weg22
Joined: 08 Jul 2005 Posts: 91
|
help with interrupts...please |
Posted: Tue Nov 29, 2005 12:38 pm |
|
|
Hi all,
I'm having trouble turning off interrupts and then re-enabling them. I'm hoping someone can help me out with this? I have two interrupts, one software (timer0) and one hardware (ccp1). What I am trying to do is:
1. if condition 1 is TRUE, then enable hardware interrupt (ccp1) and disable the software interrupt (timer0)
2. else (condition 2): initially I need to enable the software interrupt and disable the hardware interrupt. Then, before exitiing (i.e. after the code inside the loop is executed), I need to enable all interrupts.
See below for non-working code :-(
Thanks in advance!
Code: |
main()
{
// declare variables here...
// setup hardware interrupt and timer1
setup_ccp1(CCP_CAPTURE_RE); // on rising edge
setup_timer_1(T1_INTERNAL);
// enable hardware interrupts
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(1)
{
if(condition1) // pulseLength/(clock/4) is time in us
{
disable_interrupts(INT_TIMER0);
// more code...
}
else // condition 2
{
disable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
disable_interrupts(INT_CCP1);
// more code...
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
}
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 29, 2005 1:09 pm |
|
|
I'm not sure what you're trying to do. But it's possible that you may
have a problem because you don't know that the interrupt flags for the
CCP1 and the Timer0 can be set, regardless of whether the individual or
global interrupts are enabled/disabled. In other words, the interrupt
flag for a peripheral can be set, due to some previous Timer rollover
or CCP event, and be sitting there pendin. As soon as you enable the
peripheral interrupt and the global, then you'll immediately get tossed
into the isr code. This may not be what you want.
CCS has a function called clear_interrupt(), which allows you to clear
the peripheral's interrupt flag before you enable its interrupt. That way,
you're starting fresh. Whatever interrupts that occur after you enable
interrupts will be new ones. |
|
|
weg22
Joined: 08 Jul 2005 Posts: 91
|
|
Posted: Tue Nov 29, 2005 1:16 pm |
|
|
I have an older version and thus do not have the luxury of using the clear_interrupt function :-( All I'm trying to do is: (a) enable a hardware interrupt and disable a software interrupt during one loop and (b) vice versa for the other loop.
It is working now, but is acting a little flaky and its probably due to my inefficient coding for interrupts. I'm sure it can be fixed without the clear_interrupt function. Please advise...
Thanks |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Tue Nov 29, 2005 2:01 pm |
|
|
Code: |
// point to Interrupt Control Register fir this PIC 18F....
#byte INTCON = 0x0FF2
// point to Timer 0 Interrupt flag
#bit TMR0IF = INTCON.2
// point to CCP Interrupt flag
....
//......
else // condition 2
{
disable_interrupts(GLOBAL);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
// clear stale timer interrupt flag
TMR0IF = 0;
enable_interrupts(INT_TIMER0);
disable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
// more code...
|
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
|