You are here

I2C for Compass GY 273 (HMC5883L) | Cypress Semiconductor

I2C for Compass GY 273 (HMC5883L)

Summary: 12 Replies, Latest post by Bob Marlowe on 13 Sep 2016 07:23 AM PDT
Verified Answers: 2
Last post
Log in to post new comments.
wjlee24_1764846's picture
User
11 posts

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

user_1377889's picture
User
10110 posts

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

wjlee24_1764846's picture
User
11 posts

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.

 

user_1377889's picture
User
10110 posts

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

wjlee24_1764846's picture
User
11 posts

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?

user_1377889's picture
User
10110 posts

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

 

Bob

wjlee24_1764846's picture
User
11 posts

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?

user_1377889's picture
User
10110 posts

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

wjlee24_1764846's picture
User
11 posts

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

user_1377889's picture
User
10110 posts

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

wjlee24_1764846's picture
User
11 posts

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

Log in to post new comments.