This web site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more.

Interrupt using RB0/Int pin

Last update: 31/07/2015

The use of the pin RB0/Int to execute an interrupt routine caused by an external pulse is a really interesting function available almost in any picmicro. This is a really easy matter to be managed particularly using high-level languages like MikroBasic/MikroC.

The use of the RB0/Int to manage interrupt externally generated requires the setting of a couple of registers of the picmicro: INTCON and OPTION_REG:


Interrupt intcon

It should be clear that the use of RB0/Int function of a picmicro requires the setting of the:

GIE(7): set to 1 to enable global interrupts

INTE(4): set to 1 to enable interrupts on pin RB0

PEIE(6): to disable other periferal interrupts

and the set of the INTEDG bit(6) of the OPTION_REG register simply to select the  rising or falling edge, of the signal.




A simple portD blinking on interrupt

void interrupt(void)                 
  PORTD = 0xFF;                    //high PORTD
  INTCON.INTF = 0;                // clear the interrupt flag 

void main(void)
  TRISB = 0x01;      
  TRISD = 0x00;      
  INTCON.GIE = 1;                  //Enable Global Interrupt      
  INTCON.INTE = 1;                //Enable RB0/INT external Interrupt      
  INTCON.PEIE = 0;                //Disable all unmasked peripheral interrupt      
  OPTION_REG.INTEDG = 1;    //Interrupt on rising edge       
   PORTD = 0;         
   } while(1); 

16f877 interrupt16f628 interrupt


Here follows a simple program, for a 16F877 or similar picmicro to generate the phases of a stepper motor upon a pulse on the interrupt pin. The four phases are given by portd.F0...portd.F3:

// PORTC.F7 = Direction
int stepH [] = {1,3,2,6,4,12,8,9}; // Half step  0001  0011  0010  0110 0100 1100 1000 1001
int number,direction;
void interrupt(void){                  // generation of the phases
     if (direction)
        { number++;}
        else {number--;}
     if (number>7) number=0;
     if (number<0) number=7;
     PORTD = stepH[number];
     delay_us(5);                         // small delay to set the phase correctly
     INTCON.INTF = 0;               // clear the interrupt flag

void main(void){
    number = 0;
    direction =1;
    TRISB = 0x01;
    TRISD = 0x00;
     PORTD = 0x00;
     INTCON.GIE = 1;                      //Enable Global Interrupt
     INTCON.INTE = 1;                    //Enable RB0/INT external Interrupt
     INTCON.PEIE = 0;                    //Disable all unmasked peripheral interrupt
     OPTION_REG.INTEDG = 1;      //Interrupt on rising edge
      do {
         if (PORTC.F7) direction = 1 ;
         PORTD = 0;                       // disable stepper if no-steps - to save power supply
         } while(1);

Diritti riservati 2008-2015. Tutti i marchi riportati sono detenuti dai legittimi proprietari. Tutto il materiale e` liberamente utilizzabile ma non per scopi commerciali. Non si assume alcuna responsabilita` sul materiale pubblicato per eventuali danni diretti o indiretti che possano derivare dall`utilizzo di schemi, progetti o altro materiale presente nel sito. Inviare una mail a: (contenuti) (webserver) per domande e/o suggerimenti.