8 years ago.

I2C using with HMC5883

I tried to get some information via I2C interface for the long time, but i have failed. Maybe someone can help me to get some simple information from HMC5883 compas. This is my code:

#include "mbed.h"
#define ADDR 0x3C

I2C compas(PB_9, PB_8);
Serial COM(USBTX, USBRX);
char d_write[2];
char d_read[2];

int main()
{
    COM.baud(115200);
    wait_ms(15);
    d_write[0]=0x00;
    d_write[1]=0x78;
    compas.write(ADDR, d_write, 2, 0);
    d_write[0]=0x02;
    d_write[1]=0x00;
    compas.write(ADDR, d_write, 2, 0);
    while(1)
    {
        wait_ms(400);
        d_write[0]=0x03;
        compas.write(ADDR, d_write, 1, 1);compas.read(ADDR, d_read, 1, 0);
        COM.printf("Data: %i\r\n", d_read);
    }
}

But this program returns: "Data: 536871206" everytime, even if HMC is unpluged. What have I done wrong?

I am using STM32 L152RE. SCL and SDA pins on HMC5883 are connected to PB_8 and PB_9 pins on STM. Excuse my English.

Be the first to answer this question.