David Wahl / KellerDruck_pressure

Dependents:   TestBenchSerenity-proto_F429ZI TestBenchFlow HSPFLOW1 TestBenchFlow1 ... more

Revision:
0:fc5c10fc5a05
Child:
1:805ee7853062
diff -r 000000000000 -r fc5c10fc5a05 keller_pressure.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/keller_pressure.cpp	Tue May 09 21:43:26 2017 +0000
@@ -0,0 +1,152 @@
+#include "keller_pressure.h"
+
+// Default constructor, input is the I2C address followed by min/max pressures (psi)
+KELLER_PRESSURE::KELLER_PRESSURE(I2C &i2c, int i2cAddress) : i2c(i2c), mi2cAddress(i2cAddress << 1)
+{
+    readUserInfo();
+};
+
+KELLER_PRESSURE::~KELLER_PRESSURE()
+{
+}
+
+// Write out a single address byte to I2C bus, if the sensor returns an ACK the function returns true.
+bool KELLER_PRESSURE::isAvailable()
+{
+    uint8_t i = false;
+    i2c.start();
+    i = i2c.write(mi2cAddress|I2C_WRITE);
+    i2c.stop();
+    if (i == 1) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+// Check if conversion has completed
+char KELLER_PRESSURE::getStatus()
+{
+    char result = 0xFF;
+    i2c.start();
+    i2c.write(mi2cAddress|I2C_READ);
+    result = i2c.read(0);
+    i2c.stop();
+    return result;
+}
+
+// Read pressure and temperature
+bool KELLER_PRESSURE::readPT()
+{
+    bool result = false;
+    char data[5];
+    _read_multibyte(KELLER_PRESSURE_REQUEST_MEASUREMENT, data, 5);
+    status = data[0];
+    pressure = (data[1] << 8) | data[2];
+    temperature = (data[3] << 8) | data[4];
+    
+    pressureBAR = ((pressure - 16384)*(pmax-pmin))/32768+pmin;
+    pressurePSI = pressureBAR*14.5038;
+    pressureKPA = pressureBAR*100;
+    
+    temperatureC = (temperature - 384)*0.003125-50;
+    temperatureF = (temperatureC*1.8+32);
+    result = true;
+    return result;
+}
+
+
+
+void KELLER_PRESSURE::readUserInfo()
+{
+    char data[3];
+    //const uint8_t i = NELEMS(data);
+    // _read_multibyte(char regAddress, char* data, char count)
+    _read_multibyte(KELLER_PRESSURE_CUST_ID0, data, 3);
+    Cust_ID0 = (data[1] << 8) | data[2];
+
+    _read_multibyte(KELLER_PRESSURE_CUST_ID1, data, 3);
+    Cust_ID1 = (data[1] << 8) | data[2];
+
+    _read_multibyte(KELLER_PRESSURE_SCALING0, data, 3);
+    Scaling0 = (data[1] << 8) | data[2];
+
+    _read_multibyte(KELLER_PRESSURE_SCALING1, data, 3);
+    Scaling1 = (data[1] << 8) | data[2];
+
+    _read_multibyte(KELLER_PRESSURE_SCALING2, data, 3);
+    Scaling2 = (data[1] << 8) | data[2];
+
+    _read_multibyte(KELLER_PRESSURE_SCALING3, data, 3);
+    Scaling3 = (data[1] << 8) | data[2];
+
+    _read_multibyte(KELLER_PRESSURE_SCALING4, data, 3);
+    Scaling4 = (data[1] << 8) | data[2];
+
+    pmin = ((Scaling1 << 16) | Scaling2)/(0xBF800000)*-1.0;
+    pmax = ((Scaling3 << 16) | Scaling4)/(1092616192.0)*10.0;
+
+    year = ((Scaling0 & KELLER_PRESSURE_SCALING0_YEAR_MASK) >> 11) + 2010;
+    month = (Scaling0 & KELLER_PRESSURE_SCALING0_MONTH_MASK) >> 7;
+    day = (Scaling0 & KELLER_PRESSURE_SCALING0_DAY_MASK) >> 2;
+    mode = (Scaling0 & KELLER_PRESSURE_SCALING0_MODE_MASK);
+}
+
+void KELLER_PRESSURE::_write(char regAddress, char data)
+{
+    i2c.start();
+    i2c.write(mi2cAddress|I2C_WRITE);
+    i2c.write(regAddress);
+    i2c.write(data);
+    i2c.stop();
+}
+
+char KELLER_PRESSURE::_read(char regAddress)
+{
+    char result = 0;
+
+    i2c.start();
+    i2c.write(mi2cAddress|I2C_WRITE);
+    i2c.write(regAddress);
+
+    i2c.start();
+    i2c.write(mi2cAddress|I2C_READ);
+    result = i2c.read(0);
+
+    i2c.stop();
+
+    return result;
+}
+
+void KELLER_PRESSURE::_read_multibyte(char regAddress, char* data, char count)
+{
+    //char count = (sizeof(data) / sizeof((data)[0]))-1;
+
+    /*wait_ms(1);
+    char data_write = regAddress;
+
+    i2c.write(mi2cAddress, &data_write, 1, false);
+    wait_ms(1);
+    i2c.read(mi2cAddress, data, count, false);
+    wait_ms(1);*/
+
+    i2c.start();
+    i2c.write(mi2cAddress|I2C_WRITE);
+    i2c.write(regAddress);
+    i2c.stop();
+
+    //wait_us(500);
+    while (getStatus() != 0x40)
+    {
+        wait_us(10); // wait until the status bit indicates conversion has completed
+    }
+
+    i2c.start();
+    i2c.write(mi2cAddress|I2C_READ);
+
+    for(int i = 0; i < count; i++) {
+        data[i] = i2c.read((i == count - 1) ? 0 : 1);
+    }
+
+    i2c.stop();
+}
\ No newline at end of file