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.
main.cpp
00001 #include "mbed.h" 00002 00003 /* 00004 *************************************************************************** 00005 MCP9800_Test. 9-12 bit temperature sensor and thermostat. 00006 2.7-5.5 V. 4.7kOhm pull-up resistors at SDA and SCLK. 00007 00008 This program sets 12 bit ambient temperature readings resolution. 00009 00010 Pointer bits: 00011 00: temperature register. 01: configuration register. 00012 10: temperature hysteresis register. 11: temperature limit-set register. 00013 Author: Lluis Nadal. August 2011. 00014 *************************************************************************** 00015 */ 00016 00017 I2C i2c(p9, p10); // SDA, SCL 00018 Serial pc(USBTX, USBRX); 00019 00020 const int addr_R = 0x91; // Address to read 00021 const int addr_W = 0x90; // Address to write 00022 //const int addr_R = 0x9B ; // Address to read 00023 //const int addr_W = 0x9A; // Address to write 00024 00025 char data[3]; 00026 char TL[2]; 00027 char TLR[2]; 00028 char TH[2]; 00029 char CR[1]; 00030 int t; 00031 float temp; 00032 00033 00034 void read_temp() { 00035 00036 int sign; 00037 i2c.start(); 00038 i2c.write(addr_W);//Address to write pointer 00039 i2c.write(0x00);// Write pointer to ambient temperature register 00040 i2c.read(addr_R, data, 2); 00041 i2c.stop(); 00042 00043 //pc.printf("Temperature register: (%d, %d)\r\n",data[0], data[1]); 00044 sign = data[0]>>7; 00045 //pc.printf("Sign: %d \r\n",sign); 00046 00047 if (sign == 1) { // If number is negative 00048 data[0] = ~data[0]; // Inverts (compliments)the bits 00049 data[1] = ~data[1]; // Inverts (compliments)the bits 00050 t = data[0]; 00051 t = -((t<<4) | (data[1]>>4)+1);// Packs and adds one to convert to negative integer 00052 temp = t/16.0; 00053 } 00054 00055 else { 00056 t = data[0]; 00057 t = ((t<<4) | (data[1]>>4)); 00058 temp = t/16.0; 00059 } 00060 pc.printf("Temperature: %.4f C\r\n",temp); 00061 pc.printf("\r\n"); 00062 } 00063 00064 00065 void read_TL() { // Read temperature limit register 00066 00067 i2c.start(); 00068 i2c.write(addr_W);//Address to write pointer 00069 i2c.write(0x03);// Write pointer to TL register 00070 i2c.read(addr_R, TLR, 2); 00071 i2c.stop(); 00072 pc.printf("Temperature limit register: (%d, %d)\r\n",TLR[0], TLR[1]); 00073 pc.printf("\r\n"); 00074 } 00075 00076 void read_TH() { // Read hysteresis register 00077 00078 i2c.start(); 00079 i2c.write(addr_W);//Address to write pointer 00080 i2c.write(0x02);// Write pointer to TH register 00081 i2c.read(addr_R, TH, 2); 00082 i2c.stop(); 00083 00084 pc.printf("Temperature hysteresis register: (%d, %d)\r\n",TH[0], TH[1]); 00085 pc.printf("\r\n"); 00086 } 00087 00088 00089 void read_CR() { // Read configuration register 00090 00091 i2c.start(); 00092 i2c.write(addr_W);//Address to write pointer 00093 i2c.write(0x01);// Write pointer to configuration register 00094 i2c.read(addr_R, CR, 1); 00095 i2c.stop(); 00096 00097 pc.printf("Configuration register: (%d)\r\n",CR[0]); 00098 pc.printf("\r\n"); 00099 } 00100 00101 // Set temperature limit (9-bit) 00102 void set_TL(float n) { // -25.4375, 25.4375, 125, -55... 00103 char msb; 00104 char lsb; 00105 if (n < 0) { 00106 t = (int)(-n*16.0-1); 00107 pc.printf("t = %d\r\n", t); 00108 t = t<<4; 00109 lsb = t & 0xFF; 00110 msb = t>>8; 00111 lsb = ~lsb; //Inverts (compliments)the bits 00112 msb = ~msb; //Inverts (compliments)the bits 00113 //pc.printf("(%d, %d)\r\b", msb, lsb); 00114 } else { 00115 t = (int)(n*16.0); 00116 t = t<<4; 00117 lsb = t & 0xFF; 00118 msb = t>>8; 00119 //pc.printf("(%d, %d)\r\b", msb, lsb); 00120 } 00121 i2c.start(); 00122 i2c.write(addr_W);//Address to write pointer 00123 i2c.write(0x03);// Write pointer to temperature-limit register 00124 i2c.write(msb);// Write data 00125 i2c.write(lsb);// Write data 00126 i2c.stop(); 00127 } 00128 00129 // Set hysteresis register (9-bit) 00130 void set_TH(float n) { // -25.4375, 25.4375, 125, -55... 00131 char msb; 00132 char lsb; 00133 if (n < 0) { 00134 t = (int)(-n*16.0-1); 00135 pc.printf("t = %d\r\n", t); 00136 t = t<<4; 00137 lsb = t & 0xFF; 00138 msb = t>>8; 00139 lsb = ~lsb; //Inverts (compliments)the bits 00140 msb = ~msb; //Inverts (compliments)the bits 00141 //pc.printf("(%d, %d)\r\b", msb, lsb); 00142 } else { 00143 t = (int)(n*16.0); 00144 t = t<<4; 00145 lsb = t & 0xFF; 00146 msb = t>>8; 00147 //pc.printf("(%d, %d)\r\b", msb, lsb); 00148 } 00149 i2c.start(); 00150 i2c.write(addr_W);//Address to write pointer 00151 i2c.write(0x02);// Write pointer to hysteresis register 00152 i2c.write(msb);// Write data 00153 i2c.write(lsb);// Write data 00154 i2c.stop(); 00155 } 00156 00157 00158 00159 00160 00161 int main() { 00162 00163 i2c.frequency(200000); // Max 400000 Hz 00164 00165 pc.printf("Initial readings: \r\n"); 00166 pc.printf("\r\n"); 00167 read_TL(); // Read temperature limit register 00168 read_TH(); // Read hysteresis register 00169 read_CR(); // Read configuration register 00170 00171 // Set 12 bit resolution 00172 i2c.start(); 00173 i2c.write(addr_W);//Address to write pointer 00174 i2c.write(0x01);// Write pointer to configuration register 00175 i2c.write(0x60);// Write data 00176 i2c.stop(); 00177 00178 // Set temperature limit (9-bit) 00179 set_TL(25.4375); // -25.4375, 25.4375, 125, -55... 00180 // If ambient temperature > temperature limit then alert pin is low (with a pull-up resistor). 00181 00182 00183 // Set hysteresis register (9-bit) 00184 set_TH(24.4375); // -25.4375, 25.4375, 125, -55... 00185 00186 wait(1); 00187 00188 pc.printf("\r\n"); 00189 pc.printf("Readings after changes: \r\n"); 00190 pc.printf("\r\n"); 00191 read_TL(); // Read temperature limit register 00192 read_TH(); // Read hysteresis register 00193 read_CR(); // Read configuration register 00194 read_temp(); // Read ambient temperature 00195 pc.printf("\r\n"); 00196 00197 }
Generated on Sat Jul 16 2022 00:03:27 by
1.7.2