Skip to content

Instantly share code, notes, and snippets.

@analog-io
Created April 10, 2015 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save analog-io/fb33dcbfad393dc396d6 to your computer and use it in GitHub Desktop.
Save analog-io/fb33dcbfad393dc396d6 to your computer and use it in GitHub Desktop.
I2C interface for MAG3110 wirtten for electric imp
// Define the Sensor Address
const ADDR = 0x1C;
// Define register map
const DR_STATUS = "\x00";
const OUT_X_MSB = "\x01";
const OUT_X_LSB = "\x02";
const OUT_Y_MSB = "\x03";
const OUT_Y_LSB = "\x04";
const OUT_Z_MSB = "\x05";
const OUT_Z_LSB = "\x06";
const WHO_AM_I = "\x07";
const SYSMOD = "\x08";
const OFF_X_MSB = "\x09";
const OFF_X_LSB = "\x0A";
const OFF_Y_MSB = "\x0B";
const OFF_Y_LSB = "\x0C";
const OFF_Z_MSB = "\x0D";
const OFF_Z_LSB = "\x0E";
const DIE_TEMP = "\x0F";
const CTRL_REG1 = "\x10";
const CTRL_REG2 = "\x11";
const DR_1280 = "\x00";
const DR_640 = "\x20";
const DR_320 = "\x40";
const DR_160 = "\x50";
const DR_80 = "\x80";
const DR_40 = "\xA0";
const DR_20 = "\xC0";
const DR_10 = "\xE0";
const OSR_16 = "\x00";
const OSR_32 = "\x08";
const OSR_64 = "\x10";
const OSR_128 = "\x18";
const FAST_READ = "\x04";
const TRIGGER_MEAS = "\x02";
const ACTIVE = "\x01";
const AUTO_MRST_EN = "\x80";
const RAW = "\x20";
const MAG_RST = "\x10";
// Specify the update rate in seconds
const update_rate = 5;
// Define the i2c periphrial being used
i2c <- hardware.i2c89;
int <- hardware.pin7;
x_min <- 32767
x_min_cyc <- 32767
y_min <- 32767
z_min <- 32767
x_max <- -32767
x_max_cyc <- -32767
y_max <- -32767
z_max <- -32767
x_prev <- 0
x_count <- 0
x_int <- 0
x_int_neg <- 0
x_hist <- false
x_dd <- false
x_hf <- false
z_dd <- false
debounce <- 130
x_start <- 0
x_trigs <- 0
x_trigs_hf <- 0
y_trigs <- 0
z_trigs <- 0
x_thresh <- 0
y_thresh <- 0
z_thresh <- 0
x_spread <- 0
y_spread <- 0
z_spread <- 0
calibrate <- 0;
buffer_size <- 800;
index <- 0;
buffer <- array(buffer_size);
// Configure said periphrial
i2c.configure(CLOCK_SPEED_400_KHZ);
i2c.write(ADDR,CTRL_REG2+AUTO_MRST_EN)
i2c.write(ADDR,CTRL_REG1+"\x01")
local data = i2c.read(ADDR,OUT_X_MSB,6);
function mag_isr() {
if (int.read()==1)
{
local data = i2c.read(ADDR,OUT_X_MSB,6);
if (calibrate ==0) {
server.log("calibrate")
calibrate <- 1;
i2c.write(ADDR,OFF_X_MSB+data)
}
else
{
local x = data[0]<<8 | data[1]
if (x & 0x8000) {x = -((~x & 0x7FFF) + 1);}
local y = data[2]<<8 | data[3]
if (y & 0x8000) {y = -((~y & 0x7FFF) + 1);}
local z = data[4]<<8 | data[5]
if (z & 0x8000) {z = -((~z & 0x7FFF) + 1);}
if (x<x_min) {x_min = x; x_spread = x_max-x_min; x_thresh = x_min+x_spread/2}
if (x<x_min_cyc) {x_min_cyc = x}
if (y<y_min) {y_min = y; y_spread = y_max-y_min; y_thresh = y_min+y_spread/2}
if (z<z_min) {z_min = z; z_spread = z_max-z_min; z_thresh = z_min+z_spread/2}
if (x>x_max) {x_max = x; x_spread = x_max-x_min; x_thresh = x_min+x_spread/2}
if (x>x_max_cyc) {x_max_cyc = x}
if (y>y_max) {y_max = y; y_spread = y_max-y_min; y_thresh = y_min+y_spread/2}
if (z>z_max) {z_max = z; z_spread = z_max-z_min; z_thresh = z_min+z_spread/2}
if (x>x_thresh) {
if (!x_hf){
x_hf = true
x_trigs_hf++
}
x_count++
x_int += (x-x_thresh)
if (x_int>debounce) {
if (!x_dd) {
x_trigs++
x_dd=true
x_int_neg = 0
}
}
}
else {
x_hf = false
x_int_neg += (x_thresh-x)
if (x_int_neg>debounce) {
x_int = 0
x_dd = false
}
}
if (index<buffer_size)
{
buffer[index] = [x,y,z]
index++
}
else
{
index = 0
agent.send("buffer",[buffer,[x_min,x_max,x_thresh,x_trigs_hf,x_trigs],[y_min,y_max,y_thresh,x_max_cyc-x_min_cyc,y_trigs],[z_min,z_max,z_thresh,z_spread,z_trigs]])
x_trigs = 0
x_trigs_hf = 0
x_max_cyc = -32767
x_min_cyc = 32767
}
}
}
}
int.configure(DIGITAL_IN, mag_isr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment