// Conversor tipo Buck DC-DC (step-down converter) ,
// controlador por voltaje.
//Version 1.1
//Regular voltaje usando comaparador y Vref interno --HECHO
//Version 1.0
//Regula voltaje correctamente haciendo uso de un comparador externo--HECHO
//++++++++++++++++++++++++++++++++++++++++++++++++++++++//
#include <12F683.h>
#FUSES INTRC_IO,NOWDT,PUT,MCLR,BROWNOUT,PROTECT,CPD
#use delay(clock=8M)
#use fast_io(A)
#zero_ram
//+++++++++++++++++++++++++++++++++++++++++
int16 duty_cycle;
//+++++++++++++++++++++++++++++++++++++++++
#Define LED1 pin_A5
//+++++++++++++++++++++++++++++++++++++++++
void main(){
output_A(0x00); //Latch
set_tris_A(0B001001); //MCLR=A3= RX input Int_RA3 on Change
//RA0 = --
//RA1 = Entrada Inversora del comparador Interno
// La configura el modulo como entrada.
//RA2 = Salida PWM
//RA3 = MCLR
//RA4 = --
//RA5 = Status de la operacion 1=PWM ON
//----- Configuracion comparador -------------------//
setup_comparator(A1_VR); // Entrada Inversora y uso de Vref
setup_vref(VREF_LOW|12); // Vref Low range = (Numero/24)*VCC
// 2.5v = (12/24)*5v
//Numero= (Vref/5v)*24
//----- Configuracion PWM --------------------- //
SETUP_CCP1(CCP_PWM); // Seleccionamos PWM
SETUP_TIMER_2(T2_DIV_BY_1, 19,1); // Frecuencia = 100KHz
SET_PWM1_DUTY(0); // Duty cycle = 0%
// Duty cycle maximo = 50% corresponde a 40 unidades
// Duty cycle max depende de los calculos a maxima carga.
//------------------------------------------------
//enable_interrupts(INT_COMP);
//enable_interrupts(GLOBAL); //Interrupciones Habilitadas
//--------- PROGRAMA PRINCIPAL -----------------------------\\\\
setup_comparator(A1_VR); // se rescribe en el registro para condicion mismatch
// Encendido suave //
while(C1OUT==1&&duty_cycle<30){ // Vin < Vref Vin > 2.5v
SETUP_CCP1(CCP_PWM); // Encendemos el modulo PWM
SET_PWM1_DUTY(duty_cycle++);
output_high(LED1); // Encendemos LED
delay_ms(25); // Se hace una demora de un ciclo PWM minimo antes de aumentar
}
// Encendido suave //
while(1){
while(C1OUT==0&&duty_cycle>0){ // Vin >= Vref Vin >= 2.5v
SET_PWM1_DUTY(duty_cycle--);
if(duty_cycle==0)SETUP_CCP1(CCP_OFF); // Apagamos modulo PWM porque sigue enviando duty aunque sea 0%
output_low(LED1); // Apagamos LED
delay_us(20);
}
while(C1OUT==1&&duty_cycle<40){ // Vin < Vref Vin > 2.5v
SETUP_CCP1(CCP_PWM); // Encendemos el modulo PWM
SET_PWM1_DUTY(duty_cycle++);
output_high(LED1); // Encendemos LED
delay_us(20); // Se hace una demora de un ciclo PWM minimo antes de aumentar
}
}//End while principal,loop infinito.
}//end main