#include <16F648A.h>
#FUSES XT,INTRC_IO,NOWDT,PUT,NOMCLR,NOBROWNOUT,NOLVP,NOCPD,NOPROTECT
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)
//GPIO
#byte PORTA = 0x05
#byte PORTB = 0x06
#bit LAMP1 = PORTA.0
#bit LAMP2 = PORTA.1
#bit LED2 = PORTA.2
#bit LED1 = PORTA.3
#bit CRUCE = PORTB.0
#bit DWN2 = PORTB.1
#bit UP2 = PORTB.2
#bit DWN1 = PORTB.3
#bit UP1 = PORTB.4
//Constantes
#define LIMINF 2 //limite inferior de intensidad (de cero a cien)
#define LIMSUP 96 //limite superior de intensidad (de cero a cien)
#define ONOFF 5000 //tiempo que tarda en encenderse/apagarse completamenta la luz en mS
#define DELAY (ONOFF/LIMSUP) //tiempo de cada paso de dimmer
#define IGUAL 0
#define LAMPARA1 1
#define LAMPARA2 2
//Variables
short flagIntExt = FALSE; //flag de interrupcion externa
int Primero = 0; //establece que lampara tiene la intensidad mas baja
int Intensidad1 = LIMINF; //intensidad actual de la lampara1
int Intensidad2 = LIMINF; //intensidad actual de la lampara2
long IntensidadCCP1 = 0; //cuando tiene que saltar la interrupcion CCP
long IntensidadCCP2 = 0; //cuando tiene que saltar la interrupcion CCP
long Semiciclo = 0; //duracion del semiciclo
//Rutinas
void CalcularIntensidad(void);
#int_EXT
void INT_EXT_isr(void){
LAMP1 = FALSE;
//LAMP2 = FALSE;
Semiciclo = get_timer1(); //establece la duracion del semciclo
CalcularIntensidad();
set_timer1(0); //restablece timer
}
#int_CCP1
void CCP1_isr(void){
//if(Primero == LAMPARA1){
LAMP1 = TRUE; //enciendo lampara 1
//LED1 = TRUE;
/*CCP_1 = IntensidadCCP2; //establezco proximo CCP
}
else if(Primero == LAMPARA2){
LAMP2 = TRUE; //enciendo lampara 2
LED2 = TRUE;
CCP_1 = IntensidadCCP1; //establezco proximo CCP
}
else{
LAMP1 = TRUE;
LAMP2 = TRUE;
LED1 = TRUE;
LED2 = TRUE;
}*/
}
void main() {
short Luz2 = FALSE;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_COMPARE_INT);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_EXT);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
setup_oscillator(OSC_4MHZ);
set_tris_a(0b00000000);
set_tris_b(0b00011111);
ext_int_edge(0,L_TO_H); //configura INT para que solo se active a la subida
CalcularIntensidad();
LAMP1 = FALSE;
LAMP2 = FALSE;
LED1 = FALSE;
LED2 = FALSE;
do{
if((UP1 == TRUE) && (UP2 == FALSE) && (DWN1 == FALSE) && (DWN2 == FALSE)){
while(UP1 == TRUE){delay_ms(50);} //espera que se suelte
//subir luz 1
while((Intensidad1 < LIMSUP) && (UP1 == FALSE)){
Intensidad1++; //aumenta intensidad
delay_ms(DELAY);
}
while(UP1 == TRUE){delay_ms(50);} //espera que se suelte
}
/*
if((UP2 == TRUE)){// && (UP1 == FALSE) && (DWN1 == FALSE) && (DWN2 == FALSE)){
//subir luz 2
while((Intensidad2 < LIMSUP) && (DWN2 == FALSE)){
Intensidad2++; //aumenta intensidad
delay_ms(DELAY);
}
while(DWN2 == TRUE){delay_ms(50);} //espera que se suelte
}
*/
if((DWN1 == TRUE) && (UP1 == FALSE) && (UP2 == FALSE) && (DWN2 == FALSE)){
while(DWN1 == TRUE){delay_ms(50);} //espera que se suelte
//bajar luz 1
while((Intensidad1 > LIMINF) && (DWN1 == FALSE)){
Intensidad1--; //disminuye intensidad
delay_ms(DELAY);
}
while(DWN1 == TRUE){delay_ms(50);} //espera que se suelte
}
/*
if((DWN2 == TRUE)){// && (UP1 == FALSE) && (UP2 == FALSE) && (DWN1 == FALSE)){
//bajar luz 2
while((Intensidad2 > LIMINF) && (UP2 == FALSE)){
Intensidad2--; //aumenta intensidad
delay_ms(DELAY);
}
while(UP2 == TRUE){delay_ms(50);} //espera que se suelte
}
*/
if(UP2 == TRUE)
Luz2 = TRUE;
if(DWN2 == TRUE)
Luz2 = FALSE;
LAMP2 = Luz2;
LED2 = Luz2;
}while(TRUE);
}
void CalcularIntensidad(void){
long Step = 0;
if(Intensidad1 > LIMINF)
LED1 = TRUE;
else
LED1 = FALSE;
Step = Semiciclo / LIMSUP;
IntensidadCCP1 = Semiciclo - (Step * Intensidad1);
//IntensidadCCP2 = Semiciclo - (Step * Intensidad2);
//if(Intensidad1 < Intensidad2){
//Primero = LAMPARA1;
CCP_1 = IntensidadCCP1;
/*}
else if(Intensidad1 > Intensidad2){
Primero = LAMPARA2;
CCP_1 = IntensidadCCP2;
}
else{
Primero = IGUAL;
CCP_1 = IntensidadCCP1;
}*/
}