Page 1 of 1

Need help with Interrupts PIC-16F648A

Posted: 26 Aug 2010 21:35
by X-Team
Hi,
I need help with interrupts, I know that they pause the executing of main program and execute the code under the interrupt function. I was trying to flash a led on PORTB = 0x80 the RB7 bit. And it didn't work here's the code:

Code: Select all

void main()
{
  INTCON.GIE = 1;            //Ukljuci sve Interrupte
  INTCON.INTE = 1;           //Ukljuci RB0/INT interrupt
  OPTION_REG.INTEDG = 1;     //interrupt on rising edge of RB0/INT pin.
  
  TRISA = 0xFF;
  PORTA = 0x00;
  PORTB = 0x00;
  TRISB = 0b00000001;       //Binarno znaci da samo RB0 bude ulazni port
  while(1)
  {
    PORTB = 0x02;   //Ukljuci diodu na RB1
  }

}
void interrupt()
{

   INTCON.GIE = 0;           //Iskljuci sve prekide (interrupte)
   PORTB = 0x80;           //Ukljuci led diodu na RB7
   Delay_ms(2000);
   INTCON.INTF = 0;          //Ocisti RB0 Interupt flag
   INTCON.GIE = 1;           //Ukljuci sve prekide (interrupte)
     
}
Now I tried to simulate it in proteus and when I press the button for interrupt nothing happens the LED does not flash?
I'm 100% sure that the scheme works. I think I don't know how to use the interrupts. So please correct my error here!

Thanks in advance!

Re: Need help with Interrupts PIC-16F648A

Posted: 26 Aug 2010 23:54
by braus
Hello X team, certainly you have to be sure the button you are using is going to feed 5V to RB0 bit when you press it, normally it should deliver 0V.
After that let's check the logical part of your project...

Code: Select all

void main()
{
  INTCON.GIE = 1;            //Ukljuci sve Interrupte
  INTCON.INTE = 1;           //Ukljuci RB0/INT interrupt
  OPTION_REG.INTEDG = 1;     //interrupt on rising edge of RB0/INT pin.
  
  TRISA = 0xFF;
  PORTA = 0x00;
  PORTB = 0x00;
  TRISB = 0b00000001;       //Binarno znaci da samo RB0 bude ulazni port
  while(1)
  {
    PORTB = 0x02;   //Ukljuci diodu na RB1
  }

}
void interrupt()
{

   INTCON.GIE = 0;   //it is not neccesary to clear this bit, PIC MCU does it when it enters in the
                            //interrupt.
   PORTB = 0x80;           //try PORTB.B7=1
   Delay_ms(2000);
   INTCON.INTF = 0;          //Ocisti RB0 Interupt flag
   INTCON.GIE = 1;           //Ukljuci sve prekide (interrupte)
     
}
it should work.

Re: Need help with Interrupts PIC-16F648A

Posted: 27 Aug 2010 02:54
by X-Team
I didn't quite understood you. I have the schematic like this, maybe I made there a mistake:
Image
Please correct me.

Thanks in advance!

Re: Need help with Interrupts PIC-16F648A

Posted: 27 Aug 2010 03:33
by X-Team
I also tried with this simple schematic:
Image
And this code:

Code: Select all

void main()
{
  INTCON.GIE = 1;            //Ukljuci sve Interrupte
  INTCON.INTE = 1;           //Ukljuci RB0/INT interrupt
  OPTION_REG.INTEDG = 1;     //interrupt on rising edge of RB0/INT pin.

  TRISA = 0xFF;
  PORTA = 0x00;
  PORTB = 0x00;
  TRISB = 0b00000001;       //Binarno znaci da samo RB0 bude ulazni port
  while(1)
  {
    PORTB = 0x02;   //Ukljuci diodu na RB1
  }

}
void interrupt()
{

   INTCON.GIE = 0;   //it is not neccesary to clear this bit, PIC MCU does it when it enters in the
                            //interrupt.
   PORTB.B7 = 1;           //tried with PORTB = 0x80
   Delay_ms(2000);
   INTCON.INTF = 0;          //Ocisti RB0 Interupt flag
   INTCON.GIE = 1;           //Ukljuci sve prekide (interrupte)

}
When I press the switch only led on RB1 is ON the Led or RB7 won't blink or turn on!