Could not get pressure or temperature measurements using BMP085, any problem in the code. Thanks
// BMP085 -- Atmospheric Pressure / Altitude Sensor
#include "defines.h"
#define BMP085_ADDRESS 0x77
#define START 0x00000007
#define REPEATED_START 0x0000000b
#define CONT 0x00000009
#define STOP 0x00000005
#define MEASURE_TEMP 0x2e
#define MEASURE_PRESSURE 0x34
#define BMP_START_MEASURE 0xf4
#define BMP_READ_MEASURE 0xf6
const unsigned char OSS = 0;
typedef struct
{
short ac1,ac2,ac3,b1,b2,mb,mc,md;
unsigned short ac4,ac5,ac6;
}coeffs;
typedef struct
{
long temp, pressure;
} BMP_vals;
const unsigned char OSS = 0;
long b5;
//short *ut;
// start measurement seq for ut and up
void start_measurement(unsigned char device_addr, unsigned char EEPROM_addr, unsigned char cmd)
{
I2C1_MSA_R=device_addr;
I2C1_MCS_R=START;
I2C1_MDR_R=EEPROM_addr;
while (I2C1_MCS_R & I2C_MCS_BUSY);
I2C1_MDR_R=cmd;
I2C1_MCS_R=STOP;
while (I2C1_MCS_R & I2C_MCS_BUSY);
}
// get values of ut and up from the device
unsigned char *get_measurement(unsigned device_addr, unsigned EEPROM_addr,int no_bytes)
{
static unsigned char ch[3];
I2C1_MSA_R=device_addr;
I2C1_MCS_R=START;
I2C1_MDR_R=EEPROM_addr;
while (I2C1_MCS_R & I2C_MCS_BUSY);
I2C1_MCS_R=STOP;
while (I2C1_MCS_R & I2C_MCS_BUSY);
I2C1_MSA_R=device_addr+1;
I2C1_MCS_R=REPEATED_START;
while (I2C1_MCS_R & I2C_MCS_BUSY);
ch[0]=I2C1_MDR_R;
if (no_bytes==3)
{
I2C1_MCS_R=CONT;
while (I2C1_MCS_R & I2C_MCS_BUSY);
ch[1]=I2C1_MDR_R;
I2C1_MCS_R=STOP;
while (I2C1_MCS_R & I2C_MCS_BUSY);
ch[2]=I2C1_MDR_R;
}
else
{
I2C1_MCS_R=STOP;
while (I2C1_MCS_R & I2C_MCS_BUSY);
ch[1]=I2C1_MDR_R;
}
return ch;
}
//read no of bytes from the device
unsigned char *get_chars(unsigned char slave_addr, unsigned char start_addr, int len)
{
static unsigned char ch_ptr[50];
int i;
I2C1_MSA_R=slave_addr<<1;
I2C1_MCS_R=START;
I2C1_MDR_R=start_addr;
while (I2C1_MCS_R & I2C_MCS_BUSY);
I2C1_MSA_R=slave_addr<<1+1;
I2C1_MCS_R=REPEATED_START;
while (I2C1_MCS_R & I2C_MCS_BUSY);
ch_ptr[0]=I2C1_MDR_R;
for (i=1;i<len-1;i++)
{
I2C1_MCS_R=CONT;
while (I2C1_MCS_R & I2C_MCS_BUSY);
ch_ptr[i]=I2C1_MDR_R;
}
I2C1_MCS_R=STOP;
while (I2C1_MCS_R & I2C_MCS_BUSY);
ch_ptr[len-1]=I2C1_MDR_R;
return ch_ptr;
}
//calc temperature
int calc_temp(short ut, coeffs *BMP_coeffs)
{
long x1, x2;
// coeffs *BMP_coeffs;
x1 = (((long)ut - (long)BMP_coeffs->ac6)*(long)BMP_coeffs->ac5) >> 15;
x2 = ((long)BMP_coeffs->mc << 11)/(x1 + BMP_coeffs->md);
b5 = x1 + x2;
return ((b5 + 8)>>4);
}
//calc presssure
long calc_pressure(long up, coeffs *BMP_coeffs)
{
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// Calculate B3
x1 = (BMP_coeffs->b2 * (b6 * b6)>>12)>>11;
x2 = (BMP_coeffs->ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)BMP_coeffs->ac1)*4 + x3)<<OSS) + 2)>>2;
// Calculate B4
x1 = (BMP_coeffs->ac3 * b6)>>13;
x2 = (BMP_coeffs->b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (BMP_coeffs->ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
return p;
}
BMP_vals get_BMP_vals (void)
{
// int temp;
short *ut;
long temp,pressure,*up;
coeffs *BMP_coeffs;
BMP_vals val;
BMP_coeffs=(coeffs*)get_chars(BMP085_ADDRESS,0xaa,22);
start_measurement(BMP085_ADDRESS,BMP_START_MEASURE,MEASURE_TEMP);
delay(_5ms);
ut=(short*)get_measurement(BMP085_ADDRESS,BMP_READ_MEASURE,2);
start_measurement(BMP085_ADDRESS,BMP_START_MEASURE,MEASURE_PRESSURE);
delay(_5ms);
up=(long*)get_measurement(BMP085_ADDRESS,BMP_READ_MEASURE,3);
val.temp=calc_temp(*ut,BMP_coeffs);
val.pressure=calc_pressure(*up,BMP_coeffs);
return val;
}