Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Wi-Go-MagnetometerTest EE202A_HW1_MH serialtoxively mbed_nanosec_timer ... more
Revision 0:63a8594a3866, committed 2013-05-19
- Comitter:
- SomeRandomBloke
- Date:
- Sun May 19 20:34:27 2013 +0000
- Child:
- 1:5a0e7a58d980
- Commit message:
- Initial library commit
Changed in this revision
| MAG3110.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MAG3110.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAG3110.cpp Sun May 19 20:34:27 2013 +0000
@@ -0,0 +1,87 @@
+
+#include "MAG3110.h"
+#include "mbed.h"
+
+/******************************************************************************
+ * Constructors
+ ******************************************************************************/
+MAG3110::MAG3110(I2C *i2c):
+ _i2c_address(0x1D), _i2c(i2c),_pc(NULL), _debug(false)
+{
+
+}
+
+MAG3110::MAG3110(I2C *i2c, Serial *pc):
+ _i2c_address(0x1D), _i2c(i2c), _pc(pc), _debug(true)
+{
+
+}
+
+// Read a single byte form 8 bit register, return as int
+int MAG3110::readReg(char regAddr)
+{
+ char cmd[1];
+
+ cmd[0] = regAddr;
+ _i2c->write(_i2c_address, cmd, 1);
+
+ cmd[0] = 0x00;
+ _i2c->read(_i2c_address, cmd, 1);
+ return (int)( cmd[0]);
+}
+
+
+// read a register per, pass first reg value, reading 2 bytes increments register
+// Reads MSB first then LSB
+int MAG3110::readVal(char regAddr)
+{
+ char cmd[2];
+
+ cmd[0] = regAddr;
+ _i2c->write(_i2c_address, cmd, 1);
+
+ cmd[0] = 0x00;
+ cmd[1] = 0x00;
+ _i2c->read(_i2c_address, cmd, 2);
+ return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
+}
+
+
+void MAG3110::begin()
+{
+ char cmd[2];
+
+ cmd[0] = MAG_CTRL_REG2;
+ cmd[1] = 0x80;
+ _i2c->write(_i2c_address, cmd, 2);
+
+ cmd[0] = MAG_CTRL_REG1;
+ cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
+ _i2c->write(_i2c_address, cmd, 2);
+}
+
+float MAG3110::getHeading()
+{
+ int xVal = readVal(MAG_OUT_X_MSB);
+ int yVal = readVal(MAG_OUT_Y_MSB);
+ return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI;
+}
+
+void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
+{
+ *xVal = readVal(MAG_OUT_X_MSB);
+ *yVal = readVal(MAG_OUT_Y_MSB);
+ *zVal = readVal(MAG_OUT_Z_MSB);
+}
+
+
+void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
+{
+ _avgX=(maxX+minX)/2;
+ _avgY=(maxY+minY)/2;
+}
+
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAG3110.h Sun May 19 20:34:27 2013 +0000
@@ -0,0 +1,96 @@
+/*
+ * MAG3110 Sensor Library for mbed
+ * TODO: Add proper header
+ */
+
+#ifndef MAG3110_H
+#define MAG3110_H
+
+#include "mbed.h"
+
+#define PI 3.14159265359
+
+#define MAG_ADDR 0x1D
+
+// define registers
+#define MAG_DR_STATUS 0x00
+#define MAG_OUT_X_MSB 0x01
+#define MAG_OUT_X_LSB 0x02
+#define MAG_OUT_Y_MSB 0x03
+#define MAG_OUT_Y_LSB 0x04
+#define MAG_OUT_Z_MSB 0x05
+#define MAG_OUT_Z_LSB 0x06
+#define MAG_WHO_AM_I 0x07
+#define MAG_SYSMOD 0x08
+#define MAG_OFF_X_MSB 0x09
+#define MAG_OFF_X_LSB 0x0A
+#define MAG_OFF_Y_MSB 0x0B
+#define MAG_OFF_Y_LSB 0x0C
+#define MAG_OFF_Z_MSB 0x0D
+#define MAG_OFF_Z_LSB 0x0E
+#define MAG_DIE_TEMP 0x0F
+#define MAG_CTRL_REG1 0x10
+#define MAG_CTRL_REG2 0x11
+
+// what should WHO_AM_I return?
+#define MAG_3110_WHO_AM_I_VALUE 0xC4
+
+
+// Fields in registers
+// CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac
+
+// Sampling rate from 80Hz down to 0.625Hz
+#define MAG_3110_SAMPLE80 0
+#define MAG_3110_SAMPLE40 0x20
+#define MAG_3110_SAMPLE20 0x40
+#define MAG_3110_SAMPLE10 0x60
+#define MAG_3110_SAMPLE5 0x80
+#define MAG_3110_SAMPLE2_5 0xA0
+#define MAG_3110_SAMPLE1_25 0xC0
+#define MAG_3110_SAMPLE0_625 0xE0
+
+// How many samples to average (lowers data rate)
+#define MAG_3110_OVERSAMPLE1 0
+#define MAG_3110_OVERSAMPLE2 0x08
+#define MAG_3110_OVERSAMPLE3 0x10
+#define MAG_3110_OVERSAMPLE4 0x18
+
+// read only 1 byte per axis
+#define MAG_3110_FASTREAD 0x04
+// do one measurement (even if in standby mode)
+#define MAG_3110_TRIGGER 0x02
+// put in active mode
+#define MAG_3110_ACTIVE 0x01
+
+// CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _
+// reset sensor after each reading
+#define MAG_3110_AUTO_MRST_EN 0x80
+// don't subtract user offsets
+#define MAG_3110_RAW 0x20
+// reset magnetic sensor after too-large field
+#define MAG_3110_MAG_RST 0x10
+
+// DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR
+#define MAG_3110_ZYXDR 0x08
+
+class MAG3110
+{
+ private:
+ short _i2c_address;
+ I2C *_i2c;
+ Serial *_pc;
+ bool _debug;
+ int _avgX, _avgY; //, _newX, _tempXmin, _tempXmax, _newY, _tempYmin, _tempYmax;
+
+
+ public:
+ MAG3110(I2C *i2c);
+ MAG3110(I2C *i2c, Serial *pc); //pass serial for debug
+ void begin();
+ int readReg(char regAddr);
+ int readVal(char regAddr);
+ float getHeading();
+ void getValues(int *xVal, int *yVal, int *zVal);
+ void setCalibration(int minX, int maxX, int minY, int maxY);
+};
+#endif
\ No newline at end of file
NXP MAG3110 Magnetometer