Author Topic: I2C with compass  (Read 3457 times)

0 Members and 1 Guest are viewing this topic.

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
I2C with compass
« 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!

Offline hgordon

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 373
  • Helpful? 7
    • Surveyor Robotics Journal
Re: I2C with compass
« Reply #1 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;
Surveyor Corporation
  www.surveyor.com

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: I2C with compass
« Reply #2 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

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,704
  • Helpful? 173
    • Society of Robots
Re: I2C with compass
« Reply #3 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?

 

SMF spam blocked by CleanTalk