////// 1wire.c function //////

/***********************1Wire Class***********************/
/*Description: This class handles all communication */
/* between the processor and the 1wire sensors.*/
/* 
/*********************************************************/

/*******************1-wire communication functions********************/
void onewire_reset(unsigned int8 pin_id) 	// OK if just using a single permanently connected device
{
	output_low(pin_id);		// pull 1-wire low for reset pulse
	delay_us( 500 ); 			
	output_float(pin_id); 	// float 1-wire high
	delay_us( 500 ); 			// wait-out remaining initialisation window.
	output_float(pin_id);
}

/*********************** onewire_write() ********************************/
/*This function writes a byte to the sensor.*/
/* */
/*Parameters: byte - the byte to be written to the 1-wire,pin_id */
/*Returns: */
/*********************************************************************/

void onewire_write(unsigned int data, unsigned int pin_id)
{
int count;
int pin_bit;					
unsigned int port_data;
   pin_bit=pin_id%8;				//PIN bit number within Port
   port_data=INPUT_B()&(~(1<<pin_bit)); // original data of Port and pin_bit set to 0
   for (count=0; count<8; ++count)
    {
    output_low(pin_id);			// pull 1-wire low to initiate write time-slot.
//  delay_us( 2 );			//orginal, not needed
    OUTPUT_B(port_data|((data&0b00000001)<<pin_bit)); //pin_bit. of PORT set to 0/1 and PORt write
    data=data>>1;
    delay_us( 60 );			//wait until end of write slot
    output_float(pin_id); 		// set 1-wire high again,
    delay_us( 2 ); 			// for more than 1us minimum.
    }
}

/*********************** read1wire() *********************************/
/*This function reads the 8 -bit data via the 1-wire sensor. */
/* */
/*Parameters: */
/*Returns: 8-bit (1-byte) data from sensor */
/*********************************************************************/

int onewire_read(unsigned int pin_id)
{
unsigned int pin_mask, count, pin_bit, data;	

   pin_bit=pin_id%8;			//PIN bit number within Port
   pin_mask=1<<pin_bit;			//pin mask within Port

   for (count=0; count<8; ++count)
    {
	output_low(pin_id);		// pull 1-wire low to initiate a time-slot.
//	delay_us( 2 );					
//	output_float(pin_id); 		// now let 1-wire float high,
	delay_us( 8 ); 			// let device state stabilise
	shift_right(&data,1,((input_b()&pin_mask)>>pin_bit));	//pin_bit of read Port data masked and moved to actual bit of data 
	delay_us( 120 );
    }
   return( data );
}

float ds1820_read(unsigned int pin_id)
{
 int busy=0, temp1, temp2;
 signed int16 temp3;
 float result;

 onewire_reset(pin_id);
 onewire_write(0xCC,pin_id);
 onewire_write(0x44,pin_id);

 while (busy == 0) busy = onewire_read(pin_id);		

 onewire_reset(pin_id);
 onewire_write(0xCC,pin_id);
 onewire_write(0xBE,pin_id);

 temp1 = onewire_read(pin_id);
 temp2 = onewire_read(pin_id);
 temp3 = make16(temp2, temp1);

 result = (float) temp3 / 2.0;   //Calculation for DS18S20 with 0.5 deg C resolution
// result = (float) temp3 / 16.0;  //Calculation for DS18B20 with 0.1 deg C resolution
 
 delay_ms(200);
 return(result);
}

void ds1820_init(unsigned int pin_id)
   {
   //for 10 bit resolution mod it!

   onewire_write(0xCC,pin_id);	//skip ROM command 
   onewire_write(0x4E,pin_id);

   onewire_write(125,pin_id);	// set max TH threshold (125'C)
   onewire_write(-55,pin_id); 	// set min TL threshold (-55'C), this should be done for proper working of DS18B20
   onewire_write(127,pin_id); 	//// Temp resolution, set to nbit

   onewire_reset(pin_id);
   onewire_write(0xCC,pin_id);	//skip ROM command 
   onewire_write(0x48,pin_id);  // 
   delay_ms(15);
    }
