External interruption with PIC16f887
Interrupciones, pic16f887
External interruption with PIC16f887
Objectives:
How use external interruption with a button connect to pin RB0
Introducción:
The handling of interruptions is a programming technique, based on an automatic mechanism in the microcontroller hardware, that allows to pay attention to some internal or external peripheral, only when it is required. An interrupt is actually a call to a subroutine, but initiated by the peripheral’s own hardware and not by the «CALL» instruction. Interruption can occur at any time.
Development
In the example shown is very simple, is to generate an external interruption by pressing a button connected to port rb0,
A flag is defined (by default its value is 1)
The interrupt subroutine is defined (when the button is pressed, the value of the flag is switched)
In the main routine the led flashes if the flag is 1 and if it is 0 it does not
Proteus Diagram

Programa en pic c
#include <16F887.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES NOPUT //No Power Up Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V
#use delay(clock=8000000)
BYTE blink = 0; // bandera
#int_rb //you use the pre-processor command ‘#int_xxxx’, where ‘int_xxxx’ is the individual interrupt you are using, followed by the interrupt handler function.
void button_isr() {
delay_ms (20); //debounce esperamos 20ms a dejar que se asiente estado del botón antes de que se lea
if( !input(PIN_B0) && !blink ) // cambia el estado de la bandera de 0 a 1 ( para que empieze a parpadear el led)
blink = 1;
else if( !input(PIN_B0) && blink ) // cambia el estado de la bandera de 1 a 0 (para que deje de parpadear el led)
blink = 0;
}
void main() {
enable_interrupts(global); // habilita interrupcion a nivel global
enable_interrupts(int_rb0); // habilita interrupcion a nivel individual por cambio en el pin rb0
ext_int_edge( H_TO_L ); // la interrupcion se genera cuando el pin rbo cambia de un valor alta a un valor bajo
do {
if(blink){
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
delay_ms(500);
}
} while (TRUE);
}
