//************************************************************************************
//**  
//**  File name:     C:\Users\Flonasa 01\Desktop\R0 flow\luz5.c
//**  Title:         
//**  Description:   
//**  
//**  Generated by:  Flowcode v5.1.0.0
//**  Date:          Saturday, January 19, 2013 18:13:37
//**  Licence:       Professional
//**  Registered to: Manuel Navarro
//**  Licence key:   FUAMTE
//**  
//**  
//**  http://www.matrixmultimedia.com
//**  
//************************************************************************************


#define MX_PIC

#define MX_USES_UINT8 1
#define MX_USES_SINT16 1
#define MX_USES_CHAR 0
#define MX_USES_FLOAT 0
#define MX_USES_SINT32 0
#define MX_USES_BOOL 1
#define MX_USES_UINT16 0
#define MX_USES_UINT32 0
//Definiciones para el microcontrolador
#define P16F88
#define FC_CAL_PIC
#define MX_ADC
#define MX_ADC_TYPE_2
#define MX_ADC_BITS_10
#define MX_EE
#define MX_EE_TYPE2
#define MX_EE_SIZE 256
#define MX_SPI_1
#define MX_SPI_1_MISO_PORT portb
#define MX_SPI_1_MISO_TRIS trisb
#define MX_SPI_1_MISO_PIN 1
#define MX_SPI_1_MOSI_PORT portb
#define MX_SPI_1_MOSI_TRIS trisb
#define MX_SPI_1_MOSI_PIN 2
#define MX_SPI_1_CLK_PORT portb
#define MX_SPI_1_CLK_TRIS trisb
#define MX_SPI_1_CLK_PIN 4
#define MX_SPI_1_SS_PORT portb
#define MX_SPI_1_SS_TRIS trisb
#define MX_UART_1
#define MX_UART_1_TX_PORT portb
#define MX_UART_1_TX_TRIS trisb
#define MX_UART_1_TX_PIN 5
#define MX_UART_1_RX_PORT portb
#define MX_UART_1_RX_TRIS trisb
#define MX_UART_1_RX_PIN 2
#define MX_I2C
#define MX_I2C_1
#define MX_I2C_1_SDA_PORT portb
#define MX_I2C_1_SDA_TRIS trisb
#define MX_I2C_1_SDA_PIN 1
#define MX_I2C_1_SCL_PORT portb
#define MX_I2C_1_SCL_TRIS trisb
#define MX_I2C_1_SCL_PIN 4
#define MX_PWM
#define MX_PWM_CNT 1
#define MX_PWM_PSCA1
#define MX_PWM_PSCA4
#define MX_PWM_PSCA16
#define MX_PWM_1_PORT portb
#define MX_PWM_1_TRIS trisb
#define MX_PWM_1_PIN 0
#define MX_PWM_1_PORT_A portb
#define MX_PWM_1_TRIS_A trisb
#define MX_PWM_1_PIN_A 3

//Funciones
#define MX_CLK_SPEED 4000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif

//Datos de configuración
#ifdef _BOOSTC
#pragma DATA 0x2007, 0x3f62
#endif
#ifdef HI_TECH_C
__CONFIG(0x3f62);
#endif
#ifdef _BOOSTC
#pragma DATA 0x2008, 0x3ffe
#endif
#ifdef HI_TECH_C
__CONFIG(0x3ffe);
#endif

//Funciones internas
#include "C:\Program Files\Flowcode\FCD\internals.c"

//Macro function declarations


//Variable declarations
#define FCV_TRUE (1)
#define FCV_FALSE (0)
MX_SINT16 FCV_CICLOSDIA;
MX_UINT8 FCV_TD = (0x0);
MX_SINT16 FCV_CICLOS;
MX_UINT8 FCV_PUL = (0x0);
MX_UINT8 FCV_MESES;
MX_UINT8 FCV_AGUA;
MX_SINT16 FCV_TF = (0);
MX_UINT8 FCV_LEOMES;
MX_UINT8 FCV_DIAS;
MX_UINT8 FCV_CPH = (0x0);
MX_UINT8 FCV_ONOFF;
MX_UINT8 FCV_TP = (0x0);
MX_BOOL FCV_PULHIELO = (0);
MX_UINT8 FCV_XD = (0x0);
MX_UINT8 FCV_SEG1;
MX_UINT8 FCV_TS = (0x0);
MX_UINT8 FCV_XF = (0x0);
MX_UINT8 FCV_SEG2 = (0x0);




//SWITCH(0): //Defines:

/**** Macro Substitutions ****
a = Unique Reference
b = Switch Port Letter
c = Switch Pin Number
d = Switch Polarity Control
e = Switch Debounce Delay Value (us)
******************************/

#define Switch_1_SW_PORT		porta
#define Switch_1_SW_TRIS		trisa
#define Switch_1_SW_PIN		3
#define Switch_1_SW_POLE		1
#define Switch_1_SW_DELAY		100 * 1000




//SWITCH(0): //Macro function declarations

MX_UINT8 FCD_SWITCH0_ReadState();
void FCD_SWITCH0_WaitUntilHigh();
void FCD_SWITCH0_WaitUntilLow();



//SWITCH(0): //Macro implementations


MX_UINT8 FCD_SWITCH0_ReadState()
{
	
		MX_UINT8 switchval, old_switchval;
	  #if Switch_1_SW_DELAY > 0
		MX_UINT16 del_count = 0;
	  #endif

		FC_CAL_Bit_In_DDR( Switch_1_SW_PORT , Switch_1_SW_TRIS , Switch_1_SW_PIN );
		switchval = FC_CAL_Bit_In( Switch_1_SW_PORT , Switch_1_SW_PIN );			//Convert pin to input
		old_switchval = switchval;

	  #if Switch_1_SW_DELAY > 0
		while (del_count < Switch_1_SW_DELAY)									//Debounce input
		{
			delay_us(1);
			FC_CAL_Bit_In_DDR( Switch_1_SW_PORT , Switch_1_SW_TRIS , Switch_1_SW_PIN );
			switchval = FC_CAL_Bit_In( Switch_1_SW_PORT , Switch_1_SW_PIN );		//Sample input

			if (switchval == old_switchval)								//Is switch the same state?
				del_count = del_count + 1;								//Increment counter
			else
				del_count = 0;											//Else reset counter

			old_switchval = switchval;									//Update the old state
		}
	  #endif

		#if Switch_1_SW_POLE == 0
			if (switchval)												//Invert logic if required
				switchval = 0;
			else
				switchval = 1;
		#endif

		return (switchval);												//Return state of switch as least significant bit

}

void FCD_SWITCH0_WaitUntilHigh()
{
	
		MX_UINT8 switchval, old_switchval;
	  #if Switch_1_SW_DELAY > 0
		MX_UINT16 del_count = 0;
	  #endif

		do
		{
			FC_CAL_Bit_In_DDR ( Switch_1_SW_PORT , Switch_1_SW_TRIS , Switch_1_SW_PIN );
			switchval = FC_CAL_Bit_In ( Switch_1_SW_PORT, Switch_1_SW_PIN );		//Convert pin to input
			old_switchval = switchval;

		  #if Switch_1_SW_DELAY > 0
			while (del_count < Switch_1_SW_DELAY)									//Debounce input
			{
				delay_us(1);
				FC_CAL_Bit_In_DDR ( Switch_1_SW_PORT , Switch_1_SW_TRIS , Switch_1_SW_PIN );
				switchval = FC_CAL_Bit_In ( Switch_1_SW_PORT , Switch_1_SW_PIN );	//Sample input

				if (switchval == old_switchval)								//Is switch the same state?
					del_count = del_count + 1;								//Increment counter
				else
					del_count = 0;											//Else reset counter

				old_switchval = switchval;									//Update the old state
			}
		  #endif

		} while (switchval != Switch_1_SW_POLE);									//While state not active

		#if Switch_1_SW_POLE == 0
			if (switchval)													//Invert logic if required
				switchval = 0;
			else
				switchval = 1;
		#endif

		//return (switchval);												//Return state of switch as least significant bit

}

void FCD_SWITCH0_WaitUntilLow()
{
	
		MX_UINT8 switchval, old_switchval;
	  #if Switch_1_SW_DELAY > 0
		MX_UINT16 del_count = 0;
	  #endif

		do
		{
			FC_CAL_Bit_In_DDR ( Switch_1_SW_PORT, Switch_1_SW_TRIS , Switch_1_SW_PIN );
			switchval = FC_CAL_Bit_In ( Switch_1_SW_PORT, Switch_1_SW_PIN );			//Convert pin to input
			old_switchval = switchval;

		  #if Switch_1_SW_DELAY > 0
			while (del_count < Switch_1_SW_DELAY)									//Debounce input
			{
				delay_us(1);
				FC_CAL_Bit_In_DDR ( Switch_1_SW_PORT, Switch_1_SW_TRIS , Switch_1_SW_PIN );
				switchval = FC_CAL_Bit_In ( Switch_1_SW_PORT, Switch_1_SW_PIN );		//Sample input

				if (switchval == old_switchval)								//Is switch the same state?
					del_count = del_count + 1;								//Increment counter
				else
					del_count = 0;											//Else reset counter

				old_switchval = switchval;									//Update the old state
			}
		  #endif

		} while (switchval == Switch_1_SW_POLE);									//While state active

		#if Switch_1_SW_POLE == 0
			if (switchval)													//Invert logic if required
				switchval = 0;
			else
				switchval = 1;
		#endif

		//return (switchval);												//Return state of switch as least significant bit

}

#include "C:\Program Files\Flowcode\CAL\includes.c"

//Macro implementations



void main()
{
	//Inicialización
	ansel = 0;
	cmcon = 0x07;


	//Código de inicialización de la interrupciuón
	option_reg = 0xC0;


	//Salida
	//Salida: 1 -> B6
	trisb = trisb & 0xBF;
	if ((1))
		portb = (portb & 0xBF) | 0x40;
	else
		portb = portb & 0xBF;

	//Punto de Conección
	//Punto de Conección: [A]: A
FCC_Main_A:
;

	//Llamar a Macro de Componente
	//Llamar a Macro de Componente: pul=LeerEstado()
	FCV_PUL = FCD_SWITCH0_ReadState();

	//Decisión
	//Decisión: pul = 1?
	if (FCV_PUL == 1)
	{

		//Salida
		//Salida: 1 -> B7
		trisb = trisb & 0x7F;
		if ((1))
			portb = (portb & 0x7F) | 0x80;
		else
			portb = portb & 0x7F;

	} else {

		//Salida
		//Salida: 0 -> B7
		trisb = trisb & 0x7F;
		if ((0))
			portb = (portb & 0x7F) | 0x80;
		else
			portb = portb & 0x7F;

	}

	//Ir a Punto de Conección
	//Ir a Punto de Conección: [A]: A
	goto FCC_Main_A;

	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}



