control de giro bipolar con botón de interrupción en pic c
pic16f887, motor bipolar, control de giro con botón de interrupcion
Objetivos:
Con un botón controlar el giro de un motor bipolar usando interrupcion externa
Material:
Motor bipolar
Pic16f887
boton
ld293
Diagrama en Proteus

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 derecha(){
output_c(0b00001010) ;
delay_ms(500); // pausa de 1 seg
output_c(0b00001001);
delay_ms(500);
output_c(0b00000101) ;
delay_ms(500);
output_c(0b00000110) ;
delay_ms(500);
output_c(0b00001010) ;
delay_ms(500);
}
void izquierda(){
output_c(0b00000110) ;
delay_ms(500); // pausa de 1 seg
output_c(0b00000101) ;
delay_ms(500);
output_c(0b00001001) ;
delay_ms(500);
output_c(0b00001010) ;
delay_ms(500);
output_c(0b00000110) ;
delay_ms(500);
}
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){
derecha();
}
else{
izquierda();
}
} while (TRUE);
}
