Wednesday, September 19, 2012

Extra interrupts

Once there is an interrupt routine, adding an extra interrupt is simple. In the last post there was only one thing causing an interrupt. There could have been any one of fourteen on a PIC16F873, so you need to check which interrupt has triggered, respond to it, reset the interrupt flag and return. I decided to add another to respond to the RB0 input. This pin can generate an interrupt on either a rising edge or a falling edge. I have tried both and it makes little difference, simply because I have a simple push switch that, like most switches, bounces. It switches on and off a few times as the button is pressed and even as the button is released. If a debounced switching system or real trigger such as a rotary encoder was being used then it would matter if the edge was rising or falling. The new interrupt routine is here:

// ******* interrupt routine ********
interrupt void intrtn() {
    /*
     * interrupts all come here.
     * First decide what caused the interrupt,
     * then respond to it
     */

    if (INTCONbits.T0IF) { // timer0 rolled
        static uint16_t icount=0;   // counts the timer clicks

        icount+=512;

        if (icount>1000) {          // past a millisecond
            ++millis;
            icount-=1000;           // this preserves any error, which eventually smoothes out
        }
        INTCONbits.T0IF=0;          // turn off int flag
    }
    if (INTCONbits.INTF) {  // RB0 input fired
        LEDoff=millis+2000;         // turn of secends from now
        INTCONbits.INTF=0;
    }
}


The great thing about now having a millisecond timer built-in is we can use it anywhere we want. Here I set a timer to the current millisecond+2000 and we can use that in the master loop to switch an LED on for two seconds.

        if (millis>LEDoff) {
            manual_LED=0;
        } else {
            manual_LED=1;
        }


No comments:

Post a Comment