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: MAX31875_Temperature_Sensor_Small_WLP Click-Sensor-MAX31875 NuMaker-mbed-Sensor-MAX31875
Diff: max31875_c.cpp
- Revision:
- 4:a27a0ee318bd
- Parent:
- 3:3528e660168c
- Child:
- 6:61f3693edd6c
--- a/max31875_c.cpp Mon Feb 04 23:32:38 2019 +0000 +++ b/max31875_c.cpp Wed Feb 13 05:01:35 2019 +0000 @@ -72,9 +72,9 @@ if (ret == 0) { ret = i2c_bus.read(max31875_read_address, data, 2, false); if (ret == 0) { - tmp.msb = data[0]; /* MSB */ - tmp.lsb = data[1]; /* LSB */ - *value = tmp.swrd; + tmp.msb = data[0]; + tmp.lsb = data[1]; + *value = tmp.uwrd; return MAX31875_NO_ERROR; } else { printf( @@ -91,19 +91,22 @@ return MAX31875_ERROR; } - float max31875_read_reg_as_temperature(uint8_t reg, I2C &i2c_bus) { max31875_raw_data tmp; float temperature; if (reg == MAX31875_REG_TEMPERATURE || - reg == MAX31875_REG_THYST || reg == MAX31875_REG_TOS) { + reg == MAX31875_REG_THYST_LOW_TRIP || reg == MAX31875_REG_TOS_HIGH_TRIP) { max31875_read_reg(&tmp.uwrd, reg, i2c_bus); - temperature = (float)tmp.swrd; + temperature = (float)tmp.magnitude_bits; if (max31875_extended_format) temperature *= MAX31875_CF_EXTENDED_FORMAT; else temperature *= MAX31875_CF_NORMAL_FORMAT; + if (tmp.sign_bit) + temperature = -temperature; + return temperature; + return temperature; } else { printf("%s: register is invalid, %d r\n", __func__, reg); @@ -126,10 +129,11 @@ cmd[2] = tmp.lsb; ret = i2c_bus.write(max31875_write_address, cmd, 3, false); if (ret == 0) { - if (tmp.uwrd & MAX31875_CFG_EXTENDED_FORMAT) - max31875_extended_format = 1; - else + if (MAX31875_REG_CONFIGURATION == reg) { max31875_extended_format = 0; + if (tmp.uwrd & MAX31875_CFG_EXTENDED_FORMAT) + max31875_extended_format = 1; + } return MAX31875_NO_ERROR; } else { printf("%s: I2C write error %d\r\n",__func__, ret); @@ -151,24 +155,32 @@ int max31875_write_trip_low(float temperature, I2C &i2c_bus) { max31875_raw_data raw; + if (temperature < 0) { + raw.sign_bit = 1; + temperature = -temperature; + } if (max31875_extended_format) temperature /= MAX31875_CF_EXTENDED_FORMAT; else temperature /= MAX31875_CF_NORMAL_FORMAT; - raw.uwrd = uint16_t(temperature); - return max31875_write_reg(raw.uwrd, MAX31875_REG_THYST, i2c_bus); + raw.magnitude_bits= uint16_t(temperature); + return max31875_write_reg(raw.uwrd, MAX31875_REG_THYST_LOW_TRIP, i2c_bus); } int max31875_write_trip_high(float temperature, I2C &i2c_bus) { max31875_raw_data raw; + if (temperature < 0) { + raw.sign_bit = 1; + temperature = -temperature; + } if (max31875_extended_format) temperature /= MAX31875_CF_EXTENDED_FORMAT; else temperature /= MAX31875_CF_NORMAL_FORMAT; - raw.uwrd = uint16_t(temperature); - return max31875_write_reg(raw.uwrd, MAX31875_REG_TOS, i2c_bus); + raw.magnitude_bits= uint16_t(temperature); + return max31875_write_reg(raw.uwrd, MAX31875_REG_TOS_HIGH_TRIP, i2c_bus); }