// CCS library compilation [2014]
// Driver para el sensor de presión barométrica BMP085.
// Programador: Moyano Jonathan
// Compilador: CCS v4.140
// Versión de la librería: v1.0 Revisión: 0.5
// Fecha de modificación: Enero de 2014.
// Incluimos librerías utilizadas.
#include "BMP085.h"
// Desarrollo de funciones de usuario.
int8 BMP085ReadByte(int8 address){
// Variables locales.
int8 data=0x00;
i2c_start();
i2c_write(BMP085_ADDRESS);
i2c_write(address);
i2c_start();
i2c_write(BMP085_ADDRESS | 0x01 );
data=i2c_read(0);
i2c_stop();
return(data);
}
int16 BMP085ReadInt(int8 address){
// Variables locales.
int8 msb=0x00;
int8 lsb=0x00;
int16 temp=0x00;
i2c_start();
i2c_write(BMP085_ADDRESS);
i2c_write(address);
i2c_start();
i2c_write(BMP085_ADDRESS | 0x01 );
msb = i2c_read();
lsb = i2c_read(0);
i2c_stop();
temp = make16(msb, lsb);
return(temp);
}
void BMP085WriteByte(int8 address, int8 data){
i2c_start();
i2c_write(BMP085_ADDRESS);
i2c_write(address);
i2c_write(data);
i2c_stop();
}
void bmp085Calibration(void) {
// Leemos de la EEPROM del BMP085, los factores de calibración.
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
// Calculamos los factores de calibración en punto flotante.
_c3 = 0.0048828125 * ac3;
_c4 = 0.000000030517578125 * ac4;
_c5 = 0.00000019073486328125 * ac5;
_c6 = (float)ac6;
_b1 = 0.00002384185791015625 * b1;
_mc = 0.08 * mc;
_md = (float)md / 160;
// Cálculo de constantes polinómicas.
_x0 = (float)ac1;
_x1 = 0.01953125 * ac2;
_x2 = 0.000762939453125 * b2;
_y0 = _c4 * 32768;
_y1 = _c4 * _c3;
_y2 = _c4 * _b1;
_p0 = 2.364375;
_p1 = 0.992984;
_p2 = 0.000004421;
}
int16 BMP085ReadUT(void){
// Variables locales.
int16 ut;
BMP085WriteByte(0xF4, 0x2E);
delay_ms(5);
ut = BMP085ReadInt(0xF6);
return((float)ut);
}
int32 bmp085ReadUP(void){
// Variables locales.
int8 msb, lsb, xlsb;
float p;
BMP085WriteByte(0xF4, (0x34 + (OVS_S<<6)) );
switch (OVS_S)
{
case 0: delay_ms(5); break;
case 1: delay_ms(8); break;
case 2: delay_ms(14); break;
case 3: delay_ms(26); break;
}
msb = BMP085ReadByte(0xF6);
lsb = BMP085ReadByte(0xF7);
xlsb = BMP085ReadByte(0xF8);
p = (256*msb) + lsb + (xlsb/256);
return(p);
}
float BMP085GetTemp(float _tu){
// Variables locales.
float alpha, T;
alpha = _c5 * (_tu - _c6);
T = alpha + (_mc/(alpha + _md));
_s = T - 25;
return(T);
}
float BMP085GetPressure(float _pu){
// Variables locales.
float x, y, z;
float P;
x = _x2*_s*_s + _x1*_s + _x0;
y = _y2*_s*_s + _y1*_s + _y0;
z = ((float)_pu - x) / y;
P = _p2*z*z + _p1*z + _p0;
P += P_CORRECTION;
return(P);
}
float BMP085Pressure(boolean getTemp){
if (getTemp)
_Temp = BMP085GetTemp(BMP085ReadUT());
return(BMP085GetPressure(BMP085ReadUP()));
}
float BMP085Temperature(void){
_Temp = BMP085GetTemp(BMP085ReadUT());
return(_Temp);
}