I2C Help with L3GD20H (Gyroscope)

mccheesy

Member
Hi, I'm fairly new to using I2C in general and have a basic understanding of the concept by reading the PICAXE I2C manual, the sections in the BASIC commands manual, as well as some Youtube videos, but in practice, I'm still struggling with implementation.

I'm using a PICAXE 14M2 and an L3GD20H Breakout board from Adafruit (Datasheet link https://www.st.com/resource/en/datasheet/l3gd20h.pdf)

In the I2C operation section of the manual, there is a required protocol that is mentioned that must be adhered to for this part, such as starting communication with a "Start Signal" and how the Master must send a "Data transfer acknowledge" pulse every time a byte of data is received, etc. among other requirements.

My question is this: Are these rigid and very specific protocols and order something that I must specify in my code via multiple hi2c commands and logic, or is this something that is covered "under the hood" so to speak by either the PICAXE itself or the way that the hi2c commands are compiled.

The reason I ask is that I have previously used the ADXL343 Accelerometer breakout board from Adafruit using I2C and only had to communicate using the basic hi2csetup and hi2c in and out to specific registers; there was no extra "song and dance" to getting data back, no acknowledgment pulses or what have you.
 

lbenson

Senior Member
Without knowing anything about the part, I would guess that it would be "under the hood", and I'd just hook it up and try it. Remember that when connecting to a PICAXE, for the software you need to shift the datasheet's address left by one (multiply by 2), since the PICAXE takes care of the least significant bit "under the hood".
 

AllyCat

Senior Member
Hi,

Welcome to the forum. No, generally you don't need to worry about the Acknowledge which just happens "under the hood". Sometimes manufacturers go into too much detail in their data sheets (the worry being that they have done something that isn't a "standard" of the I2C protocol). Others give so little information that you have to really search even for the Slave Address.

Basically you just need to get the setup correct; the PICaxe is normally Master, the address is full 8-bits (i.e. the high 7 bits of the byte followed by a 0), it's safer to start with i2cslow (particularly if using a higher clock frequency) and the I2Cbyte/word will normally be a byte except for addressing a larger memory. Then the command itself is normally in the form: HI2Cin/out address,(data) although the address/data discrimination does not affect how the bytes (the data is always in bytes) are actually transmitted to the slave.

Cheers, Alan.
 

mccheesy

Member
If I could ask one more question to the kind people here, how should I go about converting 16-bit data written in 2's Complement to Decimal?

Each half of the 16-bit word is found in 2 different registers on the Slave device. Currently, I'm using an hi2cin command to read the halves into PIC memory (b2 and b3 for example).

The documentation on the gyroscope I'm using is frankly very poor, so I don't even know which register is the most significant byte or which is the least significant byte, and from what I've seen online by seeing how others have done this with a PICAXE is either very glossed over by the poster or not event explained at all, so I'm left scratching my head at how to move forward.

The only leads I have so far for sure is that if the value is negative, then for certain the first bit of the Most significant byte will be a 1.
 

AllyCat

Senior Member
Hi,

It depends if the I2C device sends the low or high byte first; if low byte first then reading into b2,b3 (or any adjacent pair starting with an even address) will form a Word in the associated address (W1 in this case). If the high byte is sent first then read them as b3 , b2. If you can't find which byte is sent first then try both! If the word value changes "smoothly" then the bytes are probably correct, if the values "jump about" then they're probably the wrong way around. ;)

To send SERTXD (to the terminal) or to a serial port, the # qualifier makes formatting very easy. But an LCD/OLED display, etc. normally needs separate ASCII bytes, which are a little more tricky (so I won't bother with that unless it's needed). Thus typical code can be simply:
Code:
if w1 > 32767 then      ; It's negative (or can use  >=$8000 , etc.)
   w1 = -w1
   sertxd("-")       ; Send negative sign
endif
sertxd(#w1)          ; Send the decimal value to the terminal emulator
Cheers, Alan.
 
Last edited:

AllyCat

Senior Member
Hi,
The documentation on the gyroscope I'm using is frankly very poor, so I don't even know which register is the most significant byte or which is the least significant byte,
Hmm, I've only now made a cursory examination of the ST data sheet, but IMHO it looks like a model of clarity; I've seen far worse, even from companies that aren't hampered by a translation from Chinese. ;)

The Slave address (in PICaxe format) should be either $D4 or $D6 (%1101 01x0) depending how the input pin has been configured, and Table 17 identifies all the registers. A read from register 15 ($0F) should return the "Who_Am_I" value of $D7 (215) to test the I2C functionality, and registers $28 to $2D (40 - 45) should report data for the X, Y and Z axes, Least Significant Byte first. I haven't looked in to what "initialisation" might be needed, but then I would expect a simple HI2CIN 40,(b4,b5,b6,b7,b8,b9) to return the XYZ data words into W2, W3 and W4. In practice, you might need to read the Status from register 39 first.

24315

Cheers, Alan.
 
Top