Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TestBenchSerenity-proto_F429ZI TestBenchFlow HSPFLOW1 TestBenchFlow1 ... more
Diff: keller_pressure.cpp
- Revision:
- 3:1a0add40e308
- Parent:
- 1:805ee7853062
- Child:
- 4:c298ec31dd93
--- a/keller_pressure.cpp Wed May 10 16:57:58 2017 +0000 +++ b/keller_pressure.cpp Tue May 23 17:29:25 2017 +0000 @@ -1,17 +1,71 @@ +// Sample code +//#include <mbed.h> +//#include "keller_pressure.h" +// +//DigitalOut myled(LED1); +//Serial pc(SERIAL_TX, SERIAL_RX); +// +//// an I2C sub-class that provides a constructed default +//class I2CPreInit : public I2C +//{ +//public: +// I2CPreInit(PinName sda, PinName scl, int freq) : I2C(sda, scl) { +// frequency(freq); +// }; +//}; +////I2CPreInit gI2C1(I2C_SDA, I2C_SCL, I2C frequency); +//I2CPreInit i2c(PB_9, PB_8, 400000); +//KELLER_PRESSURE pumpP(i2c, 0x40); +// +//int main() +//{ +// pc.baud(250000); +// pc.printf("Starting up...\n\r"); +// if (pumpP.isAvailable()) { +// pc.printf("ACK\r\n"); +// pumpP.readPT(); +// pc.printf("Pmin: %.03f Pmax: %.03f\r\n", pumpP.pmin, pumpP.pmax); +// pc.printf("Year: %d Month: %d Day: %d Mode: %d\r\n", pumpP.year, pumpP.month, pumpP.day, pumpP.mode); +// pc.printf("Status: 0x%x\r\n", pumpP.getStatus()); +// pc.printf("%.02fkPa %.02fpsi %.02fC\r\n", pumpP.pressureKPA, pumpP.pressurePSI, pumpP.temperatureC); +// } +// while (1) { +// // Main loop +// if (pumpP.isAvailable()) { +// pumpP.readPT(); +// pc.printf("%.02fkPa %.02fpsi %.02fC\r\n", pumpP.pressureKPA, pumpP.pressurePSI, pumpP.temperatureC); +// } +// wait(1); +// } +// +//} +// +// End sample code + #include "keller_pressure.h" +union { + char c[4]; + float f; +} u; +//=========================================================================== // 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(); @@ -24,8 +78,11 @@ } } -// Check if conversion has completed +//=========================================================================== +// Read out status byte from sensor. First byte after any read request is status, +// following bytes are pressure/temperature. char KELLER_PRESSURE::getStatus() +//=========================================================================== { char result = 0xFF; i2c.start(); @@ -35,12 +92,16 @@ return result; } -// Read pressure and temperature -bool KELLER_PRESSURE::readPT() +//=========================================================================== +// Read pressure and temperature. Return 1 if error, 0 if OK. +// Byte 1: status, Bytes 2-3: pressure, Bytes 4-5: temperature +char KELLER_PRESSURE::readPT() +//=========================================================================== { - bool result = false; + char error = 0; char data[5]; - _read_multibyte(KELLER_PRESSURE_REQUEST_MEASUREMENT, data, 5); + + error |= _read_multibyte(KELLER_PRESSURE_REQUEST_MEASUREMENT, data, 5); status = data[0]; pressure = (data[1] << 8) | data[2]; temperature = (data[3] << 8) | data[4]; @@ -51,50 +112,45 @@ temperatureC = (temperature - 384)*0.003125-50; temperatureF = (temperatureC*1.8+32); - result = true; - return result; + return error; } - - -void KELLER_PRESSURE::readUserInfo() +//=========================================================================== +char KELLER_PRESSURE::readUserInfo() +//=========================================================================== { + char error = 0; char data[3]; - - _read_multibyte(KELLER_PRESSURE_CUST_ID0, data, 3); + + error |= _read_multibyte(KELLER_PRESSURE_CUST_ID0, data, 3); Cust_ID0 = (data[1] << 8) | data[2]; - _read_multibyte(KELLER_PRESSURE_CUST_ID1, data, 3); + error |= _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); + error |= _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); + error |= _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); + error |= _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); + error |= _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); + error |= _read_multibyte(KELLER_PRESSURE_SCALING4, data, 3); Scaling4 = (data[1] << 8) | data[2]; u.c[1] = data[1]; u.c[0] = data[2]; @@ -105,62 +161,35 @@ 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(); + return error; } -char KELLER_PRESSURE::_read(char regAddress) +//=========================================================================== +char KELLER_PRESSURE::_write(char regAddress, char data) +//=========================================================================== { - 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; + char error = 0; + char data_write[2] = {regAddress, data}; + error |= i2c.write(mi2cAddress|I2C_WRITE, data_write, 1, false); + return error; } -void KELLER_PRESSURE::_read_multibyte(char regAddress, char* data, char count) +//=========================================================================== +char 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 + char error = 0; + error |= i2c.write(mi2cAddress|I2C_WRITE, ®Address, 1, false); + for (int i = 0; i < 50; i++) { + if(getStatus() == KELLER_PRESSURE_MEASUREMENT_DONE) { + break; + } + wait_us(10); + if (i == 49) { + error |= 1; + } } - - 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(); + error |= i2c.read(mi2cAddress|I2C_READ, data, count, false); + return error; } \ No newline at end of file