CMPS03 digital compass library.

Dependents:   CMPS03_HelloWorld Final_Sonar xbeetx Compass ... more

Files at this revision

API Documentation at this revision

Comitter:
aberk
Date:
Sat Nov 27 12:09:27 2010 +0000
Commit message:
Version 1.0

Changed in this revision

CMPS03.cpp Show annotated file Show diff for this revision Revisions of this file
CMPS03.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMPS03.cpp	Sat Nov 27 12:09:27 2010 +0000
@@ -0,0 +1,85 @@
+/**
+ * @author Aaron Berk
+ * 
+ * @section LICENSE
+ *
+ * Copyright (c) 2010 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * CMPS03 digital compass module.
+ *
+ * Datasheet:
+ *
+ * http://www.robot-electronics.co.uk/htm/cmps3tech.htm
+ */
+
+/**
+ * Includes
+ */
+#include "CMPS03.h"
+
+CMPS03::CMPS03(PinName sda, PinName scl, int address) {
+
+    i2c = new I2C(sda, scl);
+    //Compass designed to work at 100KHz. See datasheet for details.
+    i2c->frequency(100000);
+    i2cAddress = address;
+
+}
+
+char CMPS03::readSoftwareRevision(void){
+
+    char registerNumber   = SOFTWARE_REVISION_REG;
+    char registerContents = 0;
+
+    //First, send the number of register we wish to read,
+    //in this case, command register, number 0.
+    i2c->write(i2cAddress, &registerNumber, 1);
+    
+    //Now, read one byte, which will be the contents of the command register.
+    i2c->read(i2cAddress, &registerContents, 1);
+    
+    return registerContents;
+    
+}
+
+int CMPS03::readBearing(void){
+
+    char registerNumber = COMPASS_BEARING_WORD_REG;
+    char registerContents[2] = {0x00, 0x00};
+    
+    //First, send the number of register we wish to read,
+    //in this case, register numbers 2, 3, which hold the
+    //compass bearing as a 16-bit word.
+    i2c->write(i2cAddress, &registerNumber, 1);
+    
+    //Now read two bytes which will be the contents of
+    //these registers.
+    i2c->read(i2cAddress, registerContents, 2);
+    
+    //Register 2 [read first], was the high byte, followed by
+    //register 3 [read second], which was the low byte.
+    int bearing = ((int)registerContents[0] << 8) | ((int)registerContents[1]);
+    
+    return bearing;
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMPS03.h	Sat Nov 27 12:09:27 2010 +0000
@@ -0,0 +1,90 @@
+/**
+ * @author Aaron Berk
+ *
+ * @section LICENSE
+ *
+ * Copyright (c) 2010 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * CMPS03 digital compass module.
+ *
+ * Datasheet:
+ *
+ * http://www.robot-electronics.co.uk/htm/cmps3tech.htm
+ */
+
+#ifndef CMPS03_H
+#define CMPS03_H
+
+/**
+ * Includes
+ */
+#include "mbed.h"
+
+/**
+ * Defines
+ */
+#define CMPS03_DEFAULT_I2C_ADDRESS 0xC0
+
+//-----------
+// Registers
+//-----------
+#define SOFTWARE_REVISION_REG    0x0
+#define COMPASS_BEARING_WORD_REG 0x2
+
+/**
+ * CMPS03 digital compass module.
+ */
+class CMPS03 {
+
+    I2C* i2c;
+    int  i2cAddress;
+
+public:
+
+    /**
+     * Constructor.
+     *
+     * @param sda mbed pin to use for I2C SDA
+     * @param scl mbed pin to use for I2C SCL
+     * @param address I2C address of this device.
+     */
+    CMPS03(PinName sda, PinName scl, int address);
+
+    /**
+     * Reads the software revision register [register 0] on the device.
+     *
+     * @return The contents of the software revision register as a byte.
+     */
+    char readSoftwareRevision(void);
+
+    /**
+     * Reads the current bearing of the compass.
+     *
+     * @return The current bearing of the compass as a value between 0 and 3599,
+     *         representing 0 - 359.9 degrees.
+     */
+    int readBearing(void);
+
+};
+
+#endif /* CMPS03_H */