Fixed some I2C items to work on STM Nucleo F446RE

Committer:
ykita
Date:
Tue Oct 02 23:00:22 2018 +0000
Revision:
0:f33e95899738
Child:
1:4fbdf7768530
mbed library for Microchip MCP9600. Revision 0.1.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykita 0:f33e95899738 1 /* mbed MCP9600 Library, for the MCP9600 Thermocouple EMF to temperature Converter
ykita 0:f33e95899738 2 * Copyright (c) 2018, Yoshiteru Kita, ULVAC-PHI, INC.
ykita 0:f33e95899738 3 *
ykita 0:f33e95899738 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
ykita 0:f33e95899738 5 * of this software and associated documentation files (the "Software"), to deal
ykita 0:f33e95899738 6 * in the Software without restriction, including without limitation the rights
ykita 0:f33e95899738 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ykita 0:f33e95899738 8 * copies of the Software, and to permit persons to whom the Software is
ykita 0:f33e95899738 9 * furnished to do so, subject to the following conditions:
ykita 0:f33e95899738 10 *
ykita 0:f33e95899738 11 * The above copyright notice and this permission notice shall be included in
ykita 0:f33e95899738 12 * all copies or substantial portions of the Software.
ykita 0:f33e95899738 13 *
ykita 0:f33e95899738 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ykita 0:f33e95899738 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ykita 0:f33e95899738 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ykita 0:f33e95899738 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ykita 0:f33e95899738 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ykita 0:f33e95899738 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ykita 0:f33e95899738 20 * THE SOFTWARE.
ykita 0:f33e95899738 21 */
ykita 0:f33e95899738 22
ykita 0:f33e95899738 23 #include "MCP9600.h"
ykita 0:f33e95899738 24 #include "mbed.h"
ykita 0:f33e95899738 25
ykita 0:f33e95899738 26 // Constructor with I2C instance and I2C Device Address of MCP9600
ykita 0:f33e95899738 27 MCP9600::MCP9600(I2C *i2c, char addr) : i2cMcp9600(i2c) {
ykita 0:f33e95899738 28 Mcp9600Addr = addr;
ykita 0:f33e95899738 29 }
ykita 0:f33e95899738 30
ykita 0:f33e95899738 31 // Destructor
ykita 0:f33e95899738 32 MCP9600::~MCP9600() {
ykita 0:f33e95899738 33 }
ykita 0:f33e95899738 34
ykita 0:f33e95899738 35 // Set I2C Device Address of MCP9600
ykita 0:f33e95899738 36 void MCP9600::setAddress(char addr) {
ykita 0:f33e95899738 37 Mcp9600Addr = addr;
ykita 0:f33e95899738 38 }
ykita 0:f33e95899738 39
ykita 0:f33e95899738 40 // Get I2C Device Address of this instance
ykita 0:f33e95899738 41 char MCP9600::getAddress(void) {
ykita 0:f33e95899738 42 return (Mcp9600Addr);
ykita 0:f33e95899738 43 }
ykita 0:f33e95899738 44
ykita 0:f33e95899738 45 // Read Temperature from Temperature Register
ykita 0:f33e95899738 46 // tempRegister 0:TH / 1:Tdelta / 2: TC
ykita 0:f33e95899738 47 // Return value: temperature in float value (degree C)
ykita 0:f33e95899738 48 float MCP9600::readTempRegister( char tempRegister )
ykita 0:f33e95899738 49 {
ykita 0:f33e95899738 50 char data[2];
ykita 0:f33e95899738 51 float float_val;
ykita 0:f33e95899738 52
ykita 0:f33e95899738 53 i2cMcp9600->write( Mcp9600Addr, &tempRegister, 1, 1);
ykita 0:f33e95899738 54
ykita 0:f33e95899738 55 // Read temp (data[0] for MSB, data[1] for LSB)
ykita 0:f33e95899738 56 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 1 );
ykita 0:f33e95899738 57 i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 0 );
ykita 0:f33e95899738 58
ykita 0:f33e95899738 59 // Convert to real value
ykita 0:f33e95899738 60 if ((data[0] & 0x80) == 0x80) { // TH < 0
ykita 0:f33e95899738 61 data[0] = data[0] & 0x7F; // Clear Sign
ykita 0:f33e95899738 62 float_val = 2032.0f - (data[0] * 16.0f);
ykita 0:f33e95899738 63 float_val = float_val + (15.9375f - data[1] / 16.0f);
ykita 0:f33e95899738 64 float_val = float_val * (-1.0f);
ykita 0:f33e95899738 65 }
ykita 0:f33e95899738 66 else { // TH >= 0
ykita 0:f33e95899738 67 float_val = (data[0] * 16.0 + data[1] / 16.0);
ykita 0:f33e95899738 68 }
ykita 0:f33e95899738 69 return(float_val);
ykita 0:f33e95899738 70 }
ykita 0:f33e95899738 71
ykita 0:f33e95899738 72
ykita 0:f33e95899738 73 long MCP9600::readADCRegister(void)
ykita 0:f33e95899738 74 {
ykita 0:f33e95899738 75 char data[3];
ykita 0:f33e95899738 76 char adcRegister;
ykita 0:f33e95899738 77 long long_val;
ykita 0:f33e95899738 78
ykita 0:f33e95899738 79 adcRegister = 0x03;
ykita 0:f33e95899738 80 i2cMcp9600->write( Mcp9600Addr, &adcRegister, 1, 1);
ykita 0:f33e95899738 81
ykita 0:f33e95899738 82 // Read ADC Register (data[0] for MSB, data[2] for LSB)
ykita 0:f33e95899738 83 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 1 );
ykita 0:f33e95899738 84 i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 1 );
ykita 0:f33e95899738 85 i2cMcp9600->read( Mcp9600Addr, &data[2], 1, 0 );
ykita 0:f33e95899738 86
ykita 0:f33e95899738 87 // Convert to long value
ykita 0:f33e95899738 88 long_val = data[0];
ykita 0:f33e95899738 89 long_val = long_val << 8;
ykita 0:f33e95899738 90 long_val = long_val + data[1];
ykita 0:f33e95899738 91 long_val = long_val << 8;
ykita 0:f33e95899738 92 long_val = long_val + data[2];
ykita 0:f33e95899738 93
ykita 0:f33e95899738 94 return(long_val);
ykita 0:f33e95899738 95 }
ykita 0:f33e95899738 96
ykita 0:f33e95899738 97
ykita 0:f33e95899738 98 char MCP9600::writeStatusRegister( char statRegister )
ykita 0:f33e95899738 99 {
ykita 0:f33e95899738 100 char data[2];
ykita 0:f33e95899738 101
ykita 0:f33e95899738 102 data[0] = 0x04;
ykita 0:f33e95899738 103 data[1] = statRegister;
ykita 0:f33e95899738 104
ykita 0:f33e95899738 105 i2cMcp9600->write( Mcp9600Addr, data, 2, 1);
ykita 0:f33e95899738 106
ykita 0:f33e95899738 107 // Readbeck Status Register
ykita 0:f33e95899738 108 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 );
ykita 0:f33e95899738 109
ykita 0:f33e95899738 110 return(data[0]);
ykita 0:f33e95899738 111 }
ykita 0:f33e95899738 112
ykita 0:f33e95899738 113
ykita 0:f33e95899738 114 char MCP9600::readStatusRegister( void )
ykita 0:f33e95899738 115 {
ykita 0:f33e95899738 116 char data;
ykita 0:f33e95899738 117
ykita 0:f33e95899738 118 data = 0x04;
ykita 0:f33e95899738 119 i2cMcp9600->write( Mcp9600Addr, &data, 1, 0);
ykita 0:f33e95899738 120
ykita 0:f33e95899738 121 // Read Status Register
ykita 0:f33e95899738 122 i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 );
ykita 0:f33e95899738 123
ykita 0:f33e95899738 124 return(data);
ykita 0:f33e95899738 125
ykita 0:f33e95899738 126 }
ykita 0:f33e95899738 127
ykita 0:f33e95899738 128
ykita 0:f33e95899738 129 char MCP9600::writeSensorConfigRegister( char cnfgRegister )
ykita 0:f33e95899738 130 {
ykita 0:f33e95899738 131 char data[2];
ykita 0:f33e95899738 132
ykita 0:f33e95899738 133 data[0] = 0x05;
ykita 0:f33e95899738 134 data[1] = cnfgRegister;
ykita 0:f33e95899738 135
ykita 0:f33e95899738 136 i2cMcp9600->write( Mcp9600Addr, data, 2, 1);
ykita 0:f33e95899738 137
ykita 0:f33e95899738 138 // Readback Sensor Configuration Register
ykita 0:f33e95899738 139 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 );
ykita 0:f33e95899738 140
ykita 0:f33e95899738 141 return(data[0]);
ykita 0:f33e95899738 142 }
ykita 0:f33e95899738 143
ykita 0:f33e95899738 144
ykita 0:f33e95899738 145 char MCP9600::readSensorConfigRegister( void )
ykita 0:f33e95899738 146 {
ykita 0:f33e95899738 147 char data;
ykita 0:f33e95899738 148
ykita 0:f33e95899738 149 data = 0x05; // Device ID / Revision Register
ykita 0:f33e95899738 150 i2cMcp9600->write( Mcp9600Addr, &data, 1, 1);
ykita 0:f33e95899738 151
ykita 0:f33e95899738 152 i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 );
ykita 0:f33e95899738 153
ykita 0:f33e95899738 154 return (data);
ykita 0:f33e95899738 155 }
ykita 0:f33e95899738 156
ykita 0:f33e95899738 157
ykita 0:f33e95899738 158 char MCP9600::writeDeviceConfigRegister( char cnfgRegister )
ykita 0:f33e95899738 159 {
ykita 0:f33e95899738 160 char data[2];
ykita 0:f33e95899738 161
ykita 0:f33e95899738 162 data[0] = 0x06;
ykita 0:f33e95899738 163 data[1] = cnfgRegister;
ykita 0:f33e95899738 164
ykita 0:f33e95899738 165
ykita 0:f33e95899738 166 i2cMcp9600->write( Mcp9600Addr, data, 2, 1);
ykita 0:f33e95899738 167
ykita 0:f33e95899738 168 // Readback Device Configuration Register
ykita 0:f33e95899738 169 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 );
ykita 0:f33e95899738 170
ykita 0:f33e95899738 171 return(data[0]);
ykita 0:f33e95899738 172 }
ykita 0:f33e95899738 173
ykita 0:f33e95899738 174
ykita 0:f33e95899738 175 char MCP9600::readDeviceConfigRegister( void )
ykita 0:f33e95899738 176 {
ykita 0:f33e95899738 177 char data;
ykita 0:f33e95899738 178
ykita 0:f33e95899738 179 data = 0x06;
ykita 0:f33e95899738 180 i2cMcp9600->write( Mcp9600Addr, &data, 1, 1);
ykita 0:f33e95899738 181
ykita 0:f33e95899738 182 // Read Device Configuration Register
ykita 0:f33e95899738 183 i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 );
ykita 0:f33e95899738 184
ykita 0:f33e95899738 185 return(data);
ykita 0:f33e95899738 186
ykita 0:f33e95899738 187 }
ykita 0:f33e95899738 188
ykita 0:f33e95899738 189
ykita 0:f33e95899738 190 char MCP9600::writeAlertConfigRegister(char alertCH, char alertConfig)
ykita 0:f33e95899738 191 {
ykita 0:f33e95899738 192 // alertCH: Channel Number to set the configuration (1 to 4)
ykita 0:f33e95899738 193 // atertConfig:
ykita 0:f33e95899738 194 // bit7: Clear Interruput (1 to clear)
ykita 0:f33e95899738 195 // bit6: Not Used (0)
ykita 0:f33e95899738 196 // bit5: Not Used (0)
ykita 0:f33e95899738 197 // bit4: Alert for TH(0) or TC(1)
ykita 0:f33e95899738 198 // bit3: Alert for Rising(0) or Falling(1)
ykita 0:f33e95899738 199 // bit2: Active level (1:Active High / 0:Active Low)
ykita 0:f33e95899738 200 // bit1: Alert Mode (1: Intrrupt Mode / 0:Comparator Mode)
ykita 0:f33e95899738 201 // bit0: Alert Enable (1: Enable / 0: Disable)
ykita 0:f33e95899738 202 char data[2];
ykita 0:f33e95899738 203
ykita 0:f33e95899738 204 if ((alertCH > 0) && (alertCH < 5)) {
ykita 0:f33e95899738 205 data[0] = 0x07 + alertCH; // Alert Configuration Register
ykita 0:f33e95899738 206 data[1] = alertConfig; // Alert Configuration data
ykita 0:f33e95899738 207 i2cMcp9600->write( Mcp9600Addr, data, 2, 1);
ykita 0:f33e95899738 208
ykita 0:f33e95899738 209 // Read register
ykita 0:f33e95899738 210 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 );
ykita 0:f33e95899738 211 }
ykita 0:f33e95899738 212 return (data[0]);
ykita 0:f33e95899738 213 }
ykita 0:f33e95899738 214
ykita 0:f33e95899738 215
ykita 0:f33e95899738 216 char MCP9600::readAlertConfigRegister(char alertCH)
ykita 0:f33e95899738 217 {
ykita 0:f33e95899738 218 char data;
ykita 0:f33e95899738 219
ykita 0:f33e95899738 220 if ((alertCH > 0) && (alertCH < 5)) {
ykita 0:f33e95899738 221 data = 0x07 + alertCH; // Alert Configuration Register
ykita 0:f33e95899738 222 i2cMcp9600->write( Mcp9600Addr, &data, 1, 1);
ykita 0:f33e95899738 223
ykita 0:f33e95899738 224 i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 );
ykita 0:f33e95899738 225 }
ykita 0:f33e95899738 226 return (data);
ykita 0:f33e95899738 227 }
ykita 0:f33e95899738 228
ykita 0:f33e95899738 229
ykita 0:f33e95899738 230 char MCP9600::writeAlertHisterisisRegister(char alertCH, char alertHist)
ykita 0:f33e95899738 231 {
ykita 0:f33e95899738 232 // alertCH: Channel Number to set the configuration (1 to 4)
ykita 0:f33e95899738 233 // atertHist:
ykita 0:f33e95899738 234 // bit7: 128 C
ykita 0:f33e95899738 235 // bit6: 64 C
ykita 0:f33e95899738 236 // bit5: 32 C
ykita 0:f33e95899738 237 // bit4: 16 C
ykita 0:f33e95899738 238 // bit3: 8 C
ykita 0:f33e95899738 239 // bit2: 4 C
ykita 0:f33e95899738 240 // bit1: 2 C
ykita 0:f33e95899738 241 // bit0: 1 C
ykita 0:f33e95899738 242 char data[2];
ykita 0:f33e95899738 243
ykita 0:f33e95899738 244 if ((alertCH > 0) && (alertCH < 5)) {
ykita 0:f33e95899738 245 data[0] = 0x0B + alertCH; // Alert Histerisis Register
ykita 0:f33e95899738 246 data[1] = alertHist; // Alert Histerisis data
ykita 0:f33e95899738 247 i2cMcp9600->write( Mcp9600Addr, data, 2, 1);
ykita 0:f33e95899738 248
ykita 0:f33e95899738 249 // Read register
ykita 0:f33e95899738 250 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 );
ykita 0:f33e95899738 251 }
ykita 0:f33e95899738 252 return (data[0]);
ykita 0:f33e95899738 253 }
ykita 0:f33e95899738 254
ykita 0:f33e95899738 255
ykita 0:f33e95899738 256 char MCP9600::readAlertHisterisisRegister(char alertCH)
ykita 0:f33e95899738 257 {
ykita 0:f33e95899738 258 char data;
ykita 0:f33e95899738 259
ykita 0:f33e95899738 260 if ((alertCH > 0) && (alertCH < 5)) {
ykita 0:f33e95899738 261 data = 0x0B + alertCH; // Alert Histerisis Register
ykita 0:f33e95899738 262 i2cMcp9600->write( Mcp9600Addr, &data, 1, 1);
ykita 0:f33e95899738 263
ykita 0:f33e95899738 264 i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 );
ykita 0:f33e95899738 265 }
ykita 0:f33e95899738 266 return (data);
ykita 0:f33e95899738 267 }
ykita 0:f33e95899738 268
ykita 0:f33e95899738 269
ykita 0:f33e95899738 270 float MCP9600::writeAlertLimit_MCP9600(char alertCH, float alertLimit_val)
ykita 0:f33e95899738 271 {
ykita 0:f33e95899738 272 // alertCH: Channel Number to set the Histerisis (1 to 4)
ykita 0:f33e95899738 273 // atertLimitMSB
ykita 0:f33e95899738 274 // bit7: Sign (1:Negative / 0:Positive)
ykita 0:f33e95899738 275 // bit 6-0: Limit Value (x16)
ykita 0:f33e95899738 276 // atertLimitLSB
ykita 0:f33e95899738 277 // bit 7-2: Limit Value ( Div. 16)
ykita 0:f33e95899738 278 // bit 1-0: Not Used
ykita 0:f33e95899738 279
ykita 0:f33e95899738 280 char data[3];
ykita 0:f33e95899738 281 float float_val;
ykita 0:f33e95899738 282 char alertLimitMSB;
ykita 0:f33e95899738 283 char alertLimitLSB;
ykita 0:f33e95899738 284
ykita 0:f33e95899738 285 // Convert to 2 bytes
ykita 0:f33e95899738 286 if (alertLimit_val >= 0.0f) {
ykita 0:f33e95899738 287 alertLimitMSB = alertLimit_val / 16.0f;
ykita 0:f33e95899738 288 alertLimit_val = alertLimit_val - alertLimitMSB * 16.0f;
ykita 0:f33e95899738 289 alertLimitLSB = alertLimit_val * 16.0f;
ykita 0:f33e95899738 290 }
ykita 0:f33e95899738 291 else {
ykita 0:f33e95899738 292 alertLimit_val = alertLimit_val * (-1.0f);
ykita 0:f33e95899738 293 data[0] = alertLimit_val / 16.0f;
ykita 0:f33e95899738 294 alertLimitMSB = 1+ (2032.0f - alertLimit_val) / 16.0f;
ykita 0:f33e95899738 295 alertLimit_val = alertLimit_val - data[0] * 16.0f;
ykita 0:f33e95899738 296 alertLimit_val = 15.75f - alertLimit_val;
ykita 0:f33e95899738 297 alertLimitLSB = alertLimit_val * 16.0f;
ykita 0:f33e95899738 298 alertLimitMSB = alertLimitMSB | 0x80; // Set Sign Flag
ykita 0:f33e95899738 299 }
ykita 0:f33e95899738 300
ykita 0:f33e95899738 301 data[0] = 0x0F + alertCH; // Alert Limit Value Register
ykita 0:f33e95899738 302 data[1] = alertLimitMSB; // Alert Limit Value (MSB)
ykita 0:f33e95899738 303 data[2] = alertLimitLSB; // Alert Limit Value (LSB)
ykita 0:f33e95899738 304 i2cMcp9600->write( Mcp9600Addr, data, 3, 1 );
ykita 0:f33e95899738 305
ykita 0:f33e95899738 306 // Read Register
ykita 0:f33e95899738 307 i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 1 );
ykita 0:f33e95899738 308 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 );
ykita 0:f33e95899738 309
ykita 0:f33e95899738 310 // Convert to real value
ykita 0:f33e95899738 311 if ((data[1] & 0x80) == 0x80) { // Limit Value < 0
ykita 0:f33e95899738 312 data[1] = data[1] & 0x7F; // Clear Sign
ykita 0:f33e95899738 313 float_val = 2032.0f - (data[1] * 16.0f);
ykita 0:f33e95899738 314 float_val = float_val + (15.75f - data[0] / 16.0f);
ykita 0:f33e95899738 315 float_val = (-1.0f) * float_val;
ykita 0:f33e95899738 316 }
ykita 0:f33e95899738 317 else { // Limit Value >= 0
ykita 0:f33e95899738 318 float_val = (data[1] * 16.0 + data[0] / 16.0);
ykita 0:f33e95899738 319 }
ykita 0:f33e95899738 320 return(float_val);
ykita 0:f33e95899738 321 }
ykita 0:f33e95899738 322
ykita 0:f33e95899738 323
ykita 0:f33e95899738 324 float MCP9600::readAlertLimit_MCP9600(char alertCH)
ykita 0:f33e95899738 325 {
ykita 0:f33e95899738 326 char data[2];
ykita 0:f33e95899738 327 float float_val;
ykita 0:f33e95899738 328
ykita 0:f33e95899738 329 data[0] = 0x0F + alertCH; // Alert Limit Value Register
ykita 0:f33e95899738 330 i2cMcp9600->write( Mcp9600Addr, &data[0], 1, 1);
ykita 0:f33e95899738 331
ykita 0:f33e95899738 332 // Read register
ykita 0:f33e95899738 333 i2cMcp9600->read( Mcp9600Addr, &data[1], 1, 1 );
ykita 0:f33e95899738 334 i2cMcp9600->read( Mcp9600Addr, &data[0], 1, 0 );
ykita 0:f33e95899738 335
ykita 0:f33e95899738 336 // Convert to real value
ykita 0:f33e95899738 337 if ((data[1] & 0x80) == 0x80) { // Limit Value < 0
ykita 0:f33e95899738 338 data[1] = data[1] & 0x7F; // Clear Sign
ykita 0:f33e95899738 339 float_val = 2032.0 - (data[1] * 16.0);
ykita 0:f33e95899738 340 float_val = float_val - (15.75 - data[0] / 16.0);
ykita 0:f33e95899738 341 }
ykita 0:f33e95899738 342 else { // Limit Value >= 0
ykita 0:f33e95899738 343 float_val = (data[1] * 16.0 + data[0] / 16.0);
ykita 0:f33e95899738 344 }
ykita 0:f33e95899738 345 return(float_val);
ykita 0:f33e95899738 346 }
ykita 0:f33e95899738 347
ykita 0:f33e95899738 348
ykita 0:f33e95899738 349 char MCP9600::readIDRevisionRegister(void)
ykita 0:f33e95899738 350 {
ykita 0:f33e95899738 351 char data;
ykita 0:f33e95899738 352
ykita 0:f33e95899738 353 data = 0x20; // Device ID / Revision Register
ykita 0:f33e95899738 354 i2cMcp9600->write( Mcp9600Addr, &data, 1, 1);
ykita 0:f33e95899738 355
ykita 0:f33e95899738 356 i2cMcp9600->read( Mcp9600Addr, &data, 1, 0 );
ykita 0:f33e95899738 357
ykita 0:f33e95899738 358 return (data);
ykita 0:f33e95899738 359 }