How to read the x/y/z and temperature values of MAG3110 correctly?

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read the x/y/z and temperature values of MAG3110 correctly?

745 Views
intrudert
Contributor II

Hi All,

Am working with MAG3110 and STM32L0..

I just want to read the temperature and x/y/z values.

Here is my code:

void mag_init(){
        ctrl_conf1[0]=0x10,ctrl_conf1[1]=0x18;

      //I tried with all sampling rates and OSR too
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, ctrl_conf1,2,100);    
        HAL_Delay(10);
    
        ctrl_conf2[0]=0x11,ctrl_conf2[1]=0x80;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, ctrl_conf2,2,100);    
        HAL_Delay(10);
    
        ctrl_conf1[0]=0x10,ctrl_conf1[1]=0x19;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, ctrl_conf1,2,100);    
        HAL_Delay(100);
}

//Temperature reading

void read_temp(){
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, &temp_add ,1,100);
        HAL_Delay(10);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, temp_val,2,100);
}

//Reading the x/y/z/lsb/msb

void read_reg(){
        x_msb[0]=0x01;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, x_msb,1,100);        
        HAL_Delay(5);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &x_msb[1],1,100);
        
        x_lsb[0]=0x02;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, x_lsb,1,100);        
        HAL_Delay(5);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &x_lsb[1],1,100);    
    
        y_msb[0]=0x03;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, y_msb,1,100);        
        HAL_Delay(5);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &y_msb[1],1,100);

        y_lsb[0]=0x04;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, y_lsb,1,100);        
        HAL_Delay(5);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &y_lsb[1],1,100);    

        z_msb[0]=0x05;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, z_msb,1,100);        
        HAL_Delay(5);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &z_msb[1],1,100);

        z_lsb[0]=0x06;
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, z_lsb,1,100);        
        HAL_Delay(5);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &z_lsb[1],1,100);        
}

void read_full_scl_values(){
        x_val = ((short) (x_msb[0]<<8 | x_lsb[1]));
        y_val = ((short) (y_msb[0]<<8 | y_lsb[1]));
        z_val = ((short) (z_msb[0]<<8 | z_lsb[1]));
    }

But the temperature is always either a constant value at 128 or randomly fluctuates around 127 but never be a consistant or predictable value..

Same case with the x/y/z values too..

X/y/z values are not at all consistant and looks lie am getting spikes in the middle of the data..

And therre is no change "If I BRING A METAL" near to the sensor..

I tried programming with PIC ic too... but the result is same..

Labels (2)
0 Kudos
8 Replies

260 Views
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Intruder,

It is hard to say where the problem can be. I do have a few questions/suggestions to let us start.

When do you read the data? There are basically two possibilities - either using the INT1 pin for an interrupt when a new data is ready or polling the ZYXDR bit of the DR_STATUS register.

There are several setup examples shown in the datasheet and I would also recommend you to take a closer look at our example project - MAG3110 – Bare metal example project 

pastedImage_4.png

As for the temperature measurement, please keep in mind that the temperature output is not trimmed for offset so the readings will be off. You can correct the offset in software. Sensitivity of 1°C/LSB is OK.

pastedImage_7.png

Best regards,

Tomas

0 Kudos

260 Views
dananghel
Contributor I

Hello Tomas,

I am working on a project that uses MAG3110FCR1. As we were unable to find  MAG3110FCR1 in stocks , we ordered FXMS3110CDR1.  We are able to read the magnetic values form the registers but not the temperature , for some boards it shows 252 and for others it shows 251. We are using the same source code from our previous  project that  used MAG3110FCR1 that read the temperature just fine and we only changed the I2C address.In the data sheet there nothing mentioned about limitations on this.

What could be the reason?

Regards,


Dan

0 Kudos

260 Views
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Dan,

As described in the datasheet, the temperature output is not trimmed for offset. You can correct the offset in software. Sensitivity of 1°C/LSB is OK.

pastedImage_7.png

Best regards,

Tomas

0 Kudos

260 Views
dananghel
Contributor I

Hello,

I found the problem. Its not tha value ittself but the way I was representing it .My old code worked fine because the value reported by MAG3110FCR at room temperature was 10 , so an offset of +12/13 was ok. But I declared the value then as in int in my C code. As stated in the data sheet it's an 8-bit 2's complement so the value ranges from -128 to 127. By declaring the value as an int8_t and adding the corresponding offset it fixes the problem.

 Regards,

Dan

0 Kudos

260 Views
intrudert
Contributor II

Hi Tomas, thanks for your reply...

Well... I followed the same bare metal example and written my code..

And I've even verified all the three cases u mentioned...

Am using the int0 pin for new data..

Yet it is of no use..

Data is always an unpredictable random number...

Even temperature too... I blew hot air on the chip up to 55 deg.. the default reading is 128 at 23 deg and as the temperature goes high... I got the readings like 10, 7,128,23,246,20,... Etc... 

How to calibrate the temperature register?

What's wrong with the code?

I tried with three mag3110 chips... All the sensors give the same result...

0 Kudos

260 Views
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Intruder,

Do you have a logic analyzer or an oscilloscope to see what is going on the I2C bus and INT0 line?

After reading the registers 0x01-0x06 in your ISR, is the INT0 pin deasserted?

Are you able to read correctly the WHO_AM_I register (0x07) whose value should be 0xC4?

Best regards,

Tomas

0 Kudos

260 Views
intrudert
Contributor II

Yes..

I am able read the 0x07 register correctly..

Logic analyzer shows what I am receiving.. I2C functionality is fine... start bit, stop bit...etc everything is alright

INT0 pin is deasserted after reading the X_MSB register..I've tried this inside the "ISR" and "While loop" also

0 Kudos

260 Views
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Intruder,

Ok, there are sometimes problems with converting the raw values to 16-bit signed real values. Your code seems to be correct, but to be sure, could you please post here a few raw values you are reading from registers 0x01 – 0x06 and their corresponding 16-bit values?

 

Would it be possible to see your schematic and layout? Is MAG3110FC the only component on your PCB along with the ST”s MCU?

 

Can you also specify how did you solder the MAG3110FC on your PCB?

Best regards,

Tomas

0 Kudos