Problems with HMC5883L Compass - Reading same data
chethan2689
Posts: 2
Hi,
I have interfacing HMC5883L in continuous mode. i have reading correct data if every time reset the device otherwise it will give constant data. if i reset it will give updated data .please give me the suggestion.
Regards,
Chethan
I have interfacing HMC5883L in continuous mode. i have reading correct data if every time reset the device otherwise it will give constant data. if i reset it will give updated data .please give me the suggestion.
Regards,
Chethan
Comments
You might want to add which MPU you are using, and any code that you are trying.
From your original post, it's impossible to give an answer.
The sensor can be very precise if it's held still and multiple readings are averaged.
I am using LPC1768 MCU my code pasted below
*****************************Main function****************************
int main()
{
int16_t x, y, z;
unsigned long i;
char buff[20],buf[6];
SystemClockUpdate();
UARTInit(9600);
I2C0Init();
init_hmc5843();
while(1)
{
read_hmc5843(buf);
x = ((buf[0] << 8) | buf[1]);
y = ((buf[2] << 8) | buf[3]);
z = ((buf[4] << 8) | buf[5]);
sprintf(buff,"x=%04d, y=%04d, z=%04d\n", x, y, z);
UART_Sendstring((unsigned char*)buff);
for ( i = 0; i < 6; i++ )
{
buf = '0';
}
for ( i = 0; i < 20; i++ )
{
buff = '0';
}
}
}
*************************Read Function****************************
int16_t read_hmc5843(char *buf)
{
unsigned long i;
unsigned int read;
// while((read = LPC_GPIO0->FIOPIN) &(0x01 << 26));
I2CWriteLength[PORT_USED] = 2;
I2CReadLength[PORT_USED] = 0;
I2CMasterBuffer[PORT_USED][0] = HMC5843_W; /* slave address */
I2CMasterBuffer[PORT_USED][1] = 0x03; /* Reg Address */
I2CEngine( PORT_USED );
for ( i = 0; i < 0x2000; i++ ); /* Delay after write */
I2CWriteLength[PORT_USED] = 2;
I2CReadLength[PORT_USED] = 6;
I2CMasterBuffer[PORT_USED][0] = HMC5843_R; /* slave address */
I2CMasterBuffer[PORT_USED][1] = 0x06; /* Read all 6 bytes */
I2CEngine( PORT_USED );
for ( i = 0; i < 0x200000; i++ ); /* Delay after write */
for ( i = 0; i < 6; i++ )
{
buf=I2CSlaveBuffer[PORT_USED]; // Readins data from Slave buffer..
}
return 0;
}
*******************init function**********************
void init_hmc5843(void)
{
unsigned int i;
I2CWriteLength[PORT_USED] = 3;
I2CReadLength[PORT_USED] = 0;
I2CMasterBuffer[PORT_USED][0] = HMC5843_W; /* slave address */
I2CMasterBuffer[PORT_USED][1] = 0x00; /* Reg Address */
I2CMasterBuffer[PORT_USED][2] = 0x70; /* 8-average, 15 Hz default, normal measurement*/
I2CEngine( PORT_USED );
for ( i = 0; i < 0x20000; i++ ); /* Delay after write */
I2CWriteLength[PORT_USED] = 3;
I2CReadLength[PORT_USED] = 0;
I2CMasterBuffer[PORT_USED][0] = HMC5843_W; /* slave address */
I2CMasterBuffer[PORT_USED][1] = 0x01; /* Reg Address */
I2CMasterBuffer[PORT_USED][2] = 0xA0; /* Set the gain = 5 */
I2CEngine( PORT_USED );
for ( i = 0; i < 0x20000; i++ ); /* Delay after write */
I2CWriteLength[PORT_USED] = 3;
I2CReadLength[PORT_USED] = 0;
I2CMasterBuffer[PORT_USED][0] = HMC5843_W; /* slave address */
I2CMasterBuffer[PORT_USED][1] = 0x02; /* Reg Address */
I2CMasterBuffer[PORT_USED][2] = 0x00; /*Continuous measurement mode */
I2CEngine( PORT_USED );
}
Please give some solution.
Regards,
Chethan
This block of code looks incorrect to me:
I think it should be:
It looks like you're using the number of bytes being read where the register number should go.
If this still doesn't work, what are the values your program outputs?