I'm just starting with the HMC5883L, I'm writing my code with C and also my own I2C library.
I noticed that the values on the Arduino IDE's serial monitor are constant.
Then I developed a function to read the mode register. So the value should be 0x00 for continuous mode.
But, what I get is that the device work in single mode which has the value 0x01 at first cycle of the program, then it goes to idle mode with the value 0x03.
So, why is that and what to do?
#include <I2C.h>
#define HMC5883L_read 0x3D
#define HMC5883L_write 0x3C
#define Configuration_Register_A 0x00 //Read/Write
#define Configuration_Register_B 0x01 //Read/Write
#define Mode_Register 0x02 //Read/Write
#define Data_Output_X_MSB_Register 0x03 //Read
#define Data_Output_X_LSB_Register 0x04 //Read
#define Data_Output_Z_MSB_Register 0x05 //Read
#define Data_Output_Z_LSB_Register 0x06 //Read
#define Data_Output_Y_MSB_Register 0x07 //Read
#define Data_Output_Y_LSB_Register 0x08 //Read
#define Status_Register 0x09 //Read
#define Identification_Register_A 0x10 //Read
#define Identification_Register_B 0x11 //Read
#define Identification_Register_C 0x12 //Read
#define declination_angle 3.46
void HMC5883L_init (void);
int16_t data_read (int16_t *results);
uint8_t HMC5883L_read_reg (uint8_t reg);
int16_t results[3];
float m_scale = 1.0;
void setup() {
// put your setup code here, to run once:
I2C_init();
Serial.begin(9600);
HMC5883L_init();
Serial.print("mode register ");
HMC5883L_read_reg(Mode_Register);
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
data_read(results);
Serial.print("x-axis = ");
Serial.println(results[0]);
Serial.print("z-axis = ");
Serial.println(results[1]);
Serial.print("y-axis = ");
Serial.println(results[2]);
Serial.println();
Serial.print("mode register ");
HMC5883L_read_reg(Mode_Register);
Serial.println();
_delay_ms(500);
}
void HMC5883L_init (void)
{
I2C_start(HMC5883L_write);
I2C_tx(Configuration_Register_A);
I2C_tx(0x07);
I2C_tx(Configuration_Register_B);
I2C_tx(0x01);
I2C_tx(Mode_Register);
I2C_tx(0x00);
I2C_stop();
}
int16_t data_read (int16_t *results)
{
uint16_t lsb,msb;
int16_t status_of_process = 0;
I2C_start(HMC5883L_write);
I2C_tx(Data_Output_X_MSB_Register);
I2C_start(HMC5883L_read);
msb = I2C_rx();
lsb = I2C_rx();
results[0] = msb << 8 | lsb;
msb = I2C_rx();
lsb = I2C_rx();
results[1] = msb << 8 | lsb;
msb = I2C_rx();
lsb = I2C_rx();
results[2] = msb << 8 | lsb;
I2C_stop();
return status_of_process;
}
uint8_t HMC5883L_read_reg (uint8_t reg)
{
uint8_t reg_dat;
I2C_start(HMC5883L_write);
I2C_tx(reg);
I2C_start(HMC5883L_read);
reg_dat = I2C_rx();
Serial.println(reg_dat);
}
Regards,