#include "main.h"		

#pragma config OSC = HS     	/* Para osc a 20Mhz */
#pragma config FCMEN = OFF		/* Para forzar a usar solo el osc externo */
#pragma config IESO = OFF		/* Osc Interno OFF */
#pragma config PWRT = OFF 		/* Power Up Timer OFF */
#pragma config BOREN = OFF		/* Si detecta una falla en la alimentacion: RESET */
#pragma config BORV = 3     	/* La tensión a la que se resetea, antes 43 */
#pragma config WDT = OFF, WDTPS = 128 /* Watch Dog Timer ON y con un preescaler de 128 */
#pragma config MCLRE = ON		/* MCLR Externo ON */
#pragma config LPT1OSC = OFF	/* Timer 1 Osc Int OFF */
#pragma config PBADEN = OFF		/* Pines del B no analógicos */
#pragma config STVREN = ON 		/* Si hay un desborde del stack: RESET */
#pragma config LVP = OFF		/* Low Voltage Programming OFF */
#pragma config XINST = ON		/* Para modo extendido */
#pragma config DEBUG = ON		/* Para habilitar el ICD */



#pragma udata MyBanco1 = 0x100
	unsigned char COUNT;
	char Cadena[8];
	char CRC[4];
	char aux[3];
	unsigned char TSend;			// Para el display
	unsigned char x_display;
	unsigned char y_display;
	unsigned char TempNro;
	unsigned char TempNro1;
	unsigned char TempRes;
	unsigned char TempPos;
	unsigned char TEMPPORTB;
	unsigned int ADTemp=0;
	int iADTemp;
	unsigned int miliseg=0;
	unsigned int seg=0;
	unsigned int pulso=0;
	unsigned int i;
	unsigned char hora=0;
	unsigned char min=0;
	unsigned char tiempo_ultimo_envio;
	unsigned char tiempo_actual;
#pragma udata


//----------------------
//-------MAIN ----------
//----------------------

void main()
{
	
	PR2 = 249; // interrumpe cada 1ms
  	OpenTimer2(	TIMER_INT_ON &
					T2_PS_1_4	 &
					T2_POST_1_5	  ); //prescaler=4 y postescaler=5 ---> tiempo = presc x postesc x 4/fosc x (PR2 + 1)
								 // Despejo, PR2 = 249

  	OpenUSART (USART_TX_INT_OFF &
  	           USART_RX_INT_ON &
   	           USART_ASYNCH_MODE &
   	           USART_EIGHT_BIT &
   	           USART_CONT_RX &
   	           USART_BRGH_HIGH, 129);

	iADTemp=0;
	
	//Configuramos el ADC con referencias en VDD y VSS.
	
	ADCON0 = 0x01;
	TRISA = 0XFD; // D=1101
	TRISD = 0XFF;
	ADCON1 = 0x0E; //E=1110
	ADCON2 = 0X0b10111110;

	LATAbits.LATA1=0;
	
	INTCON  |= 0X40;//GIE/GIEH: Global Interrupt Enable bit, habilito laas int globales
   				   	 //PEIE/GIEL: Peripheral Interrupt Enable bit, habilito las int perifericas
				 	 //INT0IE: INT0 External Interrupt Enable bit, habilito interrup RB0
	
	INTCONbits.GIE=1;

	Conversor();
	Conversor();


	while(1)
	{
		Conversor();
		iADTemp = ADTemp / 2.0466666;
		tiempo_actual=seg;
		if((tiempo_actual-tiempo_ultimo_envio)>=5)
		{
			sprintf(Cadena,(char *)"Temp: %i\n\r" , iADTemp);
			putsUSART(Cadena);
			tiempo_ultimo_envio=seg;
		}

		if(iADTemp>50 || iADTemp<=1 || hora>=4) //Apagado del equipo
		{	
			if(PORTDbits.RD2==1)
			{	
				PORTAbits.RA1=1;
				pulso=seg;			
				while((seg-pulso)<=1);
				PORTAbits.RA1=0;
				pulso=seg;
				while((seg-pulso)<=30);	
				if(PORTDbits.RD2==1)
				{
					LATAbits.LATA1=1;			
					pulso=seg;			
					while((seg-pulso)<=5);
					LATAbits.LATA1=0;
				}
			miliseg=0;
			seg=0;
			min=0;
			hora=0;		
			}

		}
		if(iADTemp<48 && iADTemp>2) // Encendido del equipo
		{	
			if(PORTDbits.RD2==0)
			{	
				pulso=seg;
				Nop();
				while((seg-pulso)<=5);
				PORTAbits.RA1=1;			
				pulso=seg;	
				Nop();		
				while((seg-pulso)<=1);
				PORTAbits.RA1=0;
				pulso=seg;
				while((seg-pulso)<=15);
			}

		}
		
		
	}

}

void Conversor()
{
	extern unsigned int ADTemp;
	unsigned int ADC;
	ADCON0 = (ADCON0 & 0b11000001);
	ADCON0bits.GO = 1;
	while(ADCON0&0x02);	
	ADC=ReadADC();
	ADTemp=ADC;

}	

#pragma code prioridad_alta = 0x08	
void interrupcion_alta(void)
{
	_asm 
      	btfsc PIR1,1,0       //Chequeo si la interrupción vino del timer2
		goto interrupcion
	_endasm
}
#pragma code


  

#pragma interrupt interrupcion
void interrupcion(void)
{	
    _asm
	goto	Timer2
	goto	FinInt 	 

Timer2:
	bcf		PIR1,1,0		//Limpiamos el flag de la interrupc (el primer "1" indica el bit a limpiar, y el segundo indica que es un comando, si este ultimo estuviese en uno buscaría el registro en la RAM)
	movlb	0x01			//Pasarse al banco 1.
	_endasm
	miliseg++;
	if(miliseg >= 1000)
	{
		seg++;
		miliseg=0;
	}
	if(seg >= 60)
	{
		min++;
		seg=0;
	}
	if(min >= 60)
	{
		hora++;
		min=0;
	}

	_asm

	goto    FinInt
FinInt:
	_endasm

}
