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

Weird behavior with interrupt

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



Joined: 14 Sep 2006
Posts: 1

View user's profile Send private message

Weird behavior with interrupt
PostPosted: Tue Jun 19, 2007 2:04 pm     Reply with quote

Hello knowledgeable pic peeps.

I'm trying to do a simple routine using the rb_isr interrupt. The interrupt routine works ok. Basically my simple program toggles between 2 led patterns depending on whether the interrupt is true (i have a switch on pin b4 going to gnd with a 1k weak pullup).

My problem is that when I turn on the pic, it just sits there and doesn't do anything. It doesn't do the default routine or the interrupt routine, despite the fact that I tried to init the status variable in main().

As soon as I do the interrupt one time, it starts to work and works from then on! When i comment out the enable_interrupts it runs the default routine.

I'm sure I'm overlooking something obvious. Can someone help me out?

thanks!

Code:

#include  <16F88>                   
#include <STDLIB.h>
#fuses INTRC,NOWDT,NOPROTECT,NOLVP, NOBROWNOUT, NOMCLR  /* set the configuration bits*/
#use  standard_io(A)
#use  standard_io(B)                            /* use standard_io - this will normally always be included
                                                in your files */

#use  delay(clock=8000000)                      /* tell the compiler what speed xtal or clock you are running your pic at.
                                                This is important so that it gets the delay_ms() calls below
                                                correct */

# byte port_b = 6 //sets up port_b as a variable


int state=0; //the interrupt flag

static char dummy;

#int_rb
rb_isr() {  //this interrupt function is called when any of port b pins B4-B7 change states


dummy = port_b; //read  port_b

        if(input(PIN_B4) == 0) {  //testing B4 for interrupt switch
               output_low(PIN_B0);  //turn off status led to show interrupt has occurred
               state=1;
        }


}


void status_led() {

        output_high(PIN_B0);//turns on led for normal state

}

void pattern_1(){ //do this pattern during the interrupt
output_high(PIN_A0);
delay_ms(50);
output_low(PIN_A0);
state=0;
}

void pattern_2(){ //do this pattern in normal state
output_high(PIN_A1);
delay_ms(100);
output_low(PIN_A1);
delay_ms(100);
output_high(PIN_A2);
delay_ms(100);
output_low(PIN_A2);
}

main() {

   setup_oscillator(OSC_8MHZ|OSC_INTRC);                //set up internal oscillator to run at 8mhz

   enable_interrupts(INT_RB);      /* enable RB interrupt (port B input interrupt) */
   enable_interrupts(GLOBAL);      /* start the interrupts running */
 
  // dummy = port_b; //read  port_b

 
   state=0; //init the state
 
   while (true) {

      status_led();  //led on during normal state

      if (state==1){
            pattern_1();
      }
      else if (state==0){
            pattern_2();
      }

      delay_ms(20);

        }  /*end of while loop*/

 }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 19, 2007 2:41 pm     Reply with quote

I didn't look at your program in detail, but before you enable interrupts
in main(), you should:

1. Read Port B to clear the mis-match condition.
2. Call the clear_interrupt() function with INT_RB as the parameter.

By doing these things, you ensure that you don't start out with
a pre-existing interrupt condition.

Your code shows that you were trying to partially do this, by reading
port B. But it's commented out, and the location of the line is wrong.
It must be done before you enable interrupts in main().
Guest








PostPosted: Tue Jun 19, 2007 5:48 pm     Reply with quote

Thanks! That fixed it.
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