bmp085_bst.c
Go to the documentation of this file.
00001 /*  $Date: 2009/10/23 $
00002  *  $Revision: 1.2 $
00003  */
00004 
00083 #include "bmp085/bmp085_bst.h"
00084 
00085 
00086 bmp085_t *p_bmp085 = 0; 
00098 int bmp085_init( bmp085_t *bmp085 ) 
00099 {
00100   char comres = 0;
00101   unsigned char data;
00102   long dummy;
00103 
00104 
00105   p_bmp085 = bmp085; /* assign BMP085 ptr */
00106   p_bmp085->sensortype = E_SENSOR_NOT_DETECTED;
00107   p_bmp085->dev_addr = BMP085_I2C_ADDR; /* preset BMP085 I2C_addr */
00108   comres += p_bmp085->BMP085_BUS_READ_FUNC(p_bmp085->dev_addr, BMP085_CHIP_ID__REG, &data, 1);  /* read Chip Id */
00109   
00110   p_bmp085->chip_id = BMP085_GET_BITSLICE(data, BMP085_CHIP_ID);  
00111   p_bmp085->number_of_samples = 1;  
00112   p_bmp085->oversampling_setting=0;
00113 
00114   if( p_bmp085->chip_id == BMP085_CHIP_ID )
00115   {            /* get bitslice */
00116     p_bmp085->sensortype = BOSCH_PRESSURE_BMP085;
00117     
00118     comres += p_bmp085->BMP085_BUS_READ_FUNC(p_bmp085->dev_addr, BMP085_VERSION_REG, &data, 1); /* read Version reg */
00119     
00120     p_bmp085->ml_version = BMP085_GET_BITSLICE(data, BMP085_ML_VERSION);        /* get ML Version */
00121     p_bmp085->al_version = BMP085_GET_BITSLICE(data, BMP085_AL_VERSION);        /* get AL Version */
00122     bmp085_get_cal_param( ); /* readout bmp085 calibparam structure */
00123   }
00124   else
00125   {
00126     p_bmp085->sensortype = BOSCH_PRESSURE_SMD500;
00127     p_bmp085->smd500_t_resolution = SMD500_T_RESOLUTION_16BIT;
00128     p_bmp085->smd500_masterclock = SMD500_MASTERCLOCK_32768HZ;
00129     smd500_get_cal_param();
00130 
00131     /* calculate B1*/
00132     dummy =  (p_bmp085->cal_param.ac3 + SMD500_PARAM_M3);
00133     dummy *= SMD500_PARAM_ME;
00134     dummy >>= 11;
00135     dummy += SMD500_PARAM_MF;   
00136     p_bmp085->cal_param.b1 = dummy;
00137     
00138     /* calculate B2*/
00139     dummy = (p_bmp085->cal_param.ac2 + SMD500_PARAM_M2);
00140     dummy *= p_bmp085->cal_param.b1;
00141     dummy >>= 4;
00142     dummy /= (p_bmp085->cal_param.ac3 + SMD500_PARAM_M3);   // calculate B2      
00143     p_bmp085->cal_param.b2 = dummy;
00144 
00145     p_bmp085->sensortype = BOSCH_PRESSURE_SMD500;
00146     
00147     p_bmp085->cal_param.ac1 += SMD500_PARAM_M1;
00148     p_bmp085->cal_param.ac1 <<= 1;
00149 
00150     p_bmp085->cal_param.ac2 += SMD500_PARAM_M2;
00151     p_bmp085->cal_param.ac2 <<= 2;
00152 
00153     p_bmp085->cal_param.ac3 += SMD500_PARAM_M3;
00154     p_bmp085->cal_param.ac3 <<= 3;
00155 
00156     p_bmp085->cal_param.ac4 += SMD500_PARAM_M4;
00157     p_bmp085->cal_param.ac4 <<= 1;
00158 
00159     p_bmp085->cal_param.ac5 += SMD500_PARAM_M5;
00160     p_bmp085->cal_param.ac5 <<= 3;
00161 
00162     p_bmp085->cal_param.ac6 += SMD500_PARAM_M6;
00163     p_bmp085->cal_param.ac6 <<= 3;
00164     
00165     p_bmp085->cal_param.mb = SMD500_PARAM_MB;
00166     p_bmp085->cal_param.mc = SMD500_PARAM_MC;
00167     p_bmp085->cal_param.md = SMD500_PARAM_MD;   
00168   }
00169 
00170   return comres;
00171 }
00172 
00177 //int bmp085_read_cal_param(void)
00178 int bmp085_get_cal_param( void )
00179 {
00180   int comres;
00181   unsigned char data[22];
00182   comres = p_bmp085->BMP085_BUS_READ_FUNC( p_bmp085->dev_addr, BMP085_PROM_START__ADDR, data, BMP085_PROM_DATA__LEN );
00183   
00184   /*parameters AC1-AC6*/
00185   p_bmp085->cal_param.ac1 =  (data[0] <<8) | data[1];
00186   p_bmp085->cal_param.ac2 =  (data[2] <<8) | data[3];
00187   p_bmp085->cal_param.ac3 =  (data[4] <<8) | data[5];
00188   p_bmp085->cal_param.ac4 =  (data[6] <<8) | data[7];
00189   p_bmp085->cal_param.ac5 =  (data[8] <<8) | data[9];
00190   p_bmp085->cal_param.ac6 = (data[10] <<8) | data[11];
00191   
00192   /*parameters B1,B2*/
00193   p_bmp085->cal_param.b1 =  (data[12] <<8) | data[13];
00194   p_bmp085->cal_param.b2 =  (data[14] <<8) | data[15];
00195   
00196   /*parameters MB,MC,MD*/
00197   p_bmp085->cal_param.mb =  (data[16] <<8) | data[17];
00198   p_bmp085->cal_param.mc =  (data[18] <<8) | data[19];
00199   p_bmp085->cal_param.md =  (data[20] <<8) | data[21];
00200   
00201   return comres;  
00202 }
00203 
00208 //int smd500_read_cal_param(void) 
00209 int smd500_get_cal_param( void ) 
00210 {
00211   unsigned char data[SMD500_PROM_DATA__LEN];    
00212   int comres;
00213   comres = p_bmp085->BMP085_BUS_READ_FUNC( p_bmp085->dev_addr, SMD500_PROM_START__ADDR, data, SMD500_PROM_DATA__LEN );
00214 
00215   p_bmp085->cal_param.ac1 = (unsigned short) ( (data[0] <<8) | data[1] ) & 0x3FFF;
00216   p_bmp085->cal_param.ac2 = ( (data[0] & 0xC0) >> 6 ) + ( (data[2] & ~0x01) << 1 );
00217   p_bmp085->cal_param.ac3 = ( (data[2] & 0x01) << 8 ) + data[3];
00218   p_bmp085->cal_param.ac4 = ( (data[4] & 0x1f) << 8 ) + data[5];
00219   p_bmp085->cal_param.ac5 = ( (data[4] & ~0x1f) >> 5 ) + ( (data[6] & 0xf0) >> 1 );
00220   p_bmp085->cal_param.ac6 = ( (data[6] & 0x0f) << 8 ) + data[7];
00221   return comres;
00222 }
00223 
00224 
00232 //short bmp085_calc_temperature(unsigned long ut) 
00233 short bmp085_get_temperature( unsigned long ut ) 
00234 {
00235   short temperature;
00236   long x1,x2,x3,x4,y2,y3,y4;    
00237 
00238   if( p_bmp085->sensortype == BOSCH_PRESSURE_BMP085 )
00239   {
00240     x1 = (((long) ut - (long) p_bmp085->cal_param.ac6) * (long) p_bmp085->cal_param.ac5) >> 15;
00241     x2 = ((long) p_bmp085->cal_param.mc << 11) / (x1 + p_bmp085->cal_param.md);
00242     p_bmp085->param_b5 = x1 + x2;
00243   }
00244   else // SMD500
00245   {
00246     if( p_bmp085->smd500_t_resolution == SMD500_T_RESOLUTION_16BIT ) // check for SMD500 temp resolution mode
00247     {
00248       x1 = ((unsigned long) ((( ut * SMD500_PARAM_MJ) >> 16) - p_bmp085->cal_param.ac6));
00249     }
00250     else //13BIT
00251     {
00252       x1 = ut * 8  - p_bmp085->cal_param.ac6;
00253     }
00254 
00255     x2 = (x1 * x1) >> 13;
00256     y2 = (SMD500_PARAM_MB * x2) >> 13;
00257     x3 = (x2 * x1) >> 16;
00258     y3 = (SMD500_PARAM_MC * x3) >> 12;
00259     x4 = (x2 * x2) >> 16;
00260     y4 = (SMD500_PARAM_MD * x4) >> 14;
00261 
00262     p_bmp085->param_b5 = (((p_bmp085->cal_param.ac5) * ( (2*x1) + y2 + y3 + y4))) >> 13; // temperature in (1/160)°C
00263   }
00264   temperature = ((p_bmp085->param_b5 + 8) >> 4);  // temperature in 0.1°C
00265 
00266   return temperature;
00267 }
00268 
00278 //long bmp085_calc_pressure(unsigned long up)
00279 long bmp085_get_pressure( unsigned long up )
00280 {
00281    long pressure,x1,x2,x3,b3,b6;
00282    unsigned long b4, b7;
00283    
00284    b6 = p_bmp085->param_b5 - 4000;
00285    //*****calculate B3************
00286    x1 = (b6*b6) >> 12;           
00287    x1 *= p_bmp085->cal_param.b2;
00288    x1 >>= 11;
00289 
00290    x2 = (p_bmp085->cal_param.ac2*b6);
00291    x2 >>= 11;
00292 
00293    x3 = x1 +x2;
00294 
00295    b3 = (((((long)p_bmp085->cal_param.ac1 )*4 + x3) <<p_bmp085->oversampling_setting) + 2) >> 2;
00296 
00297    //*****calculate B4************
00298    x1 = (p_bmp085->cal_param.ac3* b6) >> 13;
00299    x2 = (p_bmp085->cal_param.b1 * ((b6*b6) >> 12) ) >> 16;
00300    x3 = ((x1 + x2) + 2) >> 2;
00301    b4 = (p_bmp085->cal_param.ac4 * (unsigned long) (x3 + 32768)) >> 15;
00302      
00303    b7 = ((unsigned long)(up - b3) * (50000>>p_bmp085->oversampling_setting));   
00304    if( b7 < 0x80000000 )
00305    {
00306      pressure = (b7 << 1) / b4;
00307    }
00308    else
00309    { 
00310      pressure = (b7 / b4) << 1;
00311    }
00312    
00313    x1 = pressure >> 8;
00314    x1 *= x1;
00315    x1 = (x1 * SMD500_PARAM_MG) >> 16;
00316    x2 = (pressure * SMD500_PARAM_MH) >> 16;
00317    pressure += (x1 + x2 + SMD500_PARAM_MI) >> 4;        // pressure in Pa  
00318 
00319    return pressure;
00320 }
00321 
00322 
00327 //unsigned short bmp085_read_ut ()
00328 unsigned short bmp085_get_ut( void )
00329 {
00330   unsigned short ut;
00331   unsigned char data[2];    
00332   unsigned char ctrl_reg_data;
00333   int wait_time;
00334   int comres;
00335 
00336   if( p_bmp085->chip_id == BMP085_CHIP_ID ) /* get bitslice */
00337   { 
00338     ctrl_reg_data = BMP085_T_MEASURE;
00339     wait_time = BMP085_TEMP_CONVERSION_TIME;
00340   }
00341   else
00342   {
00343     ctrl_reg_data = SMD500_T_MEASURE + p_bmp085->smd500_t_resolution + p_bmp085->smd500_masterclock;
00344   }
00345 
00346   // wait_time can be 9 ms for 13 bit smd500_t_resolution
00347   if( p_bmp085->smd500_t_resolution == SMD500_T_RESOLUTION_13BIT )
00348   { 
00349      wait_time = SMD500_TEMP_CONVERSION_TIME_13;
00350   }
00351   else
00352   {
00353      wait_time = SMD500_TEMP_CONVERSION_TIME_16;
00354   }
00355   comres = p_bmp085->BMP085_BUS_WRITE_FUNC(p_bmp085->dev_addr, BMP085_CTRL_MEAS_REG, &ctrl_reg_data, 1);
00356   
00357   
00358   p_bmp085->delay_msec (wait_time);  
00359   comres += p_bmp085->BMP085_BUS_READ_FUNC(p_bmp085->dev_addr, BMP085_ADC_OUT_MSB_REG, data, 2);
00360   ut = (data[0] <<8) | data[1];
00361   
00362   return ut;
00363 }
00364 
00365 
00371 //unsigned long bmp085_read_up ()
00372 unsigned long bmp085_get_up( void )
00373 {
00374   int i;
00375   unsigned long up = 0;
00376   unsigned char data[3];    
00377   unsigned char ctrl_reg_data;
00378   int comres = 0;
00379   if( p_bmp085->chip_id == BMP085_CHIP_ID )
00380   { 
00381     ctrl_reg_data = BMP085_P_MEASURE + (p_bmp085->oversampling_setting << 6);
00382     comres = p_bmp085->BMP085_BUS_WRITE_FUNC( p_bmp085->dev_addr, BMP085_CTRL_MEAS_REG, &ctrl_reg_data, 1 );
00383     
00384     p_bmp085->delay_msec ( 2 + (3 << (p_bmp085->oversampling_setting) ) );
00385     comres += p_bmp085->BMP085_BUS_READ_FUNC( p_bmp085->dev_addr, BMP085_ADC_OUT_MSB_REG, data, 3 );
00386     
00387     up = (((unsigned long) data[0] << 16) | ((unsigned long) data[1] << 8) | (unsigned long) data[2]) >> (8-p_bmp085->oversampling_setting);
00388     p_bmp085->number_of_samples = 1;
00389   }
00390   else
00391   {
00392     // SMD500 
00393     ctrl_reg_data = SMD500_P_MEASURE + p_bmp085->smd500_masterclock;
00394     p_bmp085->number_of_samples = (1 << (p_bmp085->oversampling_setting));
00395     for( i = 0; i < p_bmp085->number_of_samples; i++ )
00396     {
00397       comres += p_bmp085->BMP085_BUS_WRITE_FUNC( p_bmp085->dev_addr, BMP085_CTRL_MEAS_REG, &ctrl_reg_data, 1 );
00398       p_bmp085->delay_msec (34);
00399       
00400       comres += p_bmp085->BMP085_BUS_READ_FUNC( p_bmp085->dev_addr, BMP085_ADC_OUT_MSB_REG, data, 2 );
00401       up += (((unsigned long)data[0] <<8) | (unsigned long)data[1]);
00402     }    
00403   }
00404   return up;
00405 }
00406 
00407 


bmp085
Author(s): Philip Roan
autogenerated on Sat Dec 28 2013 16:49:31