From d601803cdd043d54a62f0611286627806a685764 Mon Sep 17 00:00:00 2001 From: Shravika Mittal Date: Mon, 4 Sep 2017 19:41:18 +0530 Subject: [PATCH 1/8] I2C tutorial added --- Tutorials/i2c.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Tutorials/i2c.md diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md new file mode 100644 index 0000000..bc3c86e --- /dev/null +++ b/Tutorials/i2c.md @@ -0,0 +1,61 @@ +# I2C + +I2C is a simple-to-use communication protocol which can have more than one master, with the upper bus speed defined, and only two wires with pull-up resistors are needed to connect almost _unlimited_ number of I2C devices. + +Each slave device has a unique address. Transfer from and to master device is serial and it is split into 8-bit packets. All these simple requirements make it very simple to implement I2C interface even with cheap micro-controllers that have no special I2C hardware controller. You only need 2 free I/O pins and few simple i2C routines to send and receive commands. + +Let us get the accelerometer values using the I2C protocol. We would be using the [accel-mma84](https://www.seeedstudio.com/Tessel-Accelerometer-Module-p-2223.html) + +![Fritzing Diagram](http://i.imgur.com/zK4U4S3.png)https://github.com/276linesofCode/Codes-Ideas/edit/master/tut-i2c.md#fork-destination-box + +```js +var tessel = require('tessel'); //Import tessel + +// Connect to device +var port = tessel.port.A; // Select Port A of Tessel +var slaveAddress = 0x1D; // Specefic for accelerometer module +var i2c = new port.I2C(slaveAddress); // Initialize I2C communication + +// Details of I2C transfer +var numBytesToRead = 1; // Read back this number of bytes + +i2c.read(numBytesToRead, function (error, dataReceived) { + + // Print data received (buffer of hex values) + console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); + +}); + +// Read/Receive data over I2C using i2c.transfer +i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) { + + // Print data received (buffer of hex values) + console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); + +}); + + +//Now, try to print the accelerometer data using i2c.transfer +i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) { + + // Print data received (buffer of hex values) + if (error) throw error; + //Create a blank array for the output + var out=[]; + for (var i=0;i<3;i++) + { + //iterating for the x, y, z values + var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; //Converting the 8 bit data into a 12 bit + gCount=gCount >> 4; + if (dataReceived[i*2] > 0x7F) + { + gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement + } + out[i] = gCount / ((1<<12)/(2*2)); + } + console.log('The x, y, z values are :',out); //Log the Array containing the x,y,z values + +}); +``` + +[Datasheet for accelerometer module](http://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf) From df50c181cd80036711db6de5504aafa9440d3fe8 Mon Sep 17 00:00:00 2001 From: Shravika Mittal Date: Mon, 11 Sep 2017 23:30:55 +0530 Subject: [PATCH 2/8] Requested changes added --- Tutorials/i2c.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md index bc3c86e..acff7b2 100644 --- a/Tutorials/i2c.md +++ b/Tutorials/i2c.md @@ -13,6 +13,9 @@ var tessel = require('tessel'); //Import tessel // Connect to device var port = tessel.port.A; // Select Port A of Tessel + +//This address of the Slave has been taken from https://github.com/tessel/accel-mma84/blob/master/index.js#L15 +//More about registers can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf var slaveAddress = 0x1D; // Specefic for accelerometer module var i2c = new port.I2C(slaveAddress); // Initialize I2C communication @@ -27,28 +30,29 @@ i2c.read(numBytesToRead, function (error, dataReceived) { }); // Read/Receive data over I2C using i2c.transfer +// 0x0D is the WHO_AM_I Register which sends back an acknoledgement to the master for starting the communication i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) { // Print data received (buffer of hex values) + // The returned buffer from the I2C slave device should be [0x2A] console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); }); -//Now, try to print the accelerometer data using i2c.transfer +// Now, try to print the accelerometer data using i2c.transfer +// The register address for OUT_X_MSB is 0x01. This can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf +// 6 Bytes are used for pairwise MSB and LSB of the x,y and z axis i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) { - // Print data received (buffer of hex values) if (error) throw error; //Create a blank array for the output var out=[]; - for (var i=0;i<3;i++) - { - //iterating for the x, y, z values + //iterating three times the x, y, z values + for (var i=0;i<3;i++){ var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; //Converting the 8 bit data into a 12 bit gCount=gCount >> 4; - if (dataReceived[i*2] > 0x7F) - { + if (dataReceived[i*2] > 0x7F) { gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement } out[i] = gCount / ((1<<12)/(2*2)); @@ -59,3 +63,5 @@ i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) { ``` [Datasheet for accelerometer module](http://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf) + +[More Informatiom on I2C Communication](https://learn.sparkfun.com/tutorials/i2c) From 6b0e74195fc4458676bed679e97c757c0ea612aa Mon Sep 17 00:00:00 2001 From: Brihi Joshi Date: Tue, 26 Sep 2017 13:13:41 +0530 Subject: [PATCH 3/8] Added commented code --- Tutorials/i2c.md | 89 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 28 deletions(-) diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md index acff7b2..caa5849 100644 --- a/Tutorials/i2c.md +++ b/Tutorials/i2c.md @@ -9,57 +9,90 @@ Let us get the accelerometer values using the I2C protocol. We would be using th ![Fritzing Diagram](http://i.imgur.com/zK4U4S3.png)https://github.com/276linesofCode/Codes-Ideas/edit/master/tut-i2c.md#fork-destination-box ```js -var tessel = require('tessel'); //Import tessel +var tessel = require('tessel'); // Connect to device -var port = tessel.port.A; // Select Port A of Tessel +var port = tessel.port.A; // Use the SCL/SDA pins of Port A //This address of the Slave has been taken from https://github.com/tessel/accel-mma84/blob/master/index.js#L15 -//More about registers can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf -var slaveAddress = 0x1D; // Specefic for accelerometer module +var slaveAddress = 0x1D; // Specific to device + var i2c = new port.I2C(slaveAddress); // Initialize I2C communication // Details of I2C transfer var numBytesToRead = 1; // Read back this number of bytes +// Read data over I2C using i2c.transfer i2c.read(numBytesToRead, function (error, dataReceived) { - // Print data received (buffer of hex values) console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); - + }); // Read/Receive data over I2C using i2c.transfer -// 0x0D is the WHO_AM_I Register which sends back an acknoledgement to the master for starting the communication -i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) { +// 0x0D is the WHO_AM_I Register which sends back an acknowledgement to the master for starting the communication +i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) { // Print data received (buffer of hex values) // The returned buffer from the I2C slave device should be [0x2A] console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); - + }); +/* + + In the i2c.transfer function the dataReceived consists of all the 6 bytes of data 2 bytes each for the x, y and z values. + Each of the x, y and z coordinates have two registers associated with them for storing the 12 bit long sample. + The first 8 bits are stored in their respective OUT_MSB registers. These are the Most Significant first 8 bits. + The next 4 bits are stored in their respective OUT_LSB registers. The remaining 4 bits are occupied + by 0s. These lower 4 bits are redundant bits which are not required. The OUT_LSB and OUT_MSB store the 2's complement form of the coordinates. + + The organisation of the registers can be seen in the datasheet inside section 6.1 (Data Registers), page number - 21 + The register address for OUT_X_MSB is 0x01. This can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf + +*/ + i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) { + + // This is an exception. If an error is generated, it will throw the error + if (error) throw error; + + //This array is going to store the final x,y,z values + var out=[]; -// Now, try to print the accelerometer data using i2c.transfer -// The register address for OUT_X_MSB is 0x01. This can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf -// 6 Bytes are used for pairwise MSB and LSB of the x,y and z axis -i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) { - - if (error) throw error; - //Create a blank array for the output - var out=[]; - //iterating three times the x, y, z values - for (var i=0;i<3;i++){ - var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; //Converting the 8 bit data into a 12 bit - gCount=gCount >> 4; - if (dataReceived[i*2] > 0x7F) { - gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement + + /* + + The for loop is iterated 3 times, in order to extract the x,y,z values. + The gCount variable is a bitwise OR operation between two binary numbers: + 1. The OUT_MSB of the respective coordinate, that is left shifted by 8 bits in order to make space for the remaining 8 bits + of the OUT_LSB. + 2. The OUT_LSB of the respective coordinate. + + The OUT_LSB of the respective coordinate is right shifted by 4 to get rid of the redundant lower 0 bits which are 4 in number. + + Check whether the most significant bit of the OUT_MSB is 1 or 0 i.e whether the coordinate value if negative or + positive. If it is negative (checked using 0x7F, which is the maximum possible number that can be made from 7 bits), the if condition changes + it to a 2's complement form, thus making it positive and adding a "-" sign in front of it. + + Lastly, normalise the coordinate to get a value between 0 and 1, dividing the gCount by 2^10. + + */ + + for (var i=0;i<3;i++){ + var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; + console.log(gCount); + gCount=gCount >> 4; + + // 127 is checking whether we have a 0 or a 1 at the first position - basically its sign. + if (dataReceived[i*2] > 0x7F) { + gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement + } + console.log(gCount); + out[i] = gCount / ((1<<12)/(2*2)); } - out[i] = gCount / ((1<<12)/(2*2)); - } - console.log('The x, y, z values are :',out); //Log the Array containing the x,y,z values - -}); + console.log('The x, y, z values are :',out); + + }); ``` [Datasheet for accelerometer module](http://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf) From 537b8cc461d0e6bb51ea21da30efad0589501d27 Mon Sep 17 00:00:00 2001 From: Shravika Mittal Date: Tue, 26 Sep 2017 20:56:30 +0530 Subject: [PATCH 4/8] Update i2c.md --- Tutorials/i2c.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md index caa5849..8eb08aa 100644 --- a/Tutorials/i2c.md +++ b/Tutorials/i2c.md @@ -80,14 +80,12 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) for (var i=0;i<3;i++){ var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; - console.log(gCount); gCount=gCount >> 4; // 127 is checking whether we have a 0 or a 1 at the first position - basically its sign. if (dataReceived[i*2] > 0x7F) { gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement } - console.log(gCount); out[i] = gCount / ((1<<12)/(2*2)); } console.log('The x, y, z values are :',out); @@ -95,6 +93,10 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) }); ``` +### Sample Ouput + +![output](https://i.imgur.com/Dg462Jf.jpg) + [Datasheet for accelerometer module](http://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf) [More Informatiom on I2C Communication](https://learn.sparkfun.com/tutorials/i2c) From 334c440a7f5461ff845ff55e9648475096800e50 Mon Sep 17 00:00:00 2001 From: Shravika Mittal Date: Fri, 29 Sep 2017 16:21:52 +0530 Subject: [PATCH 5/8] Made the changes requested --- Tutorials/i2c.md | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md index 8eb08aa..7c6fc14 100644 --- a/Tutorials/i2c.md +++ b/Tutorials/i2c.md @@ -2,11 +2,13 @@ I2C is a simple-to-use communication protocol which can have more than one master, with the upper bus speed defined, and only two wires with pull-up resistors are needed to connect almost _unlimited_ number of I2C devices. -Each slave device has a unique address. Transfer from and to master device is serial and it is split into 8-bit packets. All these simple requirements make it very simple to implement I2C interface even with cheap micro-controllers that have no special I2C hardware controller. You only need 2 free I/O pins and few simple i2C routines to send and receive commands. +Each slave device has a unique address. Transfer from and to master device is serial and it is split into 8-bit packets. All these simple requirements make it very simple to implement I2C interface even with cheap microcontrollers that have no special I2C hardware controller. You only need 2 free I/O pins and few simple I2C routines to send and receive commands. -Let us get the accelerometer values using the I2C protocol. We would be using the [accel-mma84](https://www.seeedstudio.com/Tessel-Accelerometer-Module-p-2223.html) +Let us get the accelerometer values using the I2C protocol. We will be using the [The Tessel Accelerometer](https://www.seeedstudio.com/Tessel-Accelerometer-Module-p-2223.html) -![Fritzing Diagram](http://i.imgur.com/zK4U4S3.png)https://github.com/276linesofCode/Codes-Ideas/edit/master/tut-i2c.md#fork-destination-box +[Refer to the datasheet for the accelerometer module to follow along](http://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf) + +![Fritzing Diagram](http://i.imgur.com/zK4U4S3.png) ```js var tessel = require('tessel'); @@ -14,15 +16,16 @@ var tessel = require('tessel'); // Connect to device var port = tessel.port.A; // Use the SCL/SDA pins of Port A -//This address of the Slave has been taken from https://github.com/tessel/accel-mma84/blob/master/index.js#L15 +// This address of the Slave has been taken from https://github.com/tessel/accel-mma84/blob/master/index.js#L15 +// It can also be found in Table 10 - I2C device address sequence on page number 17 of the datasheet. var slaveAddress = 0x1D; // Specific to device var i2c = new port.I2C(slaveAddress); // Initialize I2C communication -// Details of I2C transfer +// Details of I2C read and I2C transfer var numBytesToRead = 1; // Read back this number of bytes -// Read data over I2C using i2c.transfer +// Read data over I2C using i2c.read i2c.read(numBytesToRead, function (error, dataReceived) { // Print data received (buffer of hex values) console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); @@ -31,23 +34,25 @@ i2c.read(numBytesToRead, function (error, dataReceived) { // Read/Receive data over I2C using i2c.transfer // 0x0D is the WHO_AM_I Register which sends back an acknowledgement to the master for starting the communication +// Information about 0x0D can also be found in Table 11 - Register address map on page number 19 of the datasheet. i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) { // Print data received (buffer of hex values) - // The returned buffer from the I2C slave device should be [0x2A] + // The returned buffer from the I2C slave device should be [0x2A] as specified in the register address map on page number 19 of the MMA84 datasheet console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); }); /* - In the i2c.transfer function the dataReceived consists of all the 6 bytes of data 2 bytes each for the x, y and z values. - Each of the x, y and z coordinates have two registers associated with them for storing the 12 bit long sample. + In the i2c.transfer function, the dataReceived consists of all the 6 bytes of data (which is 48 bits of data) : 2 bytes each for the x, y, and z values. + This can be seen on page 20 and 21 of the accelerometer datasheet. + Each of the x, y, and z acceleration values have two registers associated with them for storing the 12 bit long sample. The first 8 bits are stored in their respective OUT_MSB registers. These are the Most Significant first 8 bits. The next 4 bits are stored in their respective OUT_LSB registers. The remaining 4 bits are occupied - by 0s. These lower 4 bits are redundant bits which are not required. The OUT_LSB and OUT_MSB store the 2's complement form of the coordinates. + by 0s. These lower 4 bits (bits 0, 1, 2, and 3 of the OUT_LSB registers) are redundant bits which are not required. The OUT_LSB and OUT_MSB store the 2's complement form of the coordinates. - The organisation of the registers can be seen in the datasheet inside section 6.1 (Data Registers), page number - 21 + The organization of the registers can be seen in the datasheet inside section 6.1 (Data Registers), page number - 21 The register address for OUT_X_MSB is 0x01. This can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf */ @@ -63,21 +68,23 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) /* The for loop is iterated 3 times, in order to extract the x,y,z values. + We have 16 bits to put together - the most significant 8 bits and the least significant 8 bits so that we can interpret them as one number. This combined value is placed in gCount variable using the OR operation The gCount variable is a bitwise OR operation between two binary numbers: - 1. The OUT_MSB of the respective coordinate, that is left shifted by 8 bits in order to make space for the remaining 8 bits + 1. The 8 bits in OUT_MSB for a given coordinate are shifted left by 8 bits in order to make space for the remaining 8 bits of the OUT_LSB. 2. The OUT_LSB of the respective coordinate. - The OUT_LSB of the respective coordinate is right shifted by 4 to get rid of the redundant lower 0 bits which are 4 in number. + The gCount number of the respective coordinate is right shifted by 4 to get rid of the unused lower 0 bits which are 4 in number yielding the 12 bits that the datasheet specifies each coordinate has on page number 21. Check whether the most significant bit of the OUT_MSB is 1 or 0 i.e whether the coordinate value if negative or positive. If it is negative (checked using 0x7F, which is the maximum possible number that can be made from 7 bits), the if condition changes it to a 2's complement form, thus making it positive and adding a "-" sign in front of it. - Lastly, normalise the coordinate to get a value between 0 and 1, dividing the gCount by 2^10. + Lastly, normalize the coordinate to get a value between 0 and 1, dividing gCount by 2^10. */ + // Three iterations for the three coordinates for (var i=0;i<3;i++){ var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; gCount=gCount >> 4; From 0980466e1ba9a42a28d48499c1691985f0f41737 Mon Sep 17 00:00:00 2001 From: Shravika Mittal Date: Fri, 29 Sep 2017 23:46:54 +0530 Subject: [PATCH 6/8] Documentation made more clearly on 2's complement --- Tutorials/i2c.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md index 7c6fc14..8775dcd 100644 --- a/Tutorials/i2c.md +++ b/Tutorials/i2c.md @@ -76,10 +76,6 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) The gCount number of the respective coordinate is right shifted by 4 to get rid of the unused lower 0 bits which are 4 in number yielding the 12 bits that the datasheet specifies each coordinate has on page number 21. - Check whether the most significant bit of the OUT_MSB is 1 or 0 i.e whether the coordinate value if negative or - positive. If it is negative (checked using 0x7F, which is the maximum possible number that can be made from 7 bits), the if condition changes - it to a 2's complement form, thus making it positive and adding a "-" sign in front of it. - Lastly, normalize the coordinate to get a value between 0 and 1, dividing gCount by 2^10. */ @@ -89,7 +85,14 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; gCount=gCount >> 4; - // 127 is checking whether we have a 0 or a 1 at the first position - basically its sign. +/* + + The x,y, and z accelerometer values are stored in their respective OUT_MSB and OUT_LSB registers as negative values i.e. their 2's complement form. + Therefore in order to obtain the correct values, check whether the most significant bit of the OUT_MSB is 1 or 0 i.e whether the coordinate value is negative or positive. + If it is negative (checked using 0x7F, which is the maximum possible number that can be made from 7 bits), the if condition changes it to a 2's complement form, thus making it positive and adding a "-" sign in front of it. + 127 is checking whether we have a 0 or a 1 at the first position - basically its sign. + +*/ if (dataReceived[i*2] > 0x7F) { gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement } From 56a8e7ac1488884448c891e7eb3f02d34f011383 Mon Sep 17 00:00:00 2001 From: Shravika Mittal Date: Fri, 29 Sep 2017 23:53:26 +0530 Subject: [PATCH 7/8] Line by line comments added --- Tutorials/i2c.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md index 8775dcd..35ae188 100644 --- a/Tutorials/i2c.md +++ b/Tutorials/i2c.md @@ -64,25 +64,21 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) //This array is going to store the final x,y,z values var out=[]; - + // The for loop is iterated 3 times, in order to extract the x,y, and z acceleration values. + for (var i=0;i<3;i++){ + /* - - The for loop is iterated 3 times, in order to extract the x,y,z values. - We have 16 bits to put together - the most significant 8 bits and the least significant 8 bits so that we can interpret them as one number. This combined value is placed in gCount variable using the OR operation + + We have 16 bits to put together - the most significant 8 bits and the least significant 8 bits so that we can interpret them as one number. This combined value is placed in gCount variable using the OR operation. The gCount variable is a bitwise OR operation between two binary numbers: 1. The 8 bits in OUT_MSB for a given coordinate are shifted left by 8 bits in order to make space for the remaining 8 bits of the OUT_LSB. 2. The OUT_LSB of the respective coordinate. - - The gCount number of the respective coordinate is right shifted by 4 to get rid of the unused lower 0 bits which are 4 in number yielding the 12 bits that the datasheet specifies each coordinate has on page number 21. - - Lastly, normalize the coordinate to get a value between 0 and 1, dividing gCount by 2^10. - - */ - - // Three iterations for the three coordinates - for (var i=0;i<3;i++){ + + */ var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; + + // The gCount number of the respective coordinate is right shifted by 4 to get rid of the unused lower 0 bits which are 4 in number yielding the 12 bits that the datasheet specifies each coordinate has on page number 21. gCount=gCount >> 4; /* @@ -96,6 +92,8 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) if (dataReceived[i*2] > 0x7F) { gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement } + + // Lastly, normalize the coordinate to get a value between 0 and 1, dividing gCount by 2^10. out[i] = gCount / ((1<<12)/(2*2)); } console.log('The x, y, z values are :',out); From 904d7885c01b200188b9328ad13976d80b5acae8 Mon Sep 17 00:00:00 2001 From: Shravika Mittal Date: Fri, 29 Sep 2017 23:55:31 +0530 Subject: [PATCH 8/8] Changes in spacing --- Tutorials/i2c.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tutorials/i2c.md b/Tutorials/i2c.md index 35ae188..5a04288 100644 --- a/Tutorials/i2c.md +++ b/Tutorials/i2c.md @@ -81,14 +81,14 @@ i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) // The gCount number of the respective coordinate is right shifted by 4 to get rid of the unused lower 0 bits which are 4 in number yielding the 12 bits that the datasheet specifies each coordinate has on page number 21. gCount=gCount >> 4; -/* + /* - The x,y, and z accelerometer values are stored in their respective OUT_MSB and OUT_LSB registers as negative values i.e. their 2's complement form. - Therefore in order to obtain the correct values, check whether the most significant bit of the OUT_MSB is 1 or 0 i.e whether the coordinate value is negative or positive. - If it is negative (checked using 0x7F, which is the maximum possible number that can be made from 7 bits), the if condition changes it to a 2's complement form, thus making it positive and adding a "-" sign in front of it. - 127 is checking whether we have a 0 or a 1 at the first position - basically its sign. + The x,y, and z accelerometer values are stored in their respective OUT_MSB and OUT_LSB registers as negative values i.e. their 2's complement form. + Therefore in order to obtain the correct values, check whether the most significant bit of the OUT_MSB is 1 or 0 i.e whether the coordinate value is negative or positive. + If it is negative (checked using 0x7F, which is the maximum possible number that can be made from 7 bits), the if condition changes it to a 2's complement form, thus making it positive and adding a "-" sign in front of it. + 127 is checking whether we have a 0 or a 1 at the first position - basically its sign. -*/ + */ if (dataReceived[i*2] > 0x7F) { gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement }