Hey guys,
I know this topic is dead for long, but I've spent the entire last week trying to resolve all my HMC6352 problem, so that's my solution:
I worked with an arduino mini Pro 5V and Arduino0018 (the latest version) on XP.
Initially, the HMC6352 didn't work at all : I always saw on serial interface "current heading=0.0" (I used the same code as everybody, based on Wire.h". I don't remenber all the changes I made on the code, and all problem I've had : no EndTransmission(), etc.
This solution works fine :
- SDA and SCL on pin A4 and A5
- no pull-up resistors
- HMC6352 supplied with the VCC pin of Arduino (yes, I know it's a bit too high, but HMC accepts)
- GND to GND of course
First, I downloaded the I2CScanner by todbot, here :
(http://todbot.com/blog/2009/11/29/i2cscanner-pde-arduino-as-i2c-bus-scanner/)This .pde is very precious : uploaded on the arduino, it tests all the I2C address between 1 and 100 and returns if a component is present or not. And, surprise!, i discovered that my HMC6352'adress is neither 0x42, nor 0x21, but 0x33! (as the HMC6343... Why? I will surely never know...).
Secondly, due to this .pde also, I have tryed not to used Wire.h, which is likely not efficient for Arduino pro mini. This is my code :
#include <Wire.h> // only to allow to include twi.h
extern "C" {
#include "utility/twi.h"
}
byte addr =33; // address of your I2C slace : just copy the address found by I2CScanner (I don't pay attention to 7- or 8-bit)
int ledPin = 13;
boolean ledState = false;
byte data[2];
int i, headingValue;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
twi_init();
delay(10); // to enable twi to be initiated correctly
}
void loop()
{
// Flash the LED on pin 13 just to show that something is happening
// Also serves as an indication that we're not "stuck" waiting for TWI data
ledState = !ledState;
if (ledState) {
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
// Send a "A" command to the HMC6352
// This requests the current heading data
byte cmd[1];
cmd[0] = 0x41;
// method twi_writeTo from twi.h : send an array of bytes(cmd), length 1 byte (it's the first "1" input), to the slave addressed by addr.
// The last input "1" is a boolean to make the method wait for the completion of the sending
// return a indication of successful : 0:success, 1 to 4:error (search in twi.h to precision)
int res=twi_writeTo(addr, cmd, 1, 1);
Serial.print("A envoye=");
Serial.println(res);
delay(10); // The HMC6352 needs at least a 70us (microsecond) delay
// after this command. Using 10ms just makes it safe
// Read the 2 heading bytes, MSB first
// The resulting 16bit word is the compass heading in 10th's of a degree
// For example: a heading of 1345 would be 134.5 degrees
// method twi_readFrom from twi.h : receive 2 bytes (the "2" input), from the slave addressed by addr, and register in the byte array data
// return the number of bytes read
int N=twi_readFrom(addr, data, 2);
Serial.print("Nb byte recus=");
Serial.println(N);
headingValue = data[0]*256 + data[1]; // Put the MSB and LSB together
Serial.print("Current heading: ");
Serial.print(int (headingValue / 10)); // The whole number part of the heading
Serial.print(".");
Serial.print(int (headingValue % 10)); // The fractional part of the heading
Serial.println(" degrees");
delay(1000);
}
I will do something to remove the include "Wire.h" on the top, but it works.
Thank you everybody for help, I've read at least 5 times every topics about HMC6352 !

Louix
Ps: don't pay attention to my english, you've made out that it's not my native tongue...