Pages: [1]   Go Down
Author Topic: Cmps03 does not run...  (Read 536 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);

}
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
Wire.send(1); // select register 1 -- compass bearing -- for input
instead -- just to see if you get better values?
 
Mikal
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Full Member
***
Karma: 0
Posts: 193
Ard at work
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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.
« Last Edit: October 10, 2008, 01:23:57 pm by Syvwlch » Logged

----------
Mathieu

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
thanks for your code.

The twiked code is :

Code:
#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  smiley-wink !!!
Logged

Pages: [1]   Go Up
Jump to: