how to use interrupt

Thread Starter

A_Mousa

Joined Mar 14, 2010
8
hello!

i want a C code for using the interrupt pin RB0/INT in PIC16f877A
as i want to check this pin (if this pin have an i/p of logic 1 i want to take an action)
so i want to know how to use interrupt??
and is there interrupt enables or something like that??

thanks in advance.
 

AlexR

Joined Jan 16, 2008
732
The registers you will need to set up are the INTCON register to turn on interrupts and check flags and the OPTION_REG register to set the portb interrupt trigger options.
Read the data sheet under "Special Features of the CPU/Interrupts" (page 131 of the Acrobat document)

As to implementing interrupts in C, read your C manual as there is no standard in the C language with regrade to the implementation of interrupts so every version of C compiler does interrupts in its own way.

For a run down on using interrupts take a look at the following links, I realise its all in assembler and things like register saving may happen automatically in C so can be ignored but they still give you a good insight into using interrupts.
http://www.phanderson.com/PIC/16C84/interrupts/interrupt_1.html
http://www.phanderson.com/PIC/16C84/interrupts/interrupt_change.html
http://www.phanderson.com/PIC/16C84/interrupts/interrupt_1a.html
 

symqwerty

Joined Feb 22, 2010
31
hello!

i want a C code for using the interrupt pin RB0/INT in PIC16f877A
as i want to check this pin (if this pin have an i/p of logic 1 i want to take an action)
so i want to know how to use interrupt??
and is there interrupt enables or something like that??

thanks in advance.
MikroC code

Rich (BB code):
void interrupt()
{
  if(INTCON.INTF == 1)
  {
    //do something here
    INTCON.INTF = 0; //must be cleared ourself
  }
}

void main()
{
OPTION_REG.INTEDG = 1;  // select interrupt on rising/falling edge
INTCON.GIE = 1;      // global int enable bit
INTCON.INTE = 1;      // enable int 
TRISB = 0x01;           // set PORTB0 (RB0/INT) as input ~ i'm not sure if this line is needed...
while(1)
{
// wait for interrupt
}
}
 
Last edited:

AlexR

Joined Jan 16, 2008
732
MikroC code

Rich (BB code):
void interrupt()
{
  if(INTCON.INTF == 1)
  {
    //do something here
    INTCON.INTF = 0; //must be cleared ourself
  }
}

void main()
{
OPTION_REG.INTEDG = 1;  // select interrupt on rising/falling edge
INTCON.GIE = 1;      // global int enable bit
INTCON.INTE = 1;      // enable int 
TRISB = 0x01;           // set PORTB0 (RB0/INT) as input ~ i'm not sure if this line is needed...
while(1)
{
// wait for interrupt
}
}
Only if the OP uses MicroC. In any other C compiler it either won't compile and/or won't work. Which is why I said read your C manual!
 

Thread Starter

A_Mousa

Joined Mar 14, 2010
8
Thanks for both of u AlexR & symqwerty

@symqwerty
thanks for this code

but i have questions

how can i check the interrupt??
can i check it by calling the interrupt function or by checking the pin INT/RB0 ??

and what shall i put inside this loop??

while(1)
{
// wait for interrupt
}
}
Thanks
 

sairyuva

Joined Mar 9, 2010
11
U don't need to put anything inside the loop for this case. Interrupt function will be invoked after change in RB0 pin(rising of falling ur choice).
 

symqwerty

Joined Feb 22, 2010
31
If interrupt occurred, the program AUTOMATICALLY jumps to ISR or in this case call "interrupt" function. Then, it will check the FLAG bit and execute corresponding instruction. Therefore, every time interrupt occurred, micro-controller will assert flag bit ( or you can say it as status bit ) - then, based on the flag bits, you would know which interrupt occurs. You DONT have to call 'interrupt function'

Inside the looping statement, you can put anything you want. If interrupt occurred, the program will STOP for a moment, call 'interrupt function' and execute it. Next, the program will continue execution at the location where it stop before. Remember, all of these processes automatically done by micro-controller.

You can set the pin to have an interrupt at rising edge or falling edge by using this bit, OPTION_REG.INTEDG. For example, it you set this bit to have int at rising edge, then, when int occurred, u will know the state of the port(of course at rising edge).

Notes : the interrupt function or ISR is compiler-dependent. So, as suggested by AlexR, pls check your C manual..
 
Top