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..