PCF8591

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

PCF8591

Postby Thibaud » Mon Mar 09, 2009 1:55 pm

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
Thibaud
New User
 
Posts: 21
Joined: Thu Mar 05, 2009 1:28 pm

Postby enzomango » Mon Mar 09, 2009 9:26 pm

Here's some code that worked well for me:

Code: Select all
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
   }
}
enzomango
Novice
 
Posts: 30
Joined: Tue Feb 26, 2008 5:20 pm

Postby Thibaud » Tue Mar 10, 2009 4:08 pm

Thanks this works fine
Thibaud
New User
 
Posts: 21
Joined: Thu Mar 05, 2009 1:28 pm

Postby gloomyandy » Tue Mar 10, 2009 4:34 pm

Folks,
Just a heads up. In the next release of leJOS we have re-worked the I2C code a little (and improved it a lot), this means that the lowlevel API has changed so you may have to change your code to work with the new release (no problem until then though)....

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3946
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff