Go Down

Topic: Cmps03 does not run... (Read 719 times) previous topic - next topic

aboxdc

Hi!

I have bought cmps03 compass module from Robot Italy. First i read datasheet and try to write program, but it module does not respond. i got some zeros and sometimes it writes 204, but is should change numbers when i turn the module.

For interfacing i use I2C, but i think i made mistake somewhere..

http://www.robot-italy.com/product_info.php?products_id=91

Program:

#include <Wire.h>
#define COMPASSMOD 0xC0

void setup()
{
 Serial.begin(38400);

 Wire.begin();
}
void loop()
{
  int data=0 ;
 //  Send input register COMPASSMOD
 Wire.beginTransmission(COMPASSMOD);

 Wire.send(Serial.read());
 Wire.endTransmission();
 
 //  Connect to device and request two bytes
 Wire.beginTransmission(COMPASSMOD);
 Wire.requestFrom(COMPASSMOD, 2);
 
 if (Wire.available()) {
   data = Wire.receive();
 }
 if (Wire.available()) {
   data |= Wire.receive() << 8;
 }
 
 Wire.endTransmission();
 
Serial.println(data);

}

mikalhart

aboxdc--

Reading the data sheet for your device, I see that you are supposed to supply the register number you seek to access right after you send the address (0xC0).  If you are hoping that the line
Code: [Select]
Wire.send(Serial.read()) will do that, I think you will be disappointed.  Serial.read() returns -1 (except when a character is actually available).  You are sending this (invalid) value most of the time to the device.

Can you try doing something like
Code: [Select]
Wire.send(1); // select register 1 -- compass bearing -- for input
instead -- just to see if you get better values?

Mikal

aboxdc

i have change it to the Wire.send(1); but i get only 0..

Syvwlch

#3
Oct 10, 2008, 08:22 pm Last Edit: Oct 10, 2008, 08:23 pm by Syvwlch Reason: 1
A few suggestions:

I've been using other I2C compasses, and you usually need to wait between the command and reading out the reply from the sensor. As it stands, I think you may be bombarding the sensor with commands and never giving it a chance to breathe.

Also, the wire library is a little quirky in how it handles the address of the sensor. If you look at the examples in the playground [edit: uh, the hmc6354 example in the playground seems to be gone, never mind, see below for another example], you will see that the address is usually modified before being used. This means that you are probably not talking to your sensor at all, or rather that it does not realize you are talking to it.

Putting those comments together, here is how I would do it:

Code: [Select]


#include <Wire.h>

#define CPMS03Address  0xC0
int slaveAddress;             // This is calculated in the setup() function

byte data[2];                        // This will hold the raw data from the sensor

int result;                    // This will hold the value that data represents

int i = 0;

void setup()
{

// Shift the device's documented slave address (0xC0) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)

slaveAddress = CPMS03Address  >> 1;   // This results in the 7 bit address to pass to TWI

while( millis() < 500) { delay(10); }  // The HMC6343 (for example) needs a half second to start from power up

Wire.begin();

}

void loop()
{

Wire.beginTransmission(slaveAddress);
Wire.send(0x01);             // Send a "Get Data" command (e.g. (0x50 for the HMC6343)
                        // or the register number in your case: 0x01
Wire.endTransmission();

delay(2);                    // The HMC6343 needs at least a 1ms (microsecond) delay
                             // after this command.  Use whatever the data sheet says.

// Read the 2 heading bytes

Wire.requestFrom(slaveAddress, 2);        // Request the 2 bytes of data
i = 0;
while(Wire.available() && i < 2)
{
  data[i] = Wire.receive();
  i++;
}

result = data[0];
result |= data[1] << 8;

Serial.println(result);

}



I haven't tried that code and I don't have that sensor, so you may need to tweak it a bit, but at the very least you need that address trick to make it work, I suspect.
----------
Mathieu

vadda

Hi,
thanks for your code.

The twiked code is :

Code: [Select]
#include <Wire.h>

#define CPMS03Address  0xC0
int slaveAddress;             // This is calculated in the setup() function

byte data[2];                        // This will hold the raw data from the sensor
int result;                    // This will hold the value that data represents

void setup()
{
// Shift the device's documented slave address (0xC0) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)

slaveAddress = CPMS03Address  >> 1;   // This results in the 7 bit address to pass to TWI

Serial.begin(9600);
Wire.begin();
}

void loop()
{

Wire.beginTransmission(slaveAddress);
Wire.send(2);             // Send a "Get Data" command (registry 2 for heading)
Wire.endTransmission();

// Read the 2 heading bytes
 Wire.requestFrom(slaveAddress, 2);   // Request the 2 bytes of data
 while(Wire.available() < 2);         //while there is a byte to receive
 data[0] = Wire.receive();            //reads the byte as an integer
 data[1] = Wire.receive();
 int result = ((data[0]<<8)+data[1])/10;

 Serial.println(result);
 delay(500);

}


Tested  ;) !!!

Go Up