0

I want to develop my C code for HMC5883L.

I have 2 questions for my current code development.

My first question is: But what I found in the datasheet is that the values are in the 2's compliment, so I don't know how to deal with them?! Do I have to take the values as they are in the data registers and convert them to decimal values and that's it? Or I have to convert them from 2's compliment to the original hex values?

This is a quote from the datasheet:

Send 0x3D 0x06 (Read all 6 bytes. If gain is changed then this data set is using previous gain) Convert three 16-bit 2’s compliment hex values to decimal values and assign to X, Z, Y, respectively.

My second question: How o read the data registers? What is the procedure? I saw in the datasheet the following method but I didn't understand it.

Loop Send 0x3D 0x06 (Read all 6 bytes. If gain is changed then this data set is using previous gain) Convert three 16-bit 2’s compliment hex values to decimal values and assign to X, Z, Y, respectively. Send 0x3C 0x03 (point to first data register 03) Wait about 67 ms (if 15 Hz rate) or monitor status register or DRDY hardware interrupt pin End_loop

So, I didn't understand that I send read command then I send write command, so how HMC5883L would return the value to me?

Regards,

1

2's complement is the normal signed values in any modern system, including Arduino.

Just place the bytes into a signed variable of the right size in the right positions and there is your value.

In I2C, to read a register, you typically have to make two transactions. The first is a write to set the register number you want to access, the you follow that with a read transaction to get the data.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Jun 9 '17 at 7:45
  • Any comments that amplify on the answer should be merged into the answer. Anyone with sufficient reputation can do that. – Nick Gammon Jun 9 '17 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.