Project

General

Profile

Reading out HDMM01 Compass Modul with DeviceI2C

Added by kami over 6 years ago

Hi,

i just connected a RTC-Modul and a HDMM01 Compass Modul on Port 3 of a SMDKit.

PortI2C myport (3 /*, PortI2C::KHZ400 */);
DeviceI2C rtc (myport, 0x68);
DeviceI2C compass (myport, 0x30);

i would like to use this source-code to read out the Modul like this:

[[[http://www.aurob.com/?p=467]]]

#include

#define I2ADDR 0×30
#define TakeMeasure 0×01

void setup(){
Wire.begin();
Serial.begin(9600);
Serial.println(“Start Programm”);
}
void loop(){
byte MsbX,LsbX,MsbY,LsbY;
int x,y;
char line[80];
Wire.beginTransmission(0×30); // Pollin HDMM01
Wire.send(0×00);
Wire.send(0×01);
Wire.endTransmission();
delay(20);
Wire.beginTransmission(0×30);
Wire.send(0×01);
Wire.requestFrom(0×30, 4);

while(Wire.available()<4);
MsbX =Wire.receive(); // obere 4 Bit X
LsbX =Wire.receive(); // untere 8 Bit X
MsbY =Wire.receive(); // obere 4 Bit Y
LsbY =Wire.receive(); // untere 8 Bit Y
Wire.endTransmission(); // stop transmitting
x=((MsbX&0x0f)*256)+(LsbX);
y=((MsbY&0x0f)*256)+(LsbY);
x = map(x, 1900, 2188, -180, 180);
y = map(y, 1910, 2193, -180, 180);
double mygrad = atan2(x, y)*180/3.1415927410;
if (mygrad < 0) mygrad = mygrad +360;
// Ausgabe von X, Y und Grad
Serial.print("KOM:");
Serial.print(x);
Serial.print(";");
Serial.print(y);
Serial.print(";");
Serial.println(mygrad);
delay(200);
}
- See more at: http://www.aurob.com/?p=467#sthash.6wxaBbsx.dpuf

Can someone help me to transfer it for PortsI2C and DeviceI2C?

Thanks a lot.

Cu kami


Replies (4)

RE: Reading out HDMM01 Compass Modul with DeviceI2C - Added by kami over 6 years ago

Hi,

anyone with a short break to help me with this???

thanks a lot.

Cu kami

RE: Reading out HDMM01 Compass Modul with DeviceI2C - Added by kami over 6 years ago

Hi,

i just tested a little bit:

#include 

PortI2C i2cBus (3);                        // Jeenode Port 1, DIO -> SDA
DeviceI2C compassChip (i2cBus, 0x30);

#define SET_COIL 0x02 // set magnetic coil
#define RESET_COIL 0x04 // reset magnetic coil

void setup()
{
  Serial.begin(9600);
  Serial.println("\nCompass");
}


void loop()
{

ResetSetCoils();
byte MsbX,LsbX,MsbY,LsbY;
int x,y;
char line[80];



compassChip.send();
compassChip.write(0x00);   
compassChip.write(0x01);
compassChip.stop();
delay(20);
compassChip.send();
compassChip.write(0x01);
compassChip.stop();

        compassChip.receive();                      


MsbX  =compassChip.read(0); 
LsbX  =compassChip.read(0);
MsbY  =compassChip.read(0); 
LsbY  =compassChip.read(0); 
compassChip.stop();

    Serial.print("C: ");
    Serial.print(MsbX);
    Serial.print(" ");
    Serial.print(LsbX); 
     Serial.print(" ");   
    Serial.print(MsbY);
    Serial.print(" ");
    Serial.println(LsbY);



    x=((MsbX&0x0f)*256)+(LsbX);
    y=((MsbY&0x0f)*256)+(LsbY);
    x = map(x, 1900, 2188, -180, 180);
    y = map(y, 1910, 2193, -180, 180);
    double mygrad = atan2(x, y)*180/3.1415927410;
    if (mygrad < 0) mygrad = mygrad +360;
    // Ausgabe von X, Y und Grad
    Serial.print("KOM:");
    Serial.print(x);
    Serial.print(";");
    Serial.print(y);
    Serial.print(";");
    Serial.println(mygrad);
    delay(200);
}

void ResetSetCoils (void)
{

    // RESET / SET Coils
    compassChip.send();
    compassChip.write(0x00);
    compassChip.write(RESET_COIL);

    delay(1);
     compassChip.send();
    compassChip.write(0x00);
    compassChip.write(SET_COIL);

    delay(1);
} 

This code is working but i have got some understanding problem with reading out the MSB and LSB like it is described in the original sketch:

Wire.requestFrom(0×30, 4);

while(Wire.available()<4);
MsbX =Wire.read(); // upper 4 Bit X
LsbX =Wire.read(); // lower 8 Bit X
MsbY =Wire.read(); // upper 4 Bit Y
LsbY =Wire.read(); // lower 8 Bit Y

Can someone please help to transfer this for DEVICEI2C??

Thanks a lot.

Cu kami

RE: Reading out HDMM01 Compass Modul with DeviceI2C - Added by JohnO over 6 years ago

Perhaps you could check the return codes from compassChip.send(); etc. Also, do you think compassChip.isPresent(); would work with your hardware?

    (1-4/4)