I am trying to get the same setup to work and I keep hitting the same problem :-/
Ardunio NG connected to a Sparkfun HMC6352, SDA is connected to analog pin 4, SCL is connected to analog pin 5, both SDA & SCL are pulled high with 1.8 kOhm.
The odd thing is that this setup works with my PIC18 microcontroller! That's the code I use there
// controlling HMC6352 via I2C bus
#include "18f452.h"
#fuses hs,nowdt,put,noprotect
#use delay(clock=20000000) // 20 MHz
#use i2c(master, sda=PIN_C4, scl=PIN_C3)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#define SLAVE 0x42 // address of slave
#define ACK 0x01
#define NACK 0x00
// functions
byte readByte(byte *buffer, int len);
byte writeByte(byte command, byte *buffer, int len);
void main()
{
byte buffer[2] = {0,0};
float yaw;
long sum;
// set operational mode
buffer[0] = 0x74; // register address
buffer[1] = 0x62; // 20 Hz, continuous mode
writeByte(0x47, buffer, 2);
delay_ms(70);
// get data
writeByte(0x41, 0, 0);
delay_ms(6000);
while (true)
{
readByte(buffer, 2);
sum = buffer[0];
sum = sum << 8; // can't shift buffer[n] itself
sum += buffer[1];
yaw = sum/10.0;
printf("%05.1f\n", yaw);
}
}
byte readByte(byte *buffer, int len)
{
byte i;
i2c_start();
if (i2c_write(SLAVE+1))
{
// no ACK received
// this fails quite often, maybe we should wait somewhere?
//printf("readByte: i2c_write() failed\n");
return 0;
}
for (i=0; i<len; i++)
{
// fill buffer
*(buffer+i) = i2c_read(ACK);
}
// might be needed..
//i2c_stop();
return 1;
}
byte writeByte(byte command, byte *buffer, int len)
{
byte i;
i2c_start();
if (i2c_write(SLAVE))
{
// no ACK received
// DEBUG (never occured so far)
//printf("writeByte: i2c_write() failed for address\n");
return 0;
}
if (i2c_write(command))
{
// no ACK received
// DEBUG (never occured so far)
//printf("writeByte: i2c_write() failed for command\n");
return 0;
}
for (i=0; i<len; i++)
{
// send buffer
if (i2c_write(*(buffer+i)))
{
// no ACK received
// DEBUG (never occured so far)
//printf("writeByte: i2c_write() failed for data byte %u\n", i);
return 0;
}
}
// might be needed..
//i2c_stop();
return 1;
}
(the PICC suite lacks high level I2C routines, and as you see from the comments in readByte and writeByte, this does not all work according to the spec here either, but it does work and maybe this gives a hint..)
Using similar code with Arduino or even trying to read values from EEPROM returns just (static) garbage. Help!
I am not that much into electronics, but maybe something is wrong wrt to levels or the pull-up resistors? The HMC6352 spec says
The HMC6352 Serial Clock (SCL) and Serial Data (SDA) lines do not have internal pull-up resistors, and require resistive pull-ups (Rp) between the master device (usually a host microprocessor) and the HMC6352. Pull-up resistance values of about 10k ohms are recommended with a nominal 3.0-volt supply voltage. Other values may be used as defined in the I2C Bus Specification 2.1.
And I am also unsure whether or not pull-up resistors are even needed, because http://research.techkwondo.com/blog/julian/279 is quoting Nicolas
I have pull-up resistors on those lines, but Nicolas explains that his library enables the internal pull-ups on the Atmega, so I can probably pull those from my breadboard.