#include <p18f4620.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <FSIO.h>
#include <string.h>
#include "delay.h"
#include "delay.c"
#include <adc.h>

#define STRT PORTBbits.RB0


void main();
void initPIC();
void GetSensor(int *SData);
void display (unsigned int c);
void SD();

int SData[3];
unsigned char a;


void main()
{
	unsigned int one;
	unsigned int ten;
	unsigned int hundreds;
	unsigned int thousands;
	unsigned int Data;
	unsigned int Threshold=1300;
	unsigned long x;
	unsigned long x2;
	unsigned long y;
	unsigned long y2;
	unsigned long z;
	unsigned long z2;
	unsigned int calculated_acceleration;


	initPIC();

	while(1)
	{
	//	GetSensor(SData);

		SD();
		Data=SData[0]/10;
		one=SData[0]%10;
		ten=Data%10;
		Data=Data/10;
		hundreds=Data%10;
		Data=Data/10;
		thousands=Data%10;
		//display(0x30+thousands);
		//display(0x30+hundreds);
		//display(0x30+ten);
		//display(0x30+one);
		//display(0x20);		
		Data=SData[1]/10;
		one=SData[1]%10;
		ten=Data%10;
		Data=Data/10;
		hundreds=Data%10;
		Data=Data/10;
		thousands=Data%10;
		//display(0x30+thousands);
		//display(0x30+hundreds);
		//display(0x30+ten);
		//display(0x30+one);
		//display(0x20);					// Space
		Data=SData[2]/10;
		one=SData[2]%10;
		ten=Data%10;
		Data=Data/10;
		hundreds=Data%10;
		Data=Data/10;
		thousands=Data%10;
		//display(0x30+thousands);
		//display(0x30+hundreds);
		//display(0x30+ten);
		//display(0x30+one);
		//display(0x0a);	
	
		

		/*x=SData[0];
		x2=x*x;
		y=SData[1];
		y2=y*y;
		z=SData[2];
		z2=z*z;
		calculated_acceleration=(unsigned int)ceil(sqrt((x2+y2+z2)));
		if(calculated_acceleration >= Threshold)
		{
			while(1)
			{
				PORTDbits.RD1=1;
			}
		}
		*/

	}
}


	



void display(unsigned int c)
{
	while (PIR1bits.TXIF == 0);
	TXREG = c;
}

void initPIC()
{
	SPBRG=129;
	TXSTAbits.BRGH=1;
	TXSTAbits.TXEN=1;
	RCSTAbits.CREN=1;
	RCSTAbits.SPEN=1;

	TRISE=0b00000111;
	TRISA=0b00101111;
	TRISD=0;
	PIE1bits.ADIE=1;
	ADCON2=0x81;
	ADCON1=0x1A;
	ADCON0=0x01;
	ADRESL=0;
	ADRESH=0;
	PORTD=0;
}

void GetSensor(int *SData)
{
	unsigned int temp;
	ADCON0bits.CHS3=0;ADCON0bits.CHS2=0;ADCON0bits.CHS1=0;ADCON0bits.CHS0=0;
	DelayUs(20);
	ADCON0bits.GO=1;
	DelayUs(20);
	if(PIR1bits.ADIF)
	{
		SData[0]=ADRESL;
		temp=ADRESH*0b100000000;
		SData[0]+=temp;
	}
	
	
	ADCON0bits.CHS3=0;ADCON0bits.CHS2=0;ADCON0bits.CHS1=0;ADCON0bits.CHS0=1;
	DelayUs(20);
	ADCON0bits.GO=1;
	DelayUs(20);
	if(PIR1bits.ADIF)
	{
		SData[1]=ADRESL;
		temp=ADRESH*0b100000000;
		SData[1]+=temp;
	}

	ADCON0bits.CHS3=0;ADCON0bits.CHS2=0;ADCON0bits.CHS1=1;ADCON0bits.CHS0=0;
	DelayUs(20);
	ADCON0bits.GO=1;
	DelayUs(20);
	if(PIR1bits.ADIF)
	{
		SData[2]=ADRESL;
		temp=ADRESH*0b100000000;
		SData[2]+=temp;
	}
}

void SD()
{

	FSFILE*pntr;
	int SData[3];

	TRISA=1;
	TRISB=1;

	while(STRT);
	while(!STRT);

	while(!FSInit());

	PORTDbits.RD0=1;

	pntr=FSfopenpgm("XYZ.TXT","w+");

	GetSensor(SData);


	FSfprintf(pntr,"%d,%d,%d\n",SData[0],SData[1],SData[2]);

 
	//if(FSfwrite((int)Data,1,1000,MyFile)!=1000)while(1);
	
	if(STRT==0)
	{
		FSfclose(pntr);
		while(1);
	
	
	}
	


}