Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

More I2C Questions

I2C

  • Please log in to reply
1 reply to this topic

#1 Rik P.

Rik P.

    Member

  • Members
  • PipPip
  • 18 posts

Posted 15 January 2014 - 09:30 PM

Hey,

 

Firstly thanks to everyone who keeps offering up support as I learn. Much appreciated!

 

I'm embarking on a relatively ambitious (for me!) project, that involves reading data from an array of I2C sensor devices. I think I'm finally getting my head around I2C and was recently using the AbstractI2CDevice.cs class I'd seen posted on these forums, but ran into a few problems with some sensors.

 

I decided to switch from this, to the MultiI2C class in the NETMF Toolbox.

 

But there are still some things that don't quite make sense to me, that I'm hoping you folks can clarify.

 

Let me pick a concrete example:

 

One of the sensors I'm using is an ITG3200 Gyro. It returns data on the I2C bus, and there's already a driver class built using AbstractI2CDevice. For example, using that class, you call the ReadShort method, pass in the registers you want, and you get the data:

public double readX()        {            int data = ReadShort((byte)Register.GYRO_XOUT_H, (byte)Register.GYRO_XOUT_L);            return (((double)(data)) / scaleFactor) - xOffset;        }

That's great. But, in the MultiI2C world, there is no method that appears to accept an array for the registers one wants to read from, that returns a single int as the result. There

's only Write, or WriteRead, the former accepting a byte array for the registers (presumably) to return values from. But I can't see, in this method, where the values are output to. Likewise, in WriteRead, one specifies an array of bytes to read from, and an array of bytes to read to - so I can see there how the values come out, but then can't quite make sense of how you join the array of bytes coming back out into a single int :)

 

Here's what I mean:

 _device.WriteRead(new [] { (ushort)Register.GYRO_XOUT_H, (ushort)Register.GYRO_XOUT_L }, data);

What I need to do, according to the AbstractI2CDevice implementation, is calculate the value of the 'X axis' by dividing the returned data by the scaleFactor variable and then subtracting xOffset from the total:

return (((double)(data)) / scaleFactor) - xOffset;

In ths example though, data isn't a single int - it is a byte[] (or ushort[]) array, so clearly isn't convertible to a double.

 

Clearly there's a concept here I'm not understanding, but it seems most of the material I've been able to Bing/Google is aimed at folks who kinda already have a very good understanding of I2C and just want to figure out how to work with that in the .NET MF world. Whereas, the world of bits and bytes are relatively new to me :)

 

Of course, if I really understood what was happening under the hood I'd probably be able to write my own drivers for most of these sensors (and would happily offer them up to the community when I'm done).

 

Any help you could offer would be much appreciated.

 

Thanks,

 

rik


---
Twitter: @rikp


#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 16 January 2014 - 12:35 AM

well, you dont call it like that.

 

accelerometers, gyros and stuff like that should update their register pointer automatically.

int data = ReadShort((byte)Register.GYRO_XOUT_H); - its either _H or _L, the second _should_ get

selected automatically.

 

normally you read all 6 registers at once, specify the first address, than read 6 bytes.

 

if that doesent work, read 2x 1 byte each and convert them to int16 (bitshifting)







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.