
#include "stdio.h"
#include "p18f45k20.h"
#include "i2c.h"
#include "delays.h"
#include "usart.h"
#include "adc.h"
#include "usart1.h"
#include "string.h"




//---------------------------------------------------------------------
// konstansok/definiciok 
//---------------------------------------------------------------------

#define PASS	1
#define FAIL	0

//config bits
#pragma config FOSC = HS, FCMEN = OFF, IESO = OFF                       // CONFIG1H
#pragma config PWRT = OFF, BOREN = OFF, BORV = 30                        // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                    // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF							      // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF                // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                            // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF 


#ifdef __DEBUG
#pragma config DEBUG = ON
#else
#pragma config DEBUG = OFF
#endif

#ifndef	__USART1_H
#define	__USART1_H
#endif

// I2C orajel beallitas
#define SYSCLK		10E6
#define I2C_FREQ	100E3
#define I2C_SSPADD	(SYSCLK/(I2C_FREQ*4))-1

// Homero
#define DEVICE_ADDR	0x90
	
//I2C labak
#define SCL	LATCbits.LATC3
#define SDA	LATCbits.LATC4

//---------------------------------------------------------------------
// Prototipusok
//---------------------------------------------------------------------
unsigned char WriteI2C(unsigned char data_out );
//unsigned char putcI2C(unsigned char data_out );
unsigned char WriteRegister(char oAddress, char oData);
unsigned int  ReadRegister(char oAddress, int* oData);

//---------------------------------------------------------------------
// global valtozok
//---------------------------------------------------------------------
//unsigned char arraywr[] = {1,2,3,4,5,6,7,8,0};
//unsigned char arrayrd[20];
unsigned int a,q,iTemp=0;
unsigned char w;

float T=0;
int d1=0;
int d2=0;
//d3=0;
//float f2=0;
//float f3=0;


//---------------------------------------------------------------------
// fuggvenyek
//---------------------------------------------------------------------

void main(void)
{
	char oCount;
	int iData;
//	char 	pszText[64]= {"hello"};
	char	pszText[100];
	int i;

	InitPic();
	SetupUSART();


	CloseI2C();
//	TRISDbits.TRISD0 = 0;	//Led
//	PORTC=0; 
	TRISC = 0xF7; //11110111
	SCL=0;	//Delay1KTCYx(10);
	SCL=1;	SCL=0;	SCL=1;
	OpenI2C(MASTER, SLEW_ON);	// Initialize I2C module
	SSPADD = I2C_SSPADD;		//define-ben beallitjuk a boud reta-et
	//AckI2C();				// A beragadas ellen ved
	//StopI2C();
	
	//for (i=0;i<32;i++)
	//pszText[i]=0;



	while(1)
	{
		if (WriteRegister(0x01,0xFE)==PASS)	//meghivodik es lefut a write is
		{
			//varunk a konverzio befejezesere
			iData=0xff;
			while(iData&0x80) ReadRegister(0x01,&iData);	//iData cimen lévő ertekre hivatkozik, 0-ig olvas

			// majd kiolvassuk az eredmenyt
			ReadRegister(0x00,&iTemp);	//0x00 a temp regiszter es ezt olvassuk be az iTempbe

			if(iTemp>=0x0000 && iTemp<=0x7FF0)	//pozitiv ertekekre es a 0
				T=(iTemp/0x0010)*0.0625;		//homerseklet konverzio °C-ra (+)
			if(iTemp>=0x8000 && iTemp<=0xFFF0)	//negativ ertekekre
				T=-(((0xFFF0-iTemp)/0x0010)*0.0625);		//homerseklet konverzio °C-ra (-)
		//	for(q=0;q<50000;q++) Nop();
			//Delay1KTCYx(200); //varakozás, a debugolaskor, olvashato legyen
			
 		}
		else
		{
			StopI2C();
			for(w=0; w<2; w++)
			{	LATDbits.LATD0=1;
				for(q=0;q<50000;q++) Nop();
				LATDbits.LATD0=0;
				for(q=0;q<50000;q++) Nop();
			}
		}

		
	for (oCount=0; oCount<16;oCount++)
			{
				WriteLED(oCount);
				//Delay10KTCYx(2*ReadSW()+6);
				//putrsUSART("Hello");
			}
		d1=T; //d1 és d2 int típusúak, T float típusú
		d2=(T-d1)*1000;
		sprintf(pszText,"Homerseklet: %d.%0.3d \r\n",d1,d2);

	
//d1=T;
//f2=T-d1;
//d2=trunc(f2*10000);
//f3=f2*10000-d2;
//d3=trunc(f3*10000);

//sprintf (pszText,"Homerseklet: %d.%04d%04d\n",d1,d2,d3);

//		sprintf(pszText,"Homerseklet: %g ",T);
	
 		putsUSART(pszText);
	}
}


//---------------------------------------------------------------------

unsigned char WriteRegister(char oAddress, char oData)	//Regiszter iras, mibe mit irunk
{								// regiszter	 reg maszk
		char	i;

		IdleI2C();			// varas
		StartI2C();	
		IdleI2C();			// varas
		WriteI2C(DEVICE_ADDR & 0xFE);	//Irasra LSB=0-> ossze ES-eljuk	
									
		IdleI2C();			//varas
		// kaptunk e ACKt vissza
		if (SSPCON2bits.ACKSTAT) {StopI2C();return FAIL;}	//itt nem kell ACK

		// ha igen akkor konfig regiszter beallitasa
		for(i=0; ((i<4&&SSPCON2bits.ACKSTAT)||i==0); i++)
		{
			WriteI2C(oAddress);
			IdleI2C();			//varas
		}
		if (i>3) {StopI2C();return FAIL;}//miert lepett , ha nem jo akkor kilepunk es ezt jelezzuk (FAIL)

		//Config reg maszkolasa
		for(i=0; ((i<4&&SSPCON2bits.ACKSTAT)||i==0); i++)
		{
			WriteI2C(oData);
			IdleI2C();			//varas
		}
		if (i>3) {StopI2C();return FAIL;}
		IdleI2C();			//varas
		StopI2C();
		return PASS;
}

//---------------------------------------------------------------------

unsigned int ReadRegister(char oAddress, int* oData)	//Regiszter olvasas, mit irunk
{							//   regiszter     mibe
		unsigned int felso=0, also=0;
		unsigned int temp=0;


//FRAME1
		IdleI2C();				// varas
		StartI2C();	
		IdleI2C();				// varas
		WriteI2C(DEVICE_ADDR & 0xFE);	// irasra megnyitjuk
		IdleI2C();				//varas
		// kaptunk-e ACK-t vissza
		if (SSPCON2bits.ACKSTAT) {StopI2C();return FAIL;}	


//FRAME2
		WriteI2C(oAddress);		//statusz reg irasa, kivalasztjuk a temp reg-et
		IdleI2C();				//0x00 (2byte)Temp reg,0x10 T-low,0x11 T-high
		if (SSPCON2bits.ACKSTAT) {StopI2C();return FAIL;}	


// a konfig vege, innentől az olvasas
//FRAME3

		RestartI2C();
		IdleI2C();
		WriteI2C(DEVICE_ADDR|0x01);	// olvasasra LSB=1-> VAGY-oljuk, igy biztos olvasas lesz
		IdleI2C();
		if (SSPCON2bits.ACKSTAT) {StopI2C();return FAIL;}	
		IdleI2C();
	

//FRAME4
//1.byte

			also=ReadI2C();
			IdleI2C();
			AckI2C();
			IdleI2C();
//FRAME5
//2.byte
			if (oAddress==0x00){
			felso=ReadI2C();
			IdleI2C();
			also<<=8;
			temp=also|felso;
			IdleI2C();
			AckI2C();
			
			*oData=temp;
			}
			else
			{
				*oData=also;
			}

		StopI2C();
		IdleI2C();

		return PASS;
}

void WriteLED(unsigned char oLED)
{
	DS1_OUT = (oLED&0x01) ? 0:1;
	DS2_OUT = (oLED&0x02) ? 0:1;
	DS3_OUT = (oLED&0x04) ? 0:1;
	DS4_OUT = (oLED&0x08) ? 0:1;
}

void InitPic(void)
{
	// Globális megszakítás tiltása
	INTCONbits.GIEH = 0;

	OSCCON = 0x70;

	// ADC funkciók kikapcsolása
	CloseADC();
	// PORTA bitjeinek digitális módba állítása
//	ADCON1 = 0x7F;

	// PORTC kezdőértékének és adatirányának beállítása
	LATC = INIT_PORTC;
	TRISC = INIT_TRISC; 	// 

	// PORTD kezdőértékének és adatirányának beállítása
	LATD = INIT_PORTD;
	TRISD = INIT_TRISD; 	// output (0),inputs (1)
}

void SetupUSART(void)
{
	// Setup USART
	OpenUSART(	USART_TX_INT_OFF &
				USART_RX_INT_ON &
				USART_ASYNCH_MODE &
				USART_EIGHT_BIT &
				USART_CONT_RX &
				USART_BRGH,
				USART_SPEED	);
	baudUSART (	BAUD_IDLE_CLK_HIGH &
				BAUD_8_BIT_RATE &
				BAUD_WAKEUP_OFF &
				BAUD_IDLE_TX_PIN_STATE_HIGH &
				BAUD_IDLE_RX_PIN_STATE_HIGH &
				BAUD_AUTO_OFF);
	IPR1bits.TXIP = 0;
	IPR1bits.RCIP = 0;
}