I'm trying to make an analog sensor using PCF8591 A/D convertor but I'm facing a problem the result is always zero.
I used this class .
- Code: Select all
import lejos.nxt.*;
public class PCF8591 extends I2CSensor{
private I2CPort I2Cport;
private byte address;
public PCF8591(I2CPort sensorPort, byte sensorAddress){
super(sensorPort);
I2Cport=sensorPort;
address=sensorAddress;
}
public float getChannelValue(byte control)
{
byte[] txData={(byte)control};
byte[] rxData={0x00,0x00};
float intResult=-1;
float ret=I2Cport.i2cStart(address, 0x00, 0, txData, 1, 1);
if (ret!=0)return intResult;
while (I2Cport.i2cBusy()!=0){
Thread.yield();
}
ret= I2Cport.i2cStart(address, 0x00, 0, rxData, 2, 0);
if (ret!=0)
return intResult;
while (I2Cport.i2cBusy()!=0){
Thread.yield();
}
return intResult=(0xFF & rxData[1]);
}
}
And this is the main function
- Code: Select all
import lejos.nxt.*;
public class Detection {
static PCF8591 Val;
public static void main (String[]args){
Val=new PCF8591(SensorPort.S1,(byte) 0x49);
float rslt=Val.getChannelValue((byte)0x30);
String s=new Float(rslt).toString();
LCD.drawString(s, 0, 6);
Button.waitForPress();
}
}
Any help would be appreciated
