[FRDM-KL46Z]Cannot set up proper I2C connection to MAG3110, really need help.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

[FRDM-KL46Z]Cannot set up proper I2C connection to MAG3110, really need help.

1,157 Views
ningzhejiang
Contributor I

Hello all, I am trying to implement a E-compass using the magnetometer(MAG3110) attached on KL46Z. I found a piece of example code and tried to reproduce the same function from it. However, my code doesn't work. It seems that I fail to set up a proper I2C connection to MAG3110. I can write to I2C0->D, but it seems that the data doesn't go through. And when I read from I2C0->D, the only value I can get is 0x1c which is actually (0x0e << 1 | 0) (I2C address of MAG3110 or'ed with write bit). I am afraid there is no connection between the micro and MAG3110 at all.

I am also confused if I need to use the I2C interrupt(the example code doesn't).  I tried to disable the interrupt, but then my program got stuck in "startup_MKL46Z4.S" which probably because it could not return from interrupt. The example code clears the interrupt flag in "void I2C_wait()".

The attached file is the functions I use to configure the I2C connection. For simplicity other part of the code is not included.

Could anyone diagnose the problems for me?  Much thank!

Original Attachment has been moved to: I2C_functions.zip

Tags (3)
0 Kudos
2 Replies

603 Views
Stano
NXP Employee
NXP Employee

Hi Jiang,

you have an error in your function:

void mag_init(){ //initialization that configures MAG3110

  I2C_SingleByteWrite(MAG_CTRL_REG1,0x00); //standby mode

  I2C_SingleByteWrite(MAG_CTRL_REG2,0x80); //auto reset

  I2C_SingleByteWrite(MAG_CTRL_REG1,0x01); //active

}

It has to be as in the demo-board code:

void mag_init(void)

{

    hal_dev_mag3110_init();      //Initialize I2C modules

    hal_dev_mag3110_write_reg(MAG_CTRL_REG1,0x00); //Standby mode

    hal_dev_mag3110_write_reg(MAG_CTRL_REG2,0x80); //Auto reset

    hal_dev_mag3110_write_reg(MAG_CTRL_REG1,0x01); //Active

}

You have not properly initialized the MAG3110 device. Please take a look on the original code in the "hal_i2c.c" file.

I think it helps you.

Best Regards,

Stano.

0 Kudos

603 Views
ningzhejiang
Contributor I

Thanks for reply. But actually I did that in my main. The init function for MAG is in my main function.

0 Kudos