Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

interrupt

Status
Not open for further replies.
Code:
]#include   <pic.h>

/*
 *   Interrupt demo for PIC; wait for button press on RB0/INT,
 *   turn on a relay on another port bit for a period of time.
 *   For simplicity here, literal constants are used, usually these
 *   should be calculated with compile-time arithmetic.
 */

#define RELAY RB4                             // use this bit to drive relay

static unsigned int   relay_timer;         // timer value for relay driver

           void main(void)
{
                  /* setup stuff */

             RELAY = 1;                             // ensure relay is off before enabling output
             TRISB = 0x03;                       // Port B bits 7 and 6 are output 

              OPTION   = 0b00000111;
              TRISB   = 0b00000011;

               RBPU = 0;                           // Use internal pullups
               T0IE = 1;                            // Enable interrupt on TMR0 overflow
               INTEDG = 1;                      // falling edge trigger the interrupt
               INTE = 1;                          // enable the external interrupt
               GIE = 1;                            // Global interrupt enable
               for(;;)
   {
               CLRWDT();                       // Idly kick the dog
   }
   }

                static void interrupt
  
                isr(void)                                .
{
                if(T0IF)
{                                                            // timer interrupt
                PORTB ^= 4;
                if(relay_timer)
{                                                                 // is the relay timer running?
               relay_timer--;                                      // decrement it
               PORTB |= 8;
 }
            else
{
             RELAY = 1;                // turn the relay off
             PORTB &= ~8;           // toggle a bit to say we're alive
 }
            T0IF = 0;                     // clear the interrupt flag
  }
            if(INTF) 
{                                             // did we see a button press?
            RELAY = 0;                   // turn the relay on
            relay_timer = 16;           // start the timer - 16 timeouts ~ 1 second
            INTF = 0;                       // clear the interrupt
   }
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top