#include <16F676.h> #FUSES NOWDT //No Watch Dog Timer #FUSES INTRC_IO //Internal RC Osc, no CLKOUT #FUSES NOCPD //EE protection #FUSES NOPROTECT //Code not protected from reading #FUSES NOMCLR //Master Clear pin used for I/O #FUSES NOPUT //No Power Up Timer #FUSES NOBROWNOUT //No brownout reset #use delay(clock=4000000) #use rs232(baud=2400, xmit=PIN_A0, STREAM=IO) #define P1 PIN_C2 #define P2 PIN_C1 #define P3 PIN_C0 #define P4 PIN_A2 #define RELE PIN_A5 #define LED PIN_A1 int passo; int ledCount; int1 direcao; int1 ledEstado; int1 releEstado; int1 histerese; int segundo; int sensorValor; void andaPasso(int1 d) { if(d==1) { if(passo<4) passo++; else passo=1; } if(d==0) { if(passo>1) passo--; else passo=4; } switch(passo) { case 1 : { output_bit(p1,true); output_bit(p2,false); output_bit(p3,false); output_bit(p4,false); break; } case 2 : { output_bit(p1,false); output_bit(p2,true); output_bit(p3,false); output_bit(p4,false); break; } case 3 : { output_bit(p1,false); output_bit(p2,false); output_bit(p3,true); output_bit(p4,false); break; } case 4 : { output_bit(p1,false); output_bit(p2,false); output_bit(p3,false); output_bit(p4,true); break; } } } #int_TIMER0 void clock_isr() { ledCount--; if(ledCount==0) { ledEstado =!ledEstado; ledCount = 15; // 4000000/4/255/255 = 15 segundo++; } if(segundo>3) { histerese=0; segundo=0; } } void main() { //Inicialização passo = 1; direcao = 0; ledEstado = 1; ledCount = 15; segundo = 0; histerese = 0; //seta o ad setup_adc(ADC_CLOCK_DIV_64); setup_adc_ports(sAN4); //Seta a interrupção de serviço TIMER0 set_timer0(0); setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT); enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); while(1) { //Verifica a velocidade do motor set_adc_channel(3); sensorValor = read_ADC(); putc(sensorValor); if( (sensorValor>100) || (histerese==1) ) { releEstado = 1; histerese = 1; } else { releEstado=0; } output_bit(LED, ledEstado); output_bit(RELE,releEstado); andaPasso(direcao); // ALTERAR ESSE TEMPO - aumentando diminui a velocidade da laminadora, para menos aumenta. delay_ms(5); } //Fim do laço principal }