0
\$\begingroup\$

I 'm new to mikro c and PIC programming and I 'm having troubles shutting down the LED on RB5(green)using interrupt on RA2 and this interrupt is possible only in 5 first seconds. Any help will be appreciated

Here is the circuit in proteus

Simulation in proteus

Here is the codes in MIKRO C

void interrupt(){
  if(RA2 == 1 ){
    PORTB = 0 ;
  }
}

void main() {
  unsigned char j ;

  TRISC = 0b11111110 ;
  TRISA = 0b000100 ;
  OPTION_REG.INTEDG = 1 ;
  INTCON.INTE = 1 ;
  INTCON.GIE= 1 ;

  for ( j=0 ; j<10 ; j++ ){
    PORTC.RC0 = 1 ;
    delay_ms (500) ;
    PORTC.RC0 = 0 ;
    delay_ms (500) ;
  }

  ANSELH.ANS2 = 0 ;
  ANSELH.ANS10 = 0 ;
  ANSELH.ANS11 = 0 ;
  TRISB = 0b10010000 ;

  while (1){
    if (PORTB.RB4 == 1 )
      PORTB.RB5 = 1 ;
    else
      PORTB.RB5 = 0 ;
  }
}
\$\endgroup\$
  • \$\begingroup\$ if you try to turn off the LED without the interrupt, does it turn off? I think you need to simplify your setup and test smaller parts of the problem to determine if the hardware of the LED is workin, if the hardware of the switch is working, and later the different parts of the software, initialization, reading the switch and turning on and off the LED WITHOUT interrupts, and lastly, when you know that everything works, setup the interrupts. Divide and conquer, my friend, is the best approach to debugging. \$\endgroup\$ – payala Apr 9 '16 at 11:35
  • \$\begingroup\$ Yes Turning off the LED using the push button works well. \$\endgroup\$ – N.J.M. Robert Apr 9 '16 at 11:52
1
\$\begingroup\$

I think your problem is that you are setting the interrupt edge on the rising edge.

OPTION_REG.INTEDG = 1 ;

So, when the interrupt service routine is called, the signal will have a '1' value, so the following if,

if(RA2 == 1 ){
  PORTB = 0 ;
}

Is never true, so portb is never written to '0' value.

\$\endgroup\$
0
\$\begingroup\$

There may be other errors in this program. I stopped after seeing 4 suspicious problems:

Difficult with out knowing the definitions of the PIC hardware (with out knowing the version of the Microchip files). Regardless, the interrupt appears to not specify which output pin to change, only the port that the pin resides in.

Your program description does not appear complete. Assuming you want to control LED D1 during the first 5 seconds. This is not possible as you do not define the inputs / outputs of port B until after the time delays.

Assuming you want a delay of 5 seconds as in your description. You are looping through 2 500ms delays 10 times for a total of 10 seconds.

You do not clear the interrupt in your interrupt routine.

...

Go back and look closely at the example you appear to be using here: http://picguides.com/beginner/interrupts.php

...

@payala is correct. You are using a switch configured opposite (connected to ground) to that of the example you are using (connected to Vdd).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.