David Wahl / KellerDruck_pressure

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

keller_pressure.cpp

Committer:
dmwahl
Date:
2017-05-10
Revision:
1:805ee7853062
Parent:
0:fc5c10fc5a05
Child:
2:d6c82c96dae7
Child:
3:1a0add40e308

File content as of revision 1:805ee7853062:

#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];

    _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];

    // Scaling0 contains date/mode information
    _read_multibyte(KELLER_PRESSURE_SCALING0, data, 3);
    Scaling0 = (data[1] << 8) | data[2];

    union {
        char c[4];
        float f;
    } u;

    //Scaling1 and Scaling2 contain lower pressure limit
    _read_multibyte(KELLER_PRESSURE_SCALING1, data, 3);
    Scaling1 = (data[1] << 8) | data[2];
    u.c[3] = data[1];
    u.c[2] = data[2];

    _read_multibyte(KELLER_PRESSURE_SCALING2, data, 3);
    Scaling2 = (data[1] << 8) | data[2];
    u.c[1] = data[1];
    u.c[0] = data[2];
    pmin = u.f;

    //Scaling3 and Scaling4 contain upper pressure limit
    _read_multibyte(KELLER_PRESSURE_SCALING3, data, 3);
    Scaling3 = (data[1] << 8) | data[2];
    u.c[3] = data[1];
    u.c[2] = data[2];

    _read_multibyte(KELLER_PRESSURE_SCALING4, data, 3);
    Scaling4 = (data[1] << 8) | data[2];
    u.c[1] = data[1];
    u.c[0] = data[2];
    pmax = u.f;

    // Read out date of manufacture information and sensor mode
    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();
}