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 support@ccsinfo.com

help with interrupts...please

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



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

help with interrupts...please
PostPosted: Tue Nov 29, 2005 12:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 29, 2005 1:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 29, 2005 1:16 pm     Reply with quote

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

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

PostPosted: Tue Nov 29, 2005 2:01 pm     Reply with quote

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!!
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