//************************************************************************************
//**  
//**  File name:     C:\Users\Tere\Desktop\Diego\PFC\Matrix Multimedia\Pruebas\Sensor\ProbandoSensor.c
//**  Generated by:  Flowcode v3.4.7.48
//**  Date:          Wednesday, September 23, 2009 20:29:40
//**  Licence:       Professional
//**  Registered to: Diego Alcaide
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Definiciones para el microcontrolador
#define P16F877A
#define MX_EE
#define MX_EE_TYPE2
#define MX_EE_SIZE 256
#define MX_SPI
#define MX_SPI_C
#define MX_SPI_SDI 4
#define MX_SPI_SDO 5
#define MX_SPI_SCK 3
#define MX_UART
#define MX_UART_C
#define MX_UART_TX 6
#define MX_UART_RX 7
#define MX_I2C
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 4
#define MX_I2C_SCL 3
#define MX_PWM
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 trisc
#define MX_PWM_1 2
#define MX_PWM_TRIS2 trisc
#define MX_PWM_2 1

//Funciones
#include <system.h>
#pragma CLOCK_FREQ 19660800

//Datos de configuración
#pragma DATA 0x2007, 0x3f3a

//Funciones internas
#include "C:\Program Files\Matrix Multimedia\Flowcode V3\FCD\internals.h"

//Macro function declarations
void FCM_pintarADC();


//Variable declarations
char FCV_ADC2;
short FCV_ADC;




//LCDDisplay0: //Macro function declarations

void FCD_LCDDisplay0_GetDefines();
void FCD_LCDDisplay0_Start();
void FCD_LCDDisplay0_Clear();
void FCD_LCDDisplay0_PrintASCII(char Character);
void FCD_LCDDisplay0_Command(char in);
void FCD_LCDDisplay0_RawSend(char in, char mask);
void FCD_LCDDisplay0_Cursor(char x, char y);
void FCD_LCDDisplay0_PrintNumber(short Number);
void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String);


//ADC0: //Macro function declarations

void FCD_ADC0_SampleADC();
char FCD_ADC0_ReadAsByte();
short FCD_ADC0_ReadAsInt();


//Switches0: //Macro function declarations

void FCD_Switches0_GetDefines();
char FCD_Switches0_ReadState(char WhichSwitch);
void FCD_Switches0_WaitUntilHigh(char WhichSwitch);
void FCD_Switches0_WaitUntilLow(char WhichSwitch);

//Definiciones suplementarias




//LCDDisplay0: //Macro implementations


void FCD_LCDDisplay0_GetDefines()
{
	
	}  //Dummy end of function to allow defines to be added correctly

	//component connections
	#define LCD_133240_PORT    portb
	#define LCD_133240_TRIS    trisb
	#define LCD_133240_BIT0    0
	#define LCD_133240_BIT1    1
	#define LCD_133240_BIT2    2
	#define LCD_133240_BIT3    3
	#define LCD_133240_RS      4
	#define LCD_133240_E       5

	#ifdef _BOOSTC
	  #define LCD_133240_DELAY   delay_10us(10)
	#endif
	#ifdef _C2C_
	  #define LCD_133240_DELAY   delay_us(100)
	#endif
	#ifndef LCD_133240_DELAY
	  #define LCD_133240_DELAY   delay_us(100)
	#endif



	//internal function prototypes
	void LCD_133240_RawSend(char nIn, char nMask);

	//internal function implementations
	void LCD_133240_RawSend(char nIn, char nMask)
	{
		unsigned char pt;
		unsigned char outVal;
		outVal = LCD_133240_PORT;
		clear_bit(outVal, LCD_133240_BIT0);
		clear_bit(outVal, LCD_133240_BIT1);
		clear_bit(outVal, LCD_133240_BIT2);
		clear_bit(outVal, LCD_133240_BIT3);
		clear_bit(outVal, LCD_133240_RS);
		clear_bit(outVal, LCD_133240_E);
		pt = ((nIn >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(outVal, LCD_133240_BIT0);
		if (pt & 0x02)
		    set_bit(outVal, LCD_133240_BIT1);
		if (pt & 0x04)
		    set_bit(outVal, LCD_133240_BIT2);
		if (pt & 0x08)
		    set_bit(outVal, LCD_133240_BIT3);
		if (nMask)
		    set_bit(outVal, LCD_133240_RS);
		LCD_133240_PORT = outVal;
		LCD_133240_DELAY;
		set_bit (LCD_133240_PORT, LCD_133240_E);
		LCD_133240_DELAY;
		clear_bit (LCD_133240_PORT, LCD_133240_E);
		pt = (nIn & 0x0f);
		LCD_133240_DELAY;
		outVal = LCD_133240_PORT;
		clear_bit(outVal, LCD_133240_BIT0);
		clear_bit(outVal, LCD_133240_BIT1);
		clear_bit(outVal, LCD_133240_BIT2);
		clear_bit(outVal, LCD_133240_BIT3);
		clear_bit(outVal, LCD_133240_RS);
		clear_bit(outVal, LCD_133240_E);
		if (pt & 0x01)
		    set_bit(outVal, LCD_133240_BIT0);
		if (pt & 0x02)
		    set_bit(outVal, LCD_133240_BIT1);
		if (pt & 0x04)
		    set_bit(outVal, LCD_133240_BIT2);
		if (pt & 0x08)
		    set_bit(outVal, LCD_133240_BIT3);
		if (nMask)
		    set_bit(outVal, LCD_133240_RS);
		LCD_133240_PORT = outVal;
		LCD_133240_DELAY;
		set_bit (LCD_133240_PORT, LCD_133240_E);
		LCD_133240_DELAY;
		clear_bit (LCD_133240_PORT, LCD_133240_E);
		LCD_133240_DELAY;
	}

	// Dummy function to close the defines section off
	void LCD_133240_Dummy_Function();
	void LCD_133240_Dummy_Function()
	{

}

void FCD_LCDDisplay0_Start()
{
	
		clear_bit(LCD_133240_TRIS, LCD_133240_BIT0);
		clear_bit(LCD_133240_TRIS, LCD_133240_BIT1);
		clear_bit(LCD_133240_TRIS, LCD_133240_BIT2);
		clear_bit(LCD_133240_TRIS, LCD_133240_BIT3);
		clear_bit(LCD_133240_TRIS, LCD_133240_RS);
		clear_bit(LCD_133240_TRIS, LCD_133240_E);

		delay_ms(12);

		LCD_133240_RawSend(0x33, 0);
		delay_ms(2);
		LCD_133240_RawSend(0x32, 0);
		delay_ms(2);
		LCD_133240_RawSend(0x2c, 0);
		delay_ms(2);
		LCD_133240_RawSend(0x06, 0);
		delay_ms(2);
		LCD_133240_RawSend(0x0c, 0);
		delay_ms(2);

		//clear the display
		LCD_133240_RawSend(0x01, 0);
		delay_ms(2);
		LCD_133240_RawSend(0x02, 0);
		delay_ms(2);

}

void FCD_LCDDisplay0_Clear()
{
	
		LCD_133240_RawSend(0x01, 0);
		delay_ms(2);
		LCD_133240_RawSend(0x02, 0);
		delay_ms(2);

}

void FCD_LCDDisplay0_PrintASCII(char Character)
{
	
		LCD_133240_RawSend(Character, 0x10);

}

void FCD_LCDDisplay0_Command(char in)
{
	
		LCD_133240_RawSend(in, 0);
		delay_ms(2);

}

void FCD_LCDDisplay0_RawSend(char in, char mask)
{
	//Error Reading Code For LCD Display::Macro_RawSend
}

void FCD_LCDDisplay0_Cursor(char x, char y)
{
	
	  #if (2 == 1)
	    y=0x80;
	  #endif

	  #if (2 == 2)
		if (y==0)
			y=0x80;
		else
			y=0xc0;
	  #endif

	  #if (2 == 4)
		if (y==0)
			y=0x80;
		else if (y==1)
			y=0xc0;
		else if (y==2)
			y=0x94;
		else
			y=0xd4;
	  #endif

		LCD_133240_RawSend(y+x, 0);
		delay_ms(2);

}

void FCD_LCDDisplay0_PrintNumber(short Number)
{
	
		short tmp_int;
		char tmp_byte;
		if (Number < 0)
		{
			LCD_133240_RawSend('-', 0x10);
			Number = 0 - Number;
		}

		tmp_int = Number;
		if (Number >= 10000)
		{
			tmp_byte = tmp_int / 10000;
			LCD_133240_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10000;
				tmp_byte--;
			}
		}
		if (Number >= 1000)
		{
			tmp_byte = tmp_int / 1000;
			LCD_133240_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 1000;
				tmp_byte--;
			}
		}
		if (Number >= 100)
		{
			tmp_byte = tmp_int / 100;
			LCD_133240_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 100;
				tmp_byte--;
			}
		}
		if (Number >= 10)
		{
			tmp_byte = tmp_int / 10;
			LCD_133240_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10;
				tmp_byte--;
			}
		}
		LCD_133240_RawSend('0' + tmp_int, 0x10);

}

void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String)
{
	
		char idx;
		for (idx=0; idx<MSZ_String; idx++)
		{
			if (String[idx]==0)
			{
				break;
			}
			LCD_133240_RawSend(String[idx], 0x10);
		}

}



//ADC0: //Macro implementations


void FCD_ADC0_SampleADC()
{
	//This is from the FCD file...
	//set up ADC conversion
	char old_tris, cnt;
	#define MX_ADC_SAMP_TIME 40

	//find appropriate bit
	#if (0 == 0)
	  #define MX_ADC_TRIS_MSK  0x01
	  #define MX_ADC_TRIS_REG  trisa
	  adcon1 = 0x04;
	#endif
	#if (0 == 1)
	  #define MX_ADC_TRIS_MSK  0x02
	  #define MX_ADC_TRIS_REG  trisa
	  adcon1 = 0x04;
	#endif
	#if (0 == 2)
	  #define MX_ADC_TRIS_MSK  0x04
	  #define MX_ADC_TRIS_REG  trisa
	  adcon1 = 0x02;
	#endif
	#if (0 == 3)
	  #define MX_ADC_TRIS_MSK  0x08
	  #define MX_ADC_TRIS_REG  trisa
	  adcon1 = 0x04;
	#endif
	#if (0 == 4)
	  #define MX_ADC_TRIS_MSK  0x20
	  #define MX_ADC_TRIS_REG  trisa
	  adcon1 = 0x02;
	#endif
	#if (0 == 5)
	  #define MX_ADC_TRIS_MSK  0x01
	  #define MX_ADC_TRIS_REG  trise
	  adcon1 = 0x00;
	#endif
	#if (0 == 6)
	  #define MX_ADC_TRIS_MSK  0x02
	  #define MX_ADC_TRIS_REG  trise
	  adcon1 = 0x00;
	#endif
	#if (0 == 7)
	  #define MX_ADC_TRIS_MSK  0x04
	  #define MX_ADC_TRIS_REG  trise
	  adcon1 = 0x00;
	#endif

	//sanity check
	#ifndef MX_ADC_TRIS_REG
	  #pragma error ADC conversion code error - please contact technical support
	#endif
	 
	//store old tris value, and set the i/o pin as an input
	old_tris = MX_ADC_TRIS_REG;
	MX_ADC_TRIS_REG = MX_ADC_TRIS_REG | MX_ADC_TRIS_MSK;

	//turn ADC on
	adcon0 = 0x81 | (0 << 3);

	//wait the acquisition time
	cnt = 0;
	while (cnt < MX_ADC_SAMP_TIME) cnt++;

	//begin conversion and wait until it has finished
	adcon0 = adcon0 | 0x04;
	while (adcon0 & 0x04);

	//restore old tris value, and reset adc registers
	MX_ADC_TRIS_REG = old_tris;
	adcon1 = 0x07;
	adcon0 = 0x80;
	#undef MX_ADC_TRIS_REG
	#undef MX_ADC_TRIS_MSK
	#undef MX_ADC_SAMP_TIME

}

char FCD_ADC0_ReadAsByte()
{
	//This is from the FCD file...
	return adresh;

}

short FCD_ADC0_ReadAsInt()
{
	//This is from the FCD file...
	short iRetVal;
	iRetVal = (adresh << 2);
	iRetVal += (adresl >> 6);
	return (iRetVal);

}



//Switches0: //Macro implementations


void FCD_Switches0_GetDefines()
{
	
	}  //Dummy end of function to allow defines to be added correctly

	//internal function implementations
	inline void Switches133286_GetMask(char nSwitch, char &nMask)
	{
		switch (nSwitch)
		{
			case 0:
				nMask = 1;
				break;
		  #if (8 > 1)
			case 1:
				nMask = 2;
				break;
		  #endif
		  #if (8 > 2)
			case 2:
				nMask = 4;
				break;
		  #endif
		  #if (8 > 3)
			case 3:
				nMask = 8;
				break;
		  #endif
		  #if (8 > 4)
			case 4:
				nMask = 16;
				break;
		  #endif
		  #if (8 > 5)
			case 5:
				nMask = 32;
				break;
		  #endif
		  #if (8 > 6)
			case 6:
				nMask = 64;
				break;
		  #endif
		  #if (8 > 7)
			case 7:
				nMask = 128;
				break;
		  #endif
			default:
				nMask = 0;
				break;
		}
	}

	// Dummy function to close the defines section off
	void Switches133286_Dummy_Function();
	void Switches133286_Dummy_Function()
	{

}

char FCD_Switches0_ReadState(char WhichSwitch)
{
		char SwitchMask = 0;
		Switches133286_GetMask(WhichSwitch, SwitchMask);

		trisd = trisd | SwitchMask;
		return ((portd & SwitchMask) != 0);
}

void FCD_Switches0_WaitUntilHigh(char WhichSwitch)
{
		char SwitchMask = 0;
		Switches133286_GetMask(WhichSwitch, SwitchMask);

		trisd = trisd | SwitchMask;
		nop();
		nop();
		nop();
		nop();
		while ((portd & SwitchMask) == 0);
		delay_ms(0);
}

void FCD_Switches0_WaitUntilLow(char WhichSwitch)
{
		char SwitchMask = 0;
		Switches133286_GetMask(WhichSwitch, SwitchMask);

		trisd = trisd | SwitchMask;
		nop();
		nop();
		nop();
		nop();
		while ((portd & SwitchMask) != 0);
		delay_ms(0);
}

//Macro implementations

void FCM_pintarADC()
{
	
	//Decision
	//Decisión: ADC2 < 100?
	if (FCV_ADC2 < 100)
	{
		//Call Macro
		//Llamar a Macro de Componente: LCDDisplay(0)::PrintASCII(' ')
		FCD_LCDDisplay0_PrintASCII(' ');


		//Decision
		//Decisión: ADC2 < 10?
		if (FCV_ADC2 < 10)
		{
			//Call Macro
			//Llamar a Macro de Componente: LCDDisplay(0)::PrintASCII(' ')
			FCD_LCDDisplay0_PrintASCII(' ');


		}


	}


	//Call Macro
	//Llamar a Macro de Componente: LCDDisplay(0)::PrintNumber(ADC2)
	FCD_LCDDisplay0_PrintNumber(FCV_ADC2);


}

//Implementaciones suplementarias


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


	//Inicialización del Código de Interrupción
	option_reg = 0xC0;


	//Llamar a Macro de Componente
	//Llamar a Macro de Componente: LCDDisplay(0)::Start
	FCD_LCDDisplay0_Start();


	//Bucle
	//Bucle: Mientras 1
	while (1)
	{
		//Llamar a Macro de Componente
		//Llamar a Macro de Componente: Switches(0)::WaitUntilHigh(0)
		FCD_Switches0_WaitUntilHigh(0);


		//Llamar a Macro de Componente
		//Llamar a Macro de Componente: ADC(0)::SampleADC
		FCD_ADC0_SampleADC();


		//Demora
		//Demora: 200 ms
		delay_ms(200);


		//Llamar a Macro de Componente
		//Llamar a Macro de Componente: ADC=ADC(0)::ReadAsInt
		FCV_ADC = FCD_ADC0_ReadAsInt();


		//Llamar a Macro de Componente
		//Llamar a Macro de Componente: LCDDisplay(0)::PrintNumber(ADC)
		FCD_LCDDisplay0_PrintNumber(FCV_ADC);


		//Demora
		//Demora: 200 ms
		delay_ms(200);


		//Llamar a Macro de Componente
		//Llamar a Macro de Componente: LCDDisplay(0)::Clear
		FCD_LCDDisplay0_Clear();


		//Llamar a Macro de Componente
		//Llamar a Macro de Componente: Switches(0)::WaitUntilLow(0)
		FCD_Switches0_WaitUntilLow(0);


	}


	mainendloop: goto mainendloop;
}

void interrupt(void)
{
}



