Arduino interface ke CMPS03 Davantech

Terima kasih, arduino telah memfasilitasi I2c, pull-up di pin SCA/SCL 4.7
kohm sehingga tidak perlu lagi resistor tersebut.
Pin yang di gunakan arduino untuk I2c ialah pin4 dan pin 5
pin 4 – SDA
pin 5 – SCL
tinggal masukan power dan ground maka cmps03 langsung berfungsi.
Kode ini bisa jalan dengan mulus.
 
#include <Wire.h>

/*
CMPS03 compass reader
language: Wiring/Arduino

Reads data from a Devantech CMP03 compass sensor.

Sensor connections:
SDA - Analog pin 4
SCL - Analog pin 5

created 5 Mar. 2007
by Tom Igoe

*/

// include Wire library to read and write I2C commands:

// the commands needed for the SRF sensors:
#define sensorAddress 0x60
// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02

void setup()
{
// start the I2C bus
Wire.begin();
// open the serial port:
Serial.begin(9600);
}

void loop()
{

// send the command to read the result in inches:
setRegister(sensorAddress, resultRegister);
// read the result:
int bearing = readData(sensorAddress, 2);
// print it:
Serial.print("bearing: ");
Serial.print(bearing/10);
Serial.println(" degrees");
// wait before next reading:
delay(70);
}

/*
setRegister() tells the SRF sensor to change the address pointer position
*/
void setRegister(int address, int thisRegister) {
// start I2C transmission:
Wire.beginTransmission(address);
// send address to read from:
Wire.send(thisRegister);
// end I2C transmission:
Wire.endTransmission();
}
/*
readData() returns a result from the SRF sensor
*/

int readData(int address, int numBytes) {
int result = 0; // the result is two bytes long

// send I2C request for data:
Wire.requestFrom(address, numBytes);
// wait for two bytes to return:
while (Wire.available() < 2 ) {
// wait for result
}
// read the two bytes, and combine them into one int:
result = Wire.receive() * 256;
result = result + Wire.receive();
// return the result:
return result;
}

0 komentar:

Posting Komentar