Skip to content

Commit 9625ce8

Browse files
authored
Add files via upload
1 parent 482737f commit 9625ce8

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

README.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,37 +1211,35 @@ const r = require('array-gpio');
12111211

12121212
let i2c = r.startI2C(); // using SDA1 and SCL1 (pin 3 & 5) pins
12131213

1214-
/* set data transfer speed to 400 kHz */
1215-
i2c.setTransferSpeed(400000);
1214+
/* Set data transfer speed to 200 kHz */
1215+
i2c.setTransferSpeed(200000);
12161216

12171217
/* MCP9808 hardware device address */
12181218
let addr = 0x18;
12191219

1220-
/* select the MCP9808 device for data trasfer */
1220+
/* Select the MCP9808 device for data trasfer */
12211221
i2c.selectSlave(addr);
12221222

1223-
/* setup the application read and write data buffer */
1223+
/* Setup the application read and write data buffer */
12241224
const wbuf = Buffer.alloc(16); // write buffer
12251225
const rbuf = Buffer.alloc(16); // read buffer
12261226

1227-
/* accessing the internal 16-bit manufacturer ID register within MCP9808 */
1227+
/* Accessing the internal 16-bit manufacturer ID register within MCP9808 */
12281228
wbuf[0] = 0x06; // from the MCP9808 datasheet, set the address of the manufacturer ID register to the write buffer
12291229
i2c.write(wbuf, 1); // writes 1 data byte to the slave device selecting the MCP9808 manufacturer ID register for data access
12301230

1231-
/* master (rpi) device will now read the content of the 16-bit manufacturer ID register (should be 0x54 as per datasheet) */
1232-
/* reading 2 data bytes - the upper byte (rbuf[0]) and lower byte (rbuf[1]) from the manufacturer ID register, ID value is on the lower byte from the datasheet */
1231+
/* Master (rpi) device will now read the content of the 16-bit manufacturer ID register (should be 0x54 as per datasheet) */
1232+
/* Reading 2 data bytes - the upper byte (rbuf[0]) and lower byte (rbuf[1]) from the manufacturer ID register, ID value is on the lower byte from the datasheet */
12331233
i2c.read(rbuf, 2);
12341234

12351235
console.log('MCP9808 ID: ', rbuf[1].toString(16)); // convert the ID value to hex value
12361236

12371237
/* Based on MCP9808 datasheet, compute the temperature data as follows */
12381238
function getTemp(){
12391239

1240-
/* variable for temperature data */
1241-
let Temp;
1242-
1243-
let UpperByte = rbuf[0];
1244-
let LowerByte = rbuf[1];
1240+
let Temp = null;
1241+
let UpperByte = rbuf[0]; // MSB
1242+
let LowerByte = rbuf[1]; // LSB
12451243

12461244
UpperByte = UpperByte & 0x1F; // Clear flag bits
12471245

@@ -1251,33 +1249,36 @@ function getTemp(){
12511249
Temp = 256 - ((UpperByte * 16) + (LowerByte / 16));
12521250

12531251
/* Temp > 0 C */
1254-
}else {
1252+
}
1253+
else {
12551254
Temp = ((UpperByte * 16) + (LowerByte / 16));
12561255
}
12571256

12581257
/* Print out temperature data */
12591258
console.log('Temp: ', Temp);
12601259

1260+
return Temp;
1261+
12611262
}
12621263

1263-
/* get temperature readings every 2 seconds */
1264+
/* Get temperature readings every 2 seconds */
12641265
setInterval( function(){
1265-
/* accessing the internal 16-bit configuration register within MCP9808
1266+
/* Accessing the internal 16-bit configuration register within MCP9808.
12661267
You can skip accessing this register using default settings */
12671268
wbuf[0] = 0x01; // address of the configuration register
1268-
/* change content of configuration register */
1269+
/* Change content of configuration register */
12691270
wbuf[1] = 0x02; // register upper byte, THYST set with +1.5 C
12701271
wbuf[2] = 0x00; // register lower byte (power up defaults)
12711272
i2c.write(wbuf, 3);
12721273

1273-
/* accessing the internal 16-bit ambient temp register within MCP9808 */
1274+
/* Accessing the internal 16-bit ambient temp register within MCP9808 */
12741275
wbuf[0] = 0x05; // address of ambient temperature register
12751276
i2c.write(wbuf, 1);
12761277

1277-
/* read the content of ambient temp register */
1278+
/* Read the content of ambient temp register */
12781279
i2c.read(rbuf, 2); // read the UpperByte and LowerByte data
12791280

1280-
/* get temperature data and print out the results */
1281+
/* Get temperature data and print out the results */
12811282
getTemp();
12821283

12831284
}, 2000);

0 commit comments

Comments
 (0)