Programación de interrupciones externas pic16f887 en pic c
Programación de interrupciones externas pic16f887 en pic c
Objetivos:
- Programar un interrupción al oprimir un botón conectado al pin RB0
- Cada que se genera un interrupción ejecutar un rutina que cambie el valor de un bandera
- En el programa principal dependiendo del valor de la bandera poner a parpadear un led, o apagarlo.
Diagrama Proteus:

Código 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 int1 bandera = 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); // esperamos 20ms a dejar que se asiente estado del botón antes de que se lea if(input(PIN_B0)==0){ bandera=!bandera; } } 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 while(true){ if(bandera==1){ output_high(PIN_D1); delay_ms(1000); output_low(PIN_D1); delay_ms(1000); } } }
