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