Hi
How to tell which channel we want to read on a PCF8591? the datasheet says that we need to send the adress of the device following by the Byte. But i don't understand how can i do???
Thanks
Moderators: roger, 99jonathan, imaqine
import lejos.nxt.*;
/**
* This class is used to communicate with a PCF8591 8-bit A/D and D/A converter.
*
*/
public class PCF8591 extends I2CSensor {
/**
* Returns the sensor port.
*/
private I2CPort I2Cport;
/**
* Returns the sensor address.
* Values range from 0x48 - 0x4F.
*/
private byte address;
/**
* Initializes A/D Converter.
* @param sensorPort NXT sensor port.
* @param address sensor address. Values range from 0x48 - 0x4F.
*
*/
public PCF8591(I2CPort sensorPort, byte sensorAddress) {
super(sensorPort);
I2Cport = sensorPort;
address = sensorAddress;
}
/**
* Method for retrieving the data from all four analog input channels.
* Only works if auto-incremented channel selection is turned on (bit 3).
* @param control sensor control byte. 0x04 (bit 3) turns on auto-incremented channel selection.
* @return array containing value of each analog input channel (CH0-CH3). Values range from 0 to 255;
*/
public int[] getAllChannelValues(int control) {
byte[] txData = {(byte)control};
byte[] rxData = {0x00,0x00,0x00,0x00,0x00};
int[] intResult = {-1,-1,-1,-1};
int autoset = (byte)0x04 & (byte)control;
if ( autoset > 0 ) {
int 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, 5, 0);
if (ret != 0) return intResult;
while (I2Cport.i2cBusy() != 0) {
Thread.yield();
}
for (int x=1;x<=4;x++) {
intResult[x-1] = (0xFF & rxData[x]);
}
return intResult;
}
return intResult;
}
/**
* Method for retrieving the value from one analog input channels.
* @param control sensor control byte. First two bits select channel. 0x00 channel 0, 0x01 channel 1, etc.
* @return value of analog input channel (CH0-CH3). Values range from 0 to 255;
*/
public int getChannelValue(byte control) {
byte[] txData = {(byte)control};
byte[] rxData = {0x00,0x00};
int intResult = -1;
int 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]); //second byte
}
}
Users browsing this forum: No registered users and 6 guests