Shop OBEX P1 Docs P2 Docs Learn Events
Problems with HMC5883L Compass - Reading same data — Parallax Forums

Problems with HMC5883L Compass - Reading same data

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

Comments

  • Welcome to the forums!

    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.
  • If you've using Spin, you can give my bloated object a try. It should let you set all the registers of the sensor.

    The sensor can be very precise if it's held still and multiple readings are averaged.
  • Hello,
    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

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-10-17 17:40
    I'm not familiar with the I2C library you're using but it seems like it's not being used consistently.

    This block of code looks incorrect to me:
      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 );
    

    I think it should be:
      I2CWriteLength[PORT_USED] = 2;
      I2CReadLength[PORT_USED] = 6;
    
      I2CMasterBuffer[PORT_USED][0] = HMC5843_R;	/*	slave address */
      I2CMasterBuffer[PORT_USED][1] = 0x03;	/*	Read starting at Reg 3	*/
      I2CEngine( PORT_USED );
    

    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?
Sign In or Register to comment.