Society of Robots - Robot Forum

Software => Software => Topic started by: Razor Concepts on February 13, 2009, 09:31:08 PM

Title: I2C with compass
Post by: Razor Concepts on February 13, 2009, 09:31:08 PM
I'm trying to get data through I2C between the Axon and a HMC6352 compass module.

Code: [Select]
#include "i2c.h"
void control(void)
{
i2cInit();
i2cSetBitrate(2);
i2cSetLocalDeviceAddr(0x42,TRUE);
//i2cSetSlaveReceiveHandler(i2cSlaveReceiveService);
//i2cSetSlaveTransmitHandler(i2cSlaveTransmitService);
i2cSendStart();
i2cSendByte('A');
i2cSendStop();
delay_ms(10);
//i2cWaitForComplete();
//i2cReceiveByte(ackFlag);
int a = i2cGetReceivedByte();
int b = i2cGetReceivedByte();
rprintf("a = %d b = %d \n",a,b);
delay_ms(1000);
while(1)
{
if(button_pressed())
{
while(button_pressed());
control();
}
}
}

Here is the arduino code I based it off of:
http://wiring.org.co/learning/examples/HMC6352sparkfun.html

The first time I run my program, I get 2 for both a and b values, after that it's 0. I know it has to do something about receiving the bytes (checking if the bytes are actually received, etc) but I'm not sure what to do. Thanks!
Title: Re: I2C with compass
Post by: hgordon on February 13, 2009, 11:01:20 PM
In the 'A' mode, you have to send the read command twice to the HMC6352 to get fresh data.  The first read locks in a new value and the second read gets you the new data.

Once you get data, you multiply the first byte by 256 and add it to the second byte, and divide the sum by 10.  That will give you a reading in degrees.

Here's the code we use on the SRV-1 Blackfin board -

        i2c_data[0] = 0x41;  // read compass twice to clear last reading
        i2cread(0x21, (unsigned char * )i2c_data, 2, SCCB_ON);
        i2c_data[0] = 0x41;
        i2cread(0x21, (unsigned char * )i2c_data, 2, SCCB_ON);
        ix = ((i2c_data[0] << 8 ) + i2c_data[1] ) / 10;
Title: Re: I2C with compass
Post by: Razor Concepts on February 14, 2009, 04:32:18 PM
Hmm, I still can't figure it out. So this should be how it's done:

start send condition
send 'A' at address 0x42
stop send condition
wait 10 ms
read 2 bytes
throw out data
read 2 bytes
Title: Re: I2C with compass
Post by: Admin on February 18, 2009, 01:17:51 AM
If you ever get it working, please post your code!

And you are using I2C from AVRlib, right?