Fixed some I2C items to work on STM Nucleo F446RE
Revision 1:4fbdf7768530, committed 2019-08-08
- Comitter:
- dmwahl
- Date:
- Thu Aug 08 15:36:58 2019 +0000
- Parent:
- 0:f33e95899738
- Commit message:
- Rewrote I2C related items from ykita's original code. Not sure if the original was tested or not, but I was getting compile errors for Nucleo boards.
Changed in this revision
MCP9600.cpp | Show annotated file Show diff for this revision Revisions of this file |
MCP9600.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MCP9600.cpp Tue Oct 02 23:00:22 2018 +0000 +++ b/MCP9600.cpp Thu Aug 08 15:36:58 2019 +0000 @@ -18,28 +18,34 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. + + * Modified by David Wahl to work with STM Nucleo boards (tested on Nucleo F446RE). Rewrote I2C related items. */ - - #include "MCP9600.h" - #include "mbed.h" - + +#include "MCP9600.h" +#include "mbed.h" + // Constructor with I2C instance and I2C Device Address of MCP9600 -MCP9600::MCP9600(I2C *i2c, char addr) : i2cMcp9600(i2c) { - Mcp9600Addr = addr; +MCP9600::MCP9600(I2C &i2c, int i2cAddress) : i2c(i2c), mi2cAddress(i2cAddress << 1) +{ + //Mcp9600Addr = addr; } // Destructor -MCP9600::~MCP9600() { +MCP9600::~MCP9600() +{ } // Set I2C Device Address of MCP9600 -void MCP9600::setAddress(char addr) { - Mcp9600Addr = addr; -} +//void MCP9600::setAddress(char addr) +//{ +// Mcp9600Addr = addr; +//} // Get I2C Device Address of this instance -char MCP9600::getAddress(void) { - return (Mcp9600Addr); +char MCP9600::getAddress(void) +{ + return (mi2cAddress >> 1); // Return 7-bit address } // Read Temperature from Temperature Register @@ -50,20 +56,19 @@ char data[2]; float float_val; - i2cMcp9600->write( Mcp9600Addr, &tempRegister, 1, 1); - + i2c.write( mi2cAddress, &tempRegister, 1, 1); + // Read temp (data[0] for MSB, data[1] for LSB) - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 1 ); - i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 0 ); - + i2c.read( mi2cAddress, &data[0], 1, 1 ); + i2c.read( mi2cAddress, &data[1], 1, 0 ); + // Convert to real value if ((data[0] & 0x80) == 0x80) { // TH < 0 data[0] = data[0] & 0x7F; // Clear Sign float_val = 2032.0f - (data[0] * 16.0f); float_val = float_val + (15.9375f - data[1] / 16.0f); float_val = float_val * (-1.0f); - } - else { // TH >= 0 + } else { // TH >= 0 float_val = (data[0] * 16.0 + data[1] / 16.0); } return(float_val); @@ -77,13 +82,13 @@ long long_val; adcRegister = 0x03; - i2cMcp9600->write( Mcp9600Addr, &adcRegister, 1, 1); - + i2c.write( mi2cAddress, &adcRegister, 1, 1); + // Read ADC Register (data[0] for MSB, data[2] for LSB) - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 1 ); - i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 1 ); - i2cMcp9600->read( Mcp9600Addr, &data[2], 1, 0 ); - + i2c.read( mi2cAddress, &data[0], 1, 1 ); + i2c.read( mi2cAddress, &data[1], 1, 1 ); + i2c.read( mi2cAddress, &data[2], 1, 0 ); + // Convert to long value long_val = data[0]; long_val = long_val << 8; @@ -98,16 +103,16 @@ char MCP9600::writeStatusRegister( char statRegister ) { char data[2]; - + data[0] = 0x04; data[1] = statRegister; - - i2cMcp9600->write( Mcp9600Addr, data, 2, 1); - + + i2c.write( mi2cAddress, data, 2, 1); + // Readbeck Status Register - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 ); + i2c.read( mi2cAddress, &data[0], 1, 0 ); - return(data[0]); + return(data[0]); } @@ -116,41 +121,41 @@ char data; data = 0x04; - i2cMcp9600->write( Mcp9600Addr, &data, 1, 0); - + i2c.write( mi2cAddress, &data, 1, 0); + // Read Status Register - i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 ); - + i2c.read( mi2cAddress, &data, 1, 0 ); + return(data); - + } char MCP9600::writeSensorConfigRegister( char cnfgRegister ) { char data[2]; - + data[0] = 0x05; data[1] = cnfgRegister; - - i2cMcp9600->write( Mcp9600Addr, data, 2, 1); + + i2c.write( mi2cAddress, data, 2, 1); // Readback Sensor Configuration Register - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 ); + i2c.read( mi2cAddress, &data[0], 1, 0 ); - return(data[0]); + return(data[0]); } char MCP9600::readSensorConfigRegister( void ) { char data; - + data = 0x05; // Device ID / Revision Register - i2cMcp9600->write( Mcp9600Addr, &data, 1, 1); - - i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 ); - + i2c.write( mi2cAddress, &data, 1, 1); + + i2c.read( mi2cAddress, &data, 1, 0 ); + return (data); } @@ -158,17 +163,17 @@ char MCP9600::writeDeviceConfigRegister( char cnfgRegister ) { char data[2]; - + data[0] = 0x06; data[1] = cnfgRegister; - - i2cMcp9600->write( Mcp9600Addr, data, 2, 1); - + + i2c.write( mi2cAddress, data, 2, 1); + // Readback Device Configuration Register - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 ); + i2c.read( mi2cAddress, &data[0], 1, 0 ); - return(data[0]); + return(data[0]); } @@ -177,13 +182,13 @@ char data; data = 0x06; - i2cMcp9600->write( Mcp9600Addr, &data, 1, 1); - + i2c.write( mi2cAddress, &data, 1, 1); + // Read Device Configuration Register - i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 ); - + i2c.read( mi2cAddress, &data, 1, 0 ); + return(data); - + } @@ -202,13 +207,13 @@ char data[2]; if ((alertCH > 0) && (alertCH < 5)) { - data[0] = 0x07 + alertCH; // Alert Configuration Register + data[0] = 0x07 + alertCH; // Alert Configuration Register data[1] = alertConfig; // Alert Configuration data - i2cMcp9600->write( Mcp9600Addr, data, 2, 1); + i2c.write( mi2cAddress, data, 2, 1); // Read register - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 ); - } + i2c.read( mi2cAddress, &data[0], 1, 0 ); + } return (data[0]); } @@ -216,13 +221,13 @@ char MCP9600::readAlertConfigRegister(char alertCH) { char data; - + if ((alertCH > 0) && (alertCH < 5)) { data = 0x07 + alertCH; // Alert Configuration Register - i2cMcp9600->write( Mcp9600Addr, &data, 1, 1); + i2c.write( mi2cAddress, &data, 1, 1); - i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 ); - } + i2c.read( mi2cAddress, &data, 1, 0 ); + } return (data); } @@ -242,13 +247,13 @@ char data[2]; if ((alertCH > 0) && (alertCH < 5)) { - data[0] = 0x0B + alertCH; // Alert Histerisis Register + data[0] = 0x0B + alertCH; // Alert Histerisis Register data[1] = alertHist; // Alert Histerisis data - i2cMcp9600->write( Mcp9600Addr, data, 2, 1); + i2c.write( mi2cAddress, data, 2, 1); // Read register - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 ); - } + i2c.read( mi2cAddress, &data[0], 1, 0 ); + } return (data[0]); } @@ -256,13 +261,13 @@ char MCP9600::readAlertHisterisisRegister(char alertCH) { char data; - + if ((alertCH > 0) && (alertCH < 5)) { data = 0x0B + alertCH; // Alert Histerisis Register - i2cMcp9600->write( Mcp9600Addr, &data, 1, 1); + i2c.write( mi2cAddress, &data, 1, 1); - i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 ); - } + i2c.read( mi2cAddress, &data, 1, 0 ); + } return (data); } @@ -276,19 +281,18 @@ // atertLimitLSB // bit 7-2: Limit Value ( Div. 16) // bit 1-0: Not Used - + char data[3]; float float_val; char alertLimitMSB; char alertLimitLSB; - + // Convert to 2 bytes if (alertLimit_val >= 0.0f) { alertLimitMSB = alertLimit_val / 16.0f; alertLimit_val = alertLimit_val - alertLimitMSB * 16.0f; alertLimitLSB = alertLimit_val * 16.0f; - } - else { + } else { alertLimit_val = alertLimit_val * (-1.0f); data[0] = alertLimit_val / 16.0f; alertLimitMSB = 1+ (2032.0f - alertLimit_val) / 16.0f; @@ -297,15 +301,15 @@ alertLimitLSB = alertLimit_val * 16.0f; alertLimitMSB = alertLimitMSB | 0x80; // Set Sign Flag } - - data[0] = 0x0F + alertCH; // Alert Limit Value Register + + data[0] = 0x0F + alertCH; // Alert Limit Value Register data[1] = alertLimitMSB; // Alert Limit Value (MSB) data[2] = alertLimitLSB; // Alert Limit Value (LSB) - i2cMcp9600->write( Mcp9600Addr, data, 3, 1 ); + i2c.write( mi2cAddress, data, 3, 1 ); // Read Register - i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 1 ); - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 ); + i2c.read( mi2cAddress, &data[1], 1, 1 ); + i2c.read( mi2cAddress, &data[0], 1, 0 ); // Convert to real value if ((data[1] & 0x80) == 0x80) { // Limit Value < 0 @@ -313,8 +317,7 @@ float_val = 2032.0f - (data[1] * 16.0f); float_val = float_val + (15.75f - data[0] / 16.0f); float_val = (-1.0f) * float_val; - } - else { // Limit Value >= 0 + } else { // Limit Value >= 0 float_val = (data[1] * 16.0 + data[0] / 16.0); } return(float_val); @@ -326,20 +329,19 @@ char data[2]; float float_val; - data[0] = 0x0F + alertCH; // Alert Limit Value Register - i2cMcp9600->write( Mcp9600Addr, &data[0], 1, 1); - + data[0] = 0x0F + alertCH; // Alert Limit Value Register + i2c.write( mi2cAddress, &data[0], 1, 1); + // Read register - i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 1 ); - i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 ); - + i2c.read( mi2cAddress, &data[1], 1, 1 ); + i2c.read( mi2cAddress, &data[0], 1, 0 ); + // Convert to real value if ((data[1] & 0x80) == 0x80) { // Limit Value < 0 data[1] = data[1] & 0x7F; // Clear Sign float_val = 2032.0 - (data[1] * 16.0); float_val = float_val - (15.75 - data[0] / 16.0); - } - else { // Limit Value >= 0 + } else { // Limit Value >= 0 float_val = (data[1] * 16.0 + data[0] / 16.0); } return(float_val); @@ -349,11 +351,11 @@ char MCP9600::readIDRevisionRegister(void) { char data; - + data = 0x20; // Device ID / Revision Register - i2cMcp9600->write( Mcp9600Addr, &data, 1, 1); - - i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 ); - + i2c.write( mi2cAddress, &data, 1, 1); + + i2c.read( mi2cAddress, &data, 1, 0 ); + return (data); }
--- a/MCP9600.h Tue Oct 02 23:00:22 2018 +0000 +++ b/MCP9600.h Thu Aug 08 15:36:58 2019 +0000 @@ -18,24 +18,27 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. + + * Modified by David Wahl to work with STM Nucleo boards (tested on Nucleo F446RE). Rewrote I2C related items. */ - + #ifndef MBED_MCP9600_H #define MBED_MCP9600_H #include "mbed.h" -class MCP9600 { +class MCP9600 +{ public: // Constructor with I2C instance and I2C Device Address of MCP9600 - MCP9600(I2C *i2c, char addr); - + MCP9600(I2C &i2c, int addr); + // Set I2C Device Address of MCP9600 - void setAddress(char addr); - + //void setAddress(char addr); + // Get I2C Device Address of this instance char getAddress(void); - + // Read Temperature from Temperature Register // tempRegister 0:TH / 1:Tdelta / 2: TC // Return value: temperature in float value (degree C) @@ -45,12 +48,12 @@ // Return value: 24 bit ADC register value // Refer MCP9600 datasheet for details. long readADCRegister(void); - + // Write to Status Register // statRegister: Status data to write // About register bit, refer MCP9600 datasheet. char writeStatusRegister( char statRegister ); - + // Read Status Register // About register bit, refer MCP9600 datasheet. char readStatusRegister( void ); @@ -59,20 +62,20 @@ // cnfgRegister: Sensor Config data to write // About register bit, refer MCP9600 datasheet. char writeSensorConfigRegister( char cnfgRegister ); - + // Read Sensor Configuration Register // About register bit, refer MCP9600 datasheet. char readSensorConfigRegister( void ); - + // Write to Device Configuration Register // cnfgRegister: Device Config data to write // About register bit, refer MCP9600 datasheet. char writeDeviceConfigRegister( char cnfgRegister ); - + // Read Device Configuration Register // About register bit, refer MCP9600 datasheet. char readDeviceConfigRegister( void ); - + // Write to Alert Configuration Register // alertCH: Alert CH (1 to 4) // alertConfig: Alert Configuration data to write to register @@ -96,7 +99,7 @@ // alertCH: Alert CH (1 to 4) // Return Value: Alert Histerisis Register value char readAlertHisterisisRegister(char alertCH); - + // Write to Alert Limit Value Register // alertCH: Alert CH (1 to 4) // alertLimit_val: Alert Limit value to write to register @@ -116,8 +119,8 @@ ~MCP9600(); private: - char Mcp9600Addr ; // I2C Slave Address of MCP9600 - I2C *i2cMcp9600; // I2C bus reference + I2C &i2c; + uint8_t mi2cAddress; }; #endif