Interrupt handling in 16F877A

interrupt handling is crucial part of MCU programming. As is means it's define how handle second process while doing work.

This is Stepper motor driver code. PORTD.F0 LED is set to blink four time when PORTB.F0/INT trigger. 


unsigned int i;
void blink(){
for (i=0; i<=4; i++){    // four time loop
      PORTD.F0=1;         // portd pin1 high
      Delay_ms(300);     // delay 300 ms
      PORTD.F0=0;         // portd pin1 low
      Delay_ms(300);}
      }
void interrupt (void){      // interrupt routine 
INTCON.GIE = 0;               // disable global interrupt
if(INTCON.INTE==1){     // if portb.1 == 1 (if interrupt occurred )
                   PORTC=0;       // put portc low 
                   blink();}           // jump to blink
INTCON.INTE=0;              // clear interrupt flag 
INTCON.GIE=1;}              // enable global interrupt 


void main() {
  TRISB.F0=1;                   // set portb.0 as input
  TRISD.F0=0;                   //  set portb.0 as output
  TRISC=0;                           //  set portb.0 as output
  //ADCONO=0;
  INTCON=0;
  INTCON.GIE=1;                 // enable global interrupt 
  INTCON.INTE=1;
              while(1){                    //Spepper
             PORTC=3;
             Delay_ms(200);          // motor 
             PORTC=6;
             Delay_ms(200);            // Drive
             PORTC=12;
             Delay_ms(200);
             PORTC=9;                       // cycles
             Delay_ms(200);}
}

No comments:

Post a Comment