I2C for Compass GY 273 (HMC5883L)

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi all, as refer to this topic http://www.cypress.com/forum/psoc-5-device-programming/i2c-hmc5883l-compass , I'm using the same module and trying to use the compass but have some troubles settings up I2C connection with the compass.

I have tried following scenario:
1) Use I2C_MasterSendStart() but the program stuck at I2C_WAIT_BYTE_COMPLETE loop
2) Use I2C_MasterWriteBuf() the program runs but I keep getting the same values even though I move the compass around.
3) Pull up resistor of around 5.5k

I have attached the archive file and looking forward your answers.

Some reference for compass:
https://cdn-shop.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf

0 Likes
12 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Jack, keep in mind that most of the I2C APIs (except those for reading a byte) return a status byte which, when non-zero indicate an error condition. In the I2C corresponding datasheet are the returned values explained. Check the #defined values in the .h file when the returned value is != 0.

   

When programming the PSoC using a kitprog the supply voltage will be 5V! This is more than the compass will swallow! Disconnect it or use a MiniProg3.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Bob,

Thanks for the speedy reply, I have changed the supply voltage to 3.3V but still receiving some error now.
I was checking the status of initializing part and realize I can only write to the I2C once successfully.
Further writing to the pin return a error value of 0x02u.
I have checked the header file and I suppose the error code means "Master not Master in Bus"(Not possible? As I wrote to the pin once before) OR "Slave in operation". I have stopped the connection in WriteBuf() command I suppose?

Attached the image for reference.

   


 

   

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I2C is slow compared to the speed of a CPU. Therefore you must wait for the transfer completed using the _MasterState() API and checking for the I2C_MSTAT_WR_CMPLT bit set before you make another transfer.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Bob,

I tested it again using MasterSendStart() and wait for like 1minutes? The program was stuck in the WAIT_BYTE_COMPLETE while loop and cannot execute further. Usually the I2C shouldn't take that long I suppose?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

This is typical for an electrical problem. Fried pins, toasted sensors or baked chips. Check with a logic analyzer.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Bob,

By logic analyzer what do you mean? Is it debug using oscilloscope? Or do you have any article I can read up to?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Oscilloscope is good for watching analog values, an LA can usually trace 4 to 8 signals in parallel and is able to interpret several communications like I2C, SPI, UART etc. See an example here.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi Bob,

I have just purchased a new Magnetometer and written some new code, but the compass keep giving me fixed results.
Checked the return bit as zero without any error.

Also the compass I purchased is GY273 so I'm supplying 5V to the compass.

   

Jack

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You did not follow the advices I gave you.

   

HMC_I2C_MasterWriteBuf(): You do not wait for transmission complete checking MasterState().

   

You start the transfer with HMC_I2C_MasterSendRestart() although a SendStop() was issued implicitly

   

X=HMC_I2C_MasterReadByte(HMC_I2C_ACK_DATA)<<8; This will fail!!! You are shifting a uint8 8 bits right. This will always result in zero!!!

   

I am darn sure that you have to specify at first from where you start reading from the device, not at the end of the loop. When you do not follow the required sequence your device might stop to respond.

   

I cannot see that you get and check the returned status of the APIs.

   

I would suggest you to write a rainproof function uint8 GetCompassValues(int16 * X, int16*Y, int16*Z) which returns zero when read was successful. Do not mix the high-level and low-level APIs, use the byte APIs (low-level) at first.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi Bob,

I have followed your advice and check the return status and master status this time.

   

The return bit of MasterWriteBuf() gives 0 indicates no error but MasterStatus() stuck at 4 "Transfer in progress" for quite long time. I have waited for 2minutes and the status still return 4. 

Please don't tell me the chip was fried for another second time.

   

I have attached the code just in case you need any reference.

Jack

0 Likes
Anonymous
Not applicable

Hi Bob,

Just to update you with something.

I tried to use I2C_MasterSendStart and I2C_MasterWriteByte instead of SendBuf(), I was able to capture the compass data now.

Cheer!

Jack

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I do not believe that you may initialize several registers with a single writebuf().

   

You will neeed to address each register individually. so change your

   

    Error=HMC_I2C_MasterWriteBuf(I2C_REGISTER,Data,6,HMC_I2C_MODE_COMPLETE_XFER);

   

to 3 calls to a function uint8 WriteByte(uint8 I2CAddress, uint8 Register, uint8 DataByte) which returns zero when successful, nonzero otherwise. Just use Sendstart(), WriteByte() and SendStop() in that function.

   

Do not mix the high-level and low-level APIs, use the byte APIs (low-level) at first. (as I suggested before)

   

 

   

Bob

0 Likes