Code Monkey home page Code Monkey logo

mpl115a2's Introduction

MPL115A2 reader for Raspberry Pi

A program to read an absolute pressure sensor MPL115A2 for Raspberry Pi.

MPL115A2 is an absolute pressure sensor with a digital I2C output. This program reads the sensor and shows current absolute pressure. Make sure you have connected the MPL115A2 sensor to your Raspberry Pi.

Requirements

  • wiringPi library
  • An MPL115A2 sensor connected to I2C bus of Respberry Pi

Installation

make
make install

or just put getMPL115A2.pl on your desired place. Add your user to the group 'i2c'.

Usage

$ getMPL115A2
1006.8

or

$ getMPL115A2.pl
1005.8

Reference

MPL115A2 data sheet

Contact

Osamu Mizuno / omzn

mpl115a2's People

Contributors

omzn avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

mpl115a2's Issues

Compile for Openelec

When compile MPL115A2.c for Openelec and start on Raspberry PI MPL115A2 not send 0x12 to start conversion on i2c, and recive every time same data of measure. When start before MPL115A2 command i2cset -y 1 0x60 0x12 0x00, MPL115A2 read new data.

Compile for Openelec fix problem

I make several changes this is new c file:

include <stdio.h>

include <stdlib.h>

include <stdint.h>

include <errno.h>

include <string.h>

include <fcntl.h>

include <sys/ioctl.h>

include <unistd.h>

include <math.h>

include "wiringPi.h"

include "wiringPiI2C.h"

include "getMPL115A2.h"

int swap16(int x) {
return ( ((x & 0x00ff) << 8) + ((x & 0xff00) >> 8));
}

double conv_frac(int x,int fbits,int zero) {
int valsign = 1;
int valint = 0;
double valfrac = 0;
int mask = 0x0001;
int i;

if (x & 0x8000) {
valsign = -1;
x = ~x + 1;
}
x = x >> zero ;
for (i=fbits;i>0;i--) {
if (x & mask) {
valfrac += 1/(pow(2,i));
}
mask <<= 1;
}
valint = (x & 0x7fff) >> fbits;

return valsign * (valint + valfrac);
}

int get_reg(int fd, int reg) {
int data;
do {
data = wiringPiI2CReadReg16(fd,reg);
} while (data < 0);
return data;
}

void readCoefficients(int i2c, struct readcoeff *coeff)
{
int e;
int a0,b1,b2,c12;

if((e = wiringPiI2CWrite(i2c, 0x04))<0)
printf("error opening i2c %d channel\n\r",i2c);

coeff->a0 = conv_frac(swap16((a0 = get_reg(i2c,0x04))),3,0);
coeff->b1 = conv_frac(swap16((b1 = get_reg(i2c,0x06))),13,0);
coeff->b2 = conv_frac(swap16((b2 = get_reg(i2c,0x08))),14,0);
coeff->c12 = conv_frac(swap16((c12 = get_reg(i2c,0x0a))),22,2);

//// printf("receve a0 hex:0x%i size:%i\n\r",a0,sizeof(a0));
//// printf("receve b1 hex:0x%i size:%i\n\r",b1,sizeof(b1));
//// printf("receve b2 hex:0x%i size:%i\n\r",b2,sizeof(b2));
//// printf("receve c12 hex:0x%i size:%i\n\r",c12,sizeof(c12));
//// printf("opening i2c %d 0x00 channel %d\n\r",i2c,e);
}

void readmeasure(int i2c, struct readmeas *meas)
{
int e;
double padc,tadc;

if((e = wiringPiI2CWriteReg8(i2c, 0x12, 0x00))<0)
printf("error opening i2c channel\n\r");
usleep(100000);

meas->padc = swap16((padc = get_reg(i2c,0x00))) >> 6;
meas->tadc = swap16((tadc = get_reg(i2c,0x02))) >> 6;
// a0 = conv_frac(swap16(get_reg(i2c,0x04)),3,0);
// b1 = conv_frac(swap16(get_reg(i2c,0x06)),13,0);
// b2 = conv_frac(swap16(get_reg(i2c,0x08)),14,0);
// c12 = conv_frac(swap16(get_reg(i2c,0x0a)),22,2);

//// printf("receve padc hex:0x%i size:%i\n\r",padc,sizeof(padc));
//// printf("receve tadc hex:0x%i size:%i\n\r",tadc,sizeof(tadc));
//// printf("opening i2c %d 0x12 channel %d\n\r",i2c,e);
}

int initMPL115A2(void)
{
int i2c;
i2c = wiringPiI2CSetup(MPL115A2_DEV_ID);
if (i2c < 0) {
return -1;
}
return i2c;
}

int main(int argc, char *argv[])
{
int i2c,e;
double p_comp,p_hpa,p_temp;
struct readcoeff coeff;
struct readmeas meas;
int flags = 0;
int all = 0;

if (argc != 0){
while (1+flags < argc && argv[1+flags][0] == '-') {
switch (argv[1+flags][1]) {
case 'V': all = -1; break;
case 't': all = 1; break;
case 'p': all = 2; break;
default:
fprintf(stderr, "Warning: Unsupported flag "
""-%c"!\n", argv[1+flags][1]);
exit(1);
}
flags++;
}};

i2c = initMPL115A2();
//// printf("opening i2c channel %d\n\r",i2c);

readCoefficients(i2c,&coeff);
readmeasure(i2c,&meas);
close(i2c);

// padc = ((swap16(get_reg(i2c,0x00)) << 8) | get_reg(i2c,0x00)) >> 6;
// tadc = ((swap16(get_reg(i2c,0x02)) <<8) | get_reg(i2c,0x02)) >> 6;

// a0 = ((swap16(get_reg(i2c,0x04)) << 8) | get_reg(i2c,0x04))/8;
// b1 = ((swap16(get_reg(i2c,0x06)) << 8) | get_reg(i2c,0x06))/8192;
// b2 = ((swap16(get_reg(i2c,0x08)) << 8) | get_reg(i2c,0x08))/16384;
// c12 = ((swap16(get_reg(i2c,0x0a)) << 8) | get_reg(i2c,0x0a)) >> 2;
// c12 /= 4194304.0;

p_comp = coeff.a0 + (coeff.b1 + coeff.c12 * meas.tadc) * meas.padc + coeff.b2 * meas.tadc;
p_hpa = ((65.0/1023.0) * p_comp + 50) * 10;
p_temp = ( meas.tadc - 498.0 ) / -5.35 +25.0;
switch (all) {
case -1: printf("getMPL115A2 Version 2.0\n"); break;
case 0: printf("pressure %6.1f; temperature %6.1f\n", p_hpa, p_temp); break;
case 1: printf("%6.1f\n",p_temp); break;
case 2: printf("%6.1f\n",p_hpa); break;
}
//// printf("%6.6f %6.6f %6.6f %6.6f %6.1f %6.1f %6.6f\n",coeff.a0,coeff.b1,coeff.b2,coeff.c12,meas.padc,meas.tadc,p_comp);
return 0 ;
}

add new .h file

define MPL115A2_DEV_ID 0x60

define MPL115A2_REGISTER_PRESSURE_MSB 0x00

define MPL115A2_REGISTER_PRESSURE_LSB 0x01

define MPL115A2_REGISTER_TEMP_MSB 0x02

define MPL115A2_REGISTER_TEMP_LSB 0x03

define MPL115A2_REGISTER_A0_COEFF_MSB 0x04

define MPL115A2_REGISTER_A0_COEFF_LSB 0x05

define MPL115A2_REGISTER_B1_COEFF_MSB 0x06

define MPL115A2_REGISTER_B1_COEFF_LSB 0x07

define MPL115A2_REGISTER_B2_COEFF_MSB 0x08

define MPL115A2_REGISTER_B2_COEFF_LSB 0x09

define MPL115A2_REGISTER_C12_COEFF_MSB 0x0A

define MPL115A2_REGISTER_C12_COEFF_LSB 0x0B

define MPL115A2_REGISTER_STARTCONVERSION 0x12

struct readcoeff {
double a0;
double b1;
double b2;
double c12;
};

struct readmeas {
double padc;
double tadc;
};
only need to replace arg with defs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.