This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: roger, imaqine, 99jonathan

Active User
Posts: 131
Joined: Tue Jul 20, 2010 2:01 pm


Postby s.frings » Sun Mar 06, 2011 9:33 am

I just used the PCF8591 successfully with a Lejos snapshot from November 2010. Maybe somebody is interested in the source:

Code: Select all

import lejos.nxt.I2CPort;

 * Access to a PCF8591 chip, which is an analog port extension via I2C.
 * @author Stefan Frings
public class PCF8591 {
    /** Port where the chip is attached to */
    private final I2CPort port;
    /** Device address of the chip */
    private final int address;

    /** Buffer for i/o */
    private byte buffer[]={0,0,0,0,0};

    /** Operational mode */
    private byte mode;

    /** Standard mode: four single-ended inputs */
    public static final byte MODE_STANDARD=0;

    /** Three differential mode: input 3 gets subtracted from input 0,1 and 2 */
    public static final byte MODE_THREE_DIFFERENTIAL=1;

    /** Mixed mode: input 0 and 1 are single-ended inputs, input 3 gets subtracted from input 2 */
    public static final byte MODE_MIXED=2;

    /** Dual differential mode: input 1 is subtracted from input 0, input 3 is subtracted fom input 2 */
    public static final byte MODE_DUAL_DIFFENRENTIAL=3;

     * Constructor
     * @param i2cPort Port wheere the chip is attached to (e.g. SensorPort.S1)
     * @param deviceAddress Device address of the chip (e.g. 0x90)
     * @param mode Operational mode of the analog inputs, see MODE_XXX constants
    public PCF8591(I2CPort i2cPort, int deviceAddress, byte mode) {

     * Write a byte to the analog output
     * @param value Data to be written
    public void write(byte value) {
        buffer[0]=(byte) (0x40+(mode<<4));

     * Read from a single analog input
     * @param channel Channel number (0-3)
     * @return Data from the A/D converter
    public byte read(byte channel) {
        buffer[0]=(byte) (0x40+(mode<<4)+channel);
        return buffer[1];

     * Read from all analog inputs
     * @return Data from the A/D converter, 4 bytes
    public byte[] read() {
        buffer[0]=(byte) (0x44+(mode<<4));
        // copy and shift buffered data because the first byte is not useable
        byte[] result=new byte[4];
        return result;


Return to “NXJ Hardware”

Who is online

Users browsing this forum: No registered users and 2 guests