//SDA_D is direction port: 0: output 	1: input

data = read_dac_i2c(0xD0);		// Always 0xFFFF

unsigned short read_dac_i2c(unsigned char cmd_byte)
{
	unsigned short data;
	unsigned char address_byte;
	unsigned char timeout;
	unsigned char pos;
	unsigned char dmy;
	
	
	address_byte = 0x90;
	
	// Issue start condition
	set_i2c_start();
	
	// Send address byte
	send_byte(address_byte);
	
	wait_n_mc(20);	
	
	//Send command byte
	send_byte(cmd_byte);
	
	SDA_D = 1;
	wait_n_mc(50);	
	// Start repeat condition
	set_i2c_start();
	
	// Send address byte for receive
	address_byte = 0x91;
	send_byte(address_byte);
	
	wait_n_mc(20);	
	
	// Read the 1st byte data
	dmy = read_byte();
	data = dmy;
	data <<= 8;
	
	wait_n_mc(20);	
	
	// Read the 2nd byte data
	dmy = read_byte();
	data |= dmy;

	// Issue stop condition
	set_i2c_stop();

	return data;
}

void send_byte(unsigned char byte)
{
	unsigned char timeout;
	unsigned char pos;	
	
	pos = 0x80;
	while(pos){
		SCL = 0;			// SCL is set to Low
		SCL_D = 0;					
		if(pos & byte){
			SDA_D = 1;		// SDA is set to High
		}
		else{
			SDA = 0;		// SDA is set to Low
			SDA_D = 0;
		}
		wait_n_mc(SCL_LOW2);
		pos = pos >> 1;
		SCL_D = 1;			// SCL is set to High
		wait_n_mc(SCL_HIGH2);		
	}
	SCL = 0;				// SCL is set to Low
	SCL_D = 0;
	wait_n_mc(SCL_LOW2);	
	
	// ACK confirmation
	SDA_D = 1;				// SDA is set to Input	
	SCL_D = 1;				// SCL is set to Input	
	wait_n_mc(SCL_HIGH2);	
	timeout = 0;
	while(SDA == 1){
		timeout++;
		if(timeout > 1){
			return 0;
		}
	}	
}

unsigned char read_byte()
{
	unsigned char dmy;
	unsigned char pos;
	
	dmy = 0;
	pos = 0x80;	
	while(pos){
		SDA_D = 1;
		SCL_D = 1;
		wait_n_mc(SCL_HIGH2);
		if(SDA){
			dmy |= pos;
		}
		pos >>= 1;
		SCL = 0;
		SCL_D = 0;
		wait_n_mc(SCL_LOW2);
	}
	send_i2c_ack();						// Send ACK	
	
	return dmy;
}

