how to use interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by A_Mousa, Mar 14, 2010.

  1. A_Mousa

    Thread Starter New Member

    Mar 14, 2010
    8
    0
    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.
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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
     
  3. symqwerty

    Member

    Feb 22, 2010
    31
    0
    MikroC code

    Code ( (Unknown Language)):
    1.  
    2. void interrupt()
    3. {
    4.   if(INTCON.INTF == 1)
    5.   {
    6.     //do something here
    7.     INTCON.INTF = 0; //must be cleared ourself
    8.   }
    9. }
    10.  
    11. void main()
    12. {
    13. OPTION_REG.INTEDG = 1;  // select interrupt on rising/falling edge
    14. INTCON.GIE = 1;      // global int enable bit
    15. INTCON.INTE = 1;      // enable int
    16. TRISB = 0x01;           // set PORTB0 (RB0/INT) as input ~ i'm not sure if this line is needed...
    17. while(1)
    18. {
    19. // wait for interrupt
    20. }
    21. }
    22.  
     
    Last edited: Mar 15, 2010
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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!
     
  5. A_Mousa

    Thread Starter New Member

    Mar 14, 2010
    8
    0
    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
     
  6. sairyuva

    New Member

    Mar 9, 2010
    11
    0
    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).
     
  7. sairyuva

    New Member

    Mar 9, 2010
    11
    0
    U can check interrupt by making an event in interrupt routine like making any pin high or low. U better use simulator...
     
  8. symqwerty

    Member

    Feb 22, 2010
    31
    0
    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..
     
  9. A_Mousa

    Thread Starter New Member

    Mar 14, 2010
    8
    0
    Thanksssssssssss
    for this useful information i will try it .
     
Loading...