NXC - Reading temperature from TPA81

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
salgfrancisco
Posts: 10
Joined: 21 Oct 2011, 15:25

NXC - Reading temperature from TPA81

Post by salgfrancisco »

Hi, I recently got an NXT and I thought that using more sensors connected through i2c would bring a lot of new possibilities. So I got the TPA81 sensor in order to make a robot that would extinguish candles. (TPA81 datasheet here: http://www.robot-electronics.co.uk/htm/tpa81tech.htm ).
I was able to get everything connected in board, however I am not being successful in making the NXT read the sensor. I am programming in NXC and so far I got this:

Code: Select all

// READ TPA81
#define TPA81_PORT IN_1
#define TPA81_ADDR 0xD0


task main()
{
         SetSensorType( TPA81_PORT, IN_TYPE_LOWSPEED );
         SetSensorMode( TPA81_PORT, IN_MODE_RAW );
         ResetSensor( TPA81_PORT );
         

         byte inbuf[10];
         byte nByteReady = 0;
          
        while( 1 )
         {
                 while (I2CStatus( TPA81_PORT, nByteReady ) == STAT_COMM_PENDING);
                 {
                 }

                 I2CRead( TPA81_PORT, 10, inbuf );
                 
                 NumOut( 0, LCD_LINE1, inbuf[3], DRAW_OPT_CLEAR_WHOLE_SCREEN );
         }
}
But after downloading to the brick I keep getting File error! -1 whenever I try to run the program.
I have no idea what this means and would be glad if anyone could tell me where I am failing.
Thanks in advance ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC - Reading temperature from TPA81

Post by mattallen37 »

Try using this function to read the sensor:

Code: Select all

void MyI2CRead(byte port, byte addr, byte reg, byte cnt, byte& outbuf[])
{
  byte cmdbuf[];  // register number holder
	int             loop;
	byte            nByteReady = 0;

  ArrayBuild(cmdbuf, addr, reg);

  loop = STAT_COMM_PENDING;              //Wait until bus is free
  while (loop == STAT_COMM_PENDING ){    //        ''
    loop = I2CStatus(port, nByteReady);  //        ''
  }                                      //        ''

  I2CBytes(port, cmdbuf, cnt, outbuf)    //Read the registers
}
So your program would look more like this:

Code: Select all

// READ TPA81
#define TPA81_PORT IN_1
#define TPA81_ADDR 0xD0

void MyI2CRead(byte port, byte addr, byte reg, byte cnt, byte& outbuf[])
{
  byte cmdbuf[];  // register number holder
	int             loop;
	byte            nByteReady = 0;

  ArrayBuild(cmdbuf, addr, reg);

  loop = STAT_COMM_PENDING;              //Wait until bus is free
  while (loop == STAT_COMM_PENDING ){    //        ''
    loop = I2CStatus(port, nByteReady);  //        ''
  }                                      //        ''

  I2CBytes(port, cmdbuf, cnt, outbuf)    //Read the registers
}

task main()
{
         SetSensorLowspeed(TPA81_PORT);         

         byte inbuf[10];
          
        while( 1 )
         {
                 MyI2CRead(TPA81_PORT, TPA81_ADDR, 0, 10, inbuf);
                 NumOut( 0, LCD_LINE1, inbuf[3], DRAW_OPT_CLEAR_WHOLE_SCREEN );
         }
}
You need to be sure to use 82k pullups on the I2C lines (the recommended 1.8k pullups would be far too strong to use with the NXT).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
salgfrancisco
Posts: 10
Joined: 21 Oct 2011, 15:25

Re: NXC - Reading temperature from TPA81

Post by salgfrancisco »

Ok, so I tried your program but I keep getting the same File error! -1
I am starting to think this problem is somehow related to the NXT and not to the program. Any chance it might be the firmware version?
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC - Reading temperature from TPA81

Post by mattallen37 »

Could be. You should be running the latest enhanced FW (included with BricxCC). It's currently version 1.31. You should also make sure you are using a fairly recent version of BCC. I recommend you use the latest test release (I think it's 3.3.8.10).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
salgfrancisco
Posts: 10
Joined: 21 Oct 2011, 15:25

Re: NXC - Reading temperature from TPA81

Post by salgfrancisco »

So I reinstalled both the Enhanced Firmware and BCC but I keep getting the same error.
I added some ways to debug the program.

Code: Select all

// READ TPA81
#define TPA81_PORT IN_1
#define TPA81_ADDR 0xD0

void MyI2CRead(byte port, byte addr, byte reg, byte cnt, byte& outbuf[])
{
     TextOut( 0, LCD_LINE1, "MyI2cRead Was Called", DRAW_OPT_CLEAR_WHOLE_SCREEN );
  byte cmdbuf[];  // register number holder
   int             loop;
   byte            nByteReady = 0;

  ArrayBuild(cmdbuf, addr, reg);
  TextOut( 0, LCD_LINE1, "Array Built... ", DRAW_OPT_CLEAR_WHOLE_SCREEN );

  loop = STAT_COMM_PENDING;              //Wait until bus is free
  while (loop == STAT_COMM_PENDING ){    //        ''
    loop = I2CStatus(port, nByteReady);  //        ''
  }                                      //        ''
   TextOut( 0, LCD_LINE1, "Reading Temp      ", DRAW_OPT_CLEAR_WHOLE_SCREEN );
  I2CBytes(port, cmdbuf, cnt, outbuf)    //Read the registers
     TextOut( 0, LCD_LINE1, "Temp Read       ", DRAW_OPT_CLEAR_WHOLE_SCREEN );
}

task main()
{

         TextOut( 0, LCD_LINE1, "Program Started    ", DRAW_OPT_CLEAR_WHOLE_SCREEN );
         SetSensorLowspeed(TPA81_PORT);

         byte inbuf[10];

        while( 1 )
         {
                 MyI2CRead( TPA81_PORT, TPA81_ADDR, 0, 10, inbuf );
                 TextOut( 0, LCD_LINE1, "Returned to Main   ", DRAW_OPT_CLEAR_WHOLE_SCREEN );
                 NumOut( 0, LCD_LINE1, inbuf[3], DRAW_OPT_CLEAR_WHOLE_SCREEN );

         }
}
And now I can see that the error only pops up when I reach NumOut function to output the value. Any ideas?
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC - Reading temperature from TPA81

Post by mattallen37 »

Well, the function I gave you is the one I use to read just about any I2C device, so it shouldn't be buggy (well tested).

Instead of "NumOut( 0, LCD_LINE1, inbuf[3], DRAW_OPT_CLEAR_WHOLE_SCREEN );", try:
ClearScreen();
NumOut(0, LCD_LINE1, inbuf[3]);

Although that will probably also crash. It sounds like the array being returned isn't 10 elements long (so it throws an error when you try to use element 4, which doesn't exist).

Things to try:
Use the ArrayLen command to check the number of elements in the returned array.
Use NumOut with inbuf[0], and see if that throws an error (also try elements 1 and 2).
When you declare "byte inbuf[10];", do "byte inbuf[];" instead, and the I2C read function should size it for you.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC - Reading temperature from TPA81

Post by afanofosc »

I would try something like this:

Code: Select all


task main()
{
  byte inbuf[] = {0xD0, 0x00};
  byte outbuf[];

  SetSensorLowspeed(S1, false); // uses LOWSPEED rather than LOWSPEED_9V
  while( true )
  {
    ClearScreen();
    while (I2CCheckStatus(S1) == STAT_COMM_PENDING)
      Wait(MS_1);
    byte count = 10;
    if (I2CBytes(S1, inbuf, count, outbuf ))
    {
      NumOut(40, LCD_LINE1, count);
      if (count > 8) count = 8; // limit output to 8 lines
      for (int i=0; i < count; i++)
        NumOut(0, LCD_LINE1-(i*8), inbuf[i]);
    }
    else
      TextOut(0, LCD_LINE8, "error reading device");
  }
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
salgfrancisco
Posts: 10
Joined: 21 Oct 2011, 15:25

Re: NXC - Reading temperature from TPA81

Post by salgfrancisco »

I just tried what both of you said, and came to a few conclusions.
I used inbuf[]; without saying the length I wanted and after I2CRead I used ArrayLen() and found out that the array was only 1 byte long, meaning that I could only read inbuf[1] which was 0; So probably NXT isnt reading the sensor correctly.

Then I tried the code suggested by afanofosc and the output was always error reading device...
So I am guessing maybe I connected something wrong.

Here is a photo of the circuit
Image

I even removed the pull-up resistors but so far no success.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC - Reading temperature from TPA81

Post by mattallen37 »

The pullups are absolutely necessary. What values did you use?

BTW, mindsensors makes a nice NXT socket breakout for breadboarding with the NXT.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
salgfrancisco
Posts: 10
Joined: 21 Oct 2011, 15:25

Re: NXC - Reading temperature from TPA81

Post by salgfrancisco »

I connected the pull-up resistors (82k) again but it keeps not connecting to the TPA81. I tested the sensor with a Picaxe 28x2 microcontroller and it worked perfectly .
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 13 guests