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