/***************************************************************************
 *   Copyright (C) 30.4.2004 by                                           *
 *   Matthias Kranz (matthias.kranz@ifi.lmu.de)                            *
 *   Paul Holleis (paul.holleis@ifi.lmu.de)                                *
 *                                                                         *
 *   header file for standard SmartITs definitions.                        *
 *                                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 **************************************************************************/
#ifndef __I2C
	#include <i2c.h>
#endif
/******************************************************************************
 * function signatures
 *****************************************************************************/
// I2C FUNCTIONS
void init_ds1621(BYTE id);
BYTE read_temp_ds1621(BYTE id);
BYTE get_temperature();

/******************************************************************************
 * structs
 *****************************************************************************/

/******************************************************************************
 * global variables
 *****************************************************************************/


/******************************************************************************
 * Code
 *****************************************************************************/
/*
 * Updates id function...(whatever that means)
 */
void init_ds1621(BYTE id)
{	
	id<<1;
  i2c_start();
  i2c_write(0x90+(2*id));
  i2c_write(0xac);
  i2c_write(9);
  i2c_stop();
  //delay_ms(11);
}

/*
 * Reads out temperature sensor. Make sure to initialize it first (using method init_ds1621(BYTE id)).
 * \returns temperature
 */
BYTE read_temp_ds1621(BYTE id)
{        
	BYTE datah,datal;
   
	id<<1;
  i2c_start();
	i2c_write(0x90+(2*id));
	i2c_write(0xee);
	i2c_stop();

	delay_ms(1000);// 1 Sekunde
	//restart_wdt();
	i2c_start();
	i2c_write(0x90+(2*id));
	i2c_write(0xaa);
	i2c_start();
  i2c_write(0x91+(2*id));
  datah=i2c_read();
  datal=i2c_read(0);
  i2c_stop();

  return(datah);
}
/*
 * Reads out temperature sensor. Make sure to initialize it first (using method init_ds1621(BYTE id)).
 * Takes about 1 second ...
 * \returns temperature
 */
BYTE get_temperature()
{
	BYTE tmp = 0;
	init_ds1621(6);
	delay_ms(10);
	tmp = read_temp_ds1621(6);
	return tmp;
}
