problem with PCF8591

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

Moderators: 99jonathan, roger, imaqine

problem with PCF8591

Postby Soso » Sat May 07, 2011 10:14 am

Hi

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 :)
Soso
New User
 
Posts: 6
Joined: Sat May 07, 2011 9:56 am

Re: problem with PCF8591

Postby gloomyandy » Sat May 07, 2011 12:06 pm

Hi,
Did you by any chance get this code from this thread:
viewtopic.php?t=1392&highlight=pcf8591
Did you notice my comment at the end of the thread? Well things have changed. The i2c interface now requires that you call the i2cComplete method at the end of transaction. See this source file for some of the details:
http://lejos.svn.sourceforge.net/viewvc ... iew=markup

So your code should probably look something like this:
Code: Select all
   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.i2cComplete(null, 0);
      if (ret != 0) return intResult;
      ret= I2Cport.i2cStart(address, 0x00, 0, null, 2, 0);
      if (ret!=0)
         return intResult;
      while (I2Cport.i2cBusy()!=0){
         Thread.yield();
         
      }
      ret = I2Cport.i2cComplete(rxData, 2);
      if (ret != 0) return intResult;
      return intResult=(0xFF & rxData[1]);
     
   }


Note I have not tested the above code so you may have to make some changes. If you are still having problems you should modify the code to display the return values from the i2c calls and post the results here.

Also if you are still having problems you may want to verify that your hardware is working correctly by testing it with NXC or some other system...

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

Re: problem with PCF8591

Postby Soso » Sat May 07, 2011 4:48 pm

thanks Andy
but now the result is always -1
I use this kit with my analog sensor
http://www.mindsensors.com/index.php?mo ... PAGE_id=92

I think my problem is in setting the address and channel because this is the first time for me to deal with hardware

can you help me ?
Soso
New User
 
Posts: 6
Joined: Sat May 07, 2011 9:56 am

Re: problem with PCF8591

Postby gloomyandy » Sat May 07, 2011 5:56 pm

So have you tried doing what I suggested and printed out the return codes from the individual operations? What results did you get?

Have you tried testing the kit using the standard Lego software or NXC? It is probably worth doing that, there may be some problem with the actual device...

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

Re: problem with PCF8591

Postby Soso » Sat May 07, 2011 9:46 pm

I made the modifications then the values of the first and second calls are printed on the screen as follow
ret=0.0
ret2=-3.0
but instead of ret3 value -1.0 is displayed

Code: Select all
 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);
         String s=new Float(ret).toString();
         LCD.drawString("ret=", 0, 1);
        LCD.drawString(s, 0, 2);
         if (ret!=0)return intResult;
         while (I2Cport.i2cBusy()!=0){
            Thread.yield();
         }
         ret = I2Cport.i2cComplete(null, 0);
          s=new Float(ret).toString();
         LCD.drawString("ret2=", 0, 3);
        LCD.drawString(s, 0, 4);
         if (ret != 0) return intResult;
         ret= I2Cport.i2cStart(address, 0x00, 0, null, 2, 0);
         s=new Float(ret).toString();
         LCD.drawString("ret3=", 0, 1);
        LCD.drawString(s, 0, 2);
         if (ret!=0)
            return intResult;
         while (I2Cport.i2cBusy()!=0){
            Thread.yield();
            
         }
         ret = I2Cport.i2cComplete(rxData, 2);
         s=new Float(ret).toString();
         LCD.drawString("ret4=", 0, 1);
        LCD.drawString(s, 0, 2);
         if (ret != 0) return intResult;
         return intResult=(0xFF & rxData[1]);
        
      }
   
Soso
New User
 
Posts: 6
Joined: Sat May 07, 2011 9:56 am

Re: problem with PCF8591

Postby gloomyandy » Sat May 07, 2011 10:20 pm

The -3 return indicates that you are not talking to the device. Are you sure you have the correct address? What do you have the address jumper settings set to? You may want to try all of the 7 possible addresses that the board can have. The possible range is 0x48.. 0x4f.

If that does not help then as I suggested earlier I think you need to test the hardware using the Lego firmware and standard software.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4113
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: problem with PCF8591

Postby Soso » Mon May 09, 2011 6:41 am

I still have a problem I tested the hardware it works well with lab view
when I try address 0x48 the result is different
ret4 is displayed first with value =2.0
ret2=0.0
after that -1.0 is displayed
I tried all possible addresses and all possible channels
Soso
New User
 
Posts: 6
Joined: Sat May 07, 2011 9:56 am

Re: problem with PCF8591

Postby gloomyandy » Mon May 09, 2011 7:30 am

If ret4 is returning 2 that means it is working, you need to check the API spec for i2cComplete it returns the number of bytes read so if it is >= 0 then it has worked.
http://lejos.sourceforge.net/nxt/nxj/ap ... plete(byte[], int)


Change your final test to be if (ret != 2) rather than if (ret != 0) and try it again with an address of 0x48...

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

Re: problem with PCF8591

Postby Soso » Wed May 11, 2011 5:02 pm

Hi,
Thank you gloomyandy for your time and appreciate your help. :)
Finally, it works after changing the internal register from 0 to 2 or to 4.
I have a question: what is the best value for internal register?
Soso
New User
 
Posts: 6
Joined: Sat May 07, 2011 9:56 am

Re: problem with PCF8591

Postby gloomyandy » Wed May 11, 2011 9:09 pm

Hi,
Can you explain what you mean by "the internal register", perhaps you could repost your modified code and give an example of how you are calling it?

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

Re: problem with PCF8591

Postby Soso » Mon May 16, 2011 10:38 pm

sorry for delay but I was busy the last few days
I mean by the internal register the third argument in this line
ret= I2Cport.i2cStart(address, 0x00, 4, null, 2, 0);
Soso
New User
 
Posts: 6
Joined: Sat May 07, 2011 9:56 am

Re: problem with PCF8591

Postby gloomyandy » Mon May 16, 2011 11:34 pm

That is not the internal register is is the length of the internal register address field. By setting it to 4 your are telling the i2c code to send four bytes all of which are zero to the device when you issue the read. I don't really see why you need to set that value. I'm sure that others that have used the same device that you have do not have that value set.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4113
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff