/****************************************************************************
 Title	:	TERMINAL FOR VISUALIZATION AND CONTROL CONDITIONS IN HOUSE
 Author	:	Patrik Szalay <xszala01@stud.feec.vutbr.cz>
 File	:	lm75.c
 Software:	CodeWarrior Development Studio Version: 10.6 Build Id:140329
 Target	:	MC9S08LH64

 DESCRIPTION
       Code for the LM75 temperature sensor

 USAGE
       
*****************************************************************************/
#include "lm75.h"

void lm75_set_conf(ADDRESS addr, unsigned char ConfR, char* data){
	data[0] = LM75_CONF_REG;
	data[1] = ConfR;
	IICTransmit(addr,2,data);
}
#ifdef __NO_FLOAT__
decimal lm75_readTemp_decimal(ADDRESS addr, char* data){
	data[0] = LM75_TEMP_REG;
	IICTransmit(addr,1,data);
	delay(500);
	IICReceive(addr,2,data);
	if(data[0] & data[1] == 0xFF) return IIC_INVALID_DATA;
	if ((data[0] & LM75_TEMP_SIGN_MASK) == LM75_TEMP_SIGN_MASK){
		data[0] &= ~LM75_TEMP_SIGN_MASK;
		return (decimal)((((unsigned long int)(data[0]<<3 | (data[1]>>5)) * 125)) - 128000)/10;
	}else{		
		return (decimal)((((unsigned long int)(data[0]<<3 | data[1]>>5)) * 125)/10);
	}
}
char lm75_readTemp_char(ADDRESS addr, char* data){
	return (char) (lm75_readTemp_decimal(addr, data)/100);
}

#else
float lm75_readTemp_float(unsigned char addr){
	int i = 0;
	data[0] = LM75_TEMP_REG;
	IICTransmit(addr,1,data);
	for(i = 0; i < 1000; i++ );
	IICReceive(addr,2,data);
	if(data[0] & data[1] == 0xFF) return 9999;
	if ((data[0] & LM75_TEMP_SIGN_MASK) == LM75_TEMP_SIGN_MASK){
		data[0] &= ~LM75_TEMP_SIGN_MASK;
		return (float)(((unsigned int)(data[0]<<3 | (data[1]>>5)) * 0.125)) - 128.0;
	}else{		
		return (float)(((unsigned int)(data[0]<<3 | data[1]>>5)) * 0.125);
	}
 }
char lm75_readTemp_char(unsigned char addr){
	return (char) lm75_readTemp_float(addr);
}
#endif

unsigned char lm75_readConfig(ADDRESS addr, char* data){
	data[0] = LM75_CONF_REG;
	IICTransmit(addr,1,data);
	delay(1000);
	IICReceive(addr,1,data);
	return data[0];
}
