//************************************************************************************
//**  
//**  File name:     C:\Documents and Settings\khaled\Desktop\my motion sensor program1.c
//**  Generated by:  Flowcode v3.4.7.48
//**  Date:          Sunday, November 09, 2008 13:57:02
//**  Licence:       
//**  Registered to: khaled
//**  
//**  
//**     NOT FOR COMMERCIAL USE
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Defines for microcontroller
#define P16F877
#define MX_EE
#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

//Functions
#include <system.h>
#pragma CLOCK_FREQ 8000000

//Configuration data
#pragma DATA 0x2007, 0x3f3a

//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V3\FCD\internals.h"

//Macro function declarations


//Variable declarations
char FCV_ECHO_LOOP;
short FCV_DISTANCE;
char FCV_ECHO;
char FCV_TMR1H;
char FCV_TMR1L;



//Supplementary defines


//Macro implementations

//Supplementary implementations


void main()
{
	
	//Initialisation
	adcon1 = 0x07;


	//Interrupt initialisation code
	option_reg = 0xC0;


	//Loop
	//Loop: While 1
	while (1)
	{
		//Delay
		//Delay: 250 ms
		delay_ms(250);


		//C Code
		//C Code:
		
		tmr1h = 0 ;
		tmr1l = 0 ;


		//C Code
		//C Code:
		tmr1h = FCV_TMR1H ;
		tmr1l = FCV_TMR1L ;


		//Output
		//Output: 0xff -> C3
		trisc = trisc & 0xf7;
		if (0xff)
			portc = (portc & 0xf7) | 0x08;
		else
			portc = portc & 0xf7;


		//C Code
		//C Code:
		
		delay_10us(2); //Delay for 20 microseconds


		//Input
		//Input: C0 -> Echo
		trisc = trisc | 0x01;
		FCV_ECHO = ((portc & 0x01) == 0x01);


		//C Code
		//C Code:
		t1con=0x01;


		//Connection Point
		//Connection Point: A
FCC_Main_A:


		//Decision
		//Decision: Echo = 0?
		if (FCV_ECHO == 0)
		{
		} else {
			//Goto Connection Point
			//Goto Connection Point: A
			goto FCC_Main_A;


		}


		//C Code
		//C Code:
		t1con=0x00;
		FCV_TMR1H=tmr1h;
		FCV_TMR1L=tmr1l;



		//Calculation
		//Calculation:
		//  Distance = ( ( TMR1H << 8 ) + TMR1L ) / 58
		FCV_DISTANCE = ( ( FCV_TMR1H << 8 ) + FCV_TMR1L ) / 58;
		

		//Output
		//Output: 0 -> C3
		trisc = trisc & 0xf7;
		if (0)
			portc = (portc & 0xf7) | 0x08;
		else
			portc = portc & 0xf7;


		//Delay
		//Delay: 250 ms
		delay_ms(250);


	}


	mainendloop: goto mainendloop;
}

void interrupt(void)
{
}



