
void read_pcf8591( uchar *adrdbuff, uchar adch)
{

	I2CON=0x40;		//Initializing for 94KHz
	I2CON|=0x20;		//Send a start.
	while(!(I2CON&0x08));	//Wait for SI


	I2CON&=0xDF;		//Clear start bit.
	I2DAT=0x90;		//Slave address 000 and write.
	I2CON&=0xF7;	        //Clear the SI Flag.
	while(!(I2CON&0x08));	//Wait for slave address to be sent.


	I2DAT=adch;	        //Send the ADC channel number.
	I2CON&=0xF7;		//Clear SI bit.
	while(!(I2CON&0x08));	//Wait for sending ADC channel.


	I2CON&=0xF7;		//Clear SI bit.
	I2CON|=0x10;		//Send Stop condition.
	while(I2CON&0x10);	//Wait for stop condition to be sent.

	I2CON=0x40;
	I2CON|=0x20;
	while(!(I2CON&0x08));	//Wait for start sending.

	I2CON&=0xDF;		//Clear Start bit.

	I2DAT=0x91;	        //Slave address 000 and read.
	I2CON&=0xF7;		//Clear SI bit.
	while(!(I2CON&0x08));	//Wait for slave address to be 
sent.

// First Reading Dummy read to clear old value.

	I2CON|=0x04;	        //Set Auto Acknowledge.
	I2CON&=0xF7;		//Clear SI bit.
	while(!(I2CON&0x08));	//Wait for ADC value to be received.
	*adrdbuff=I2DAT;	//Store the ADC value.

		
	I2CON&=0xFB;		//Clear Auto Acknowledge.
	I2CON&=0xF7;	        //Clear SI.
	while(!(I2CON&0x08));	//Wait for ADC value to be received.
	*adrdbuff=I2DAT;	//Store the ADC value.

	I2CON&=0xF7;     	//clear SI bit.
	I2CON|=0x10;		//Send stop Condition.
	while(I2CON&0x10);	//Wait for stop condition to be sent.

}
