In Section7 of the datasheet the bits in the Accelerometer and gyroscope registers are described.
For example:
7.12 CTRL_REG1_G (10h) Angular rate sensor Control Register 1.
| Bit7 | Bit6| Bit5| Bit4| Bit3| Bit2| Bit1| Bit0|
| --- | --- | --- | --- | --- | --- | --- | --- |
|ODR_G2 |ODR_G1 |ODR_G0 |FS_G1 |FS_G0 |0(1)| BW_G1 |BW_G0 |
This bit must be set to ‘0’ for the correct operation of the device. BW_G1 BW_G0
Bits 5, 6, and 7 control the output data rate of the gyro.
Bits 3, and 4 select the full scale of the gyro.
Bit 2 is always 0, and
Bits 0 and 1 select the gyro bandwidth.
/** Use the datastructure to write bits to AG register */
LSM9DS1.prototype.AGwrite=function(R,value){
var A=this.xgReadByte(R.reg);
//console.log(A,value,R,R.mask,R.shift);
var v=value&R.mask;
// console.log("v= "+v);
A=A&(~(R.mask<<R.shift));
A=A|(v<<R.shift);
// console.log("A= "+A.toString(2));
this.xgWriteByte(R.reg,A);
};//end AGwrite
/** Use the data structure to read bit settings from AG register*/
LSM9DS1.prototype.AGread=function(R){
var A=this.xgReadByte(R.reg);
A=A>>R.shift;
A=A&R.mask;
// console.log("A= "+A.toString(2));
return A;
};//end AGread
The following tests to see if the communication is working, performs a reset, writes and then reads a value to the ODR bits and then iterates thru the data structure to read and display all the bits.
/** 'run() initializes the LSM9DS1' */
LSM9DS1.prototype.run=function(){
var i;
var xgTest = this.xgReadByte(Regs.WHO_AM_I_XG);
console.log("XG replied "+xgTest);
console.log("do a reset");
this.AGwrite(REGval.SW_RESET,1);
console.log("SWreset= "+this.AGread(REGval.SW_RESET));
//
console.log("try writing to the ODR_G bits");
this.AGwrite(REGval.ODR_G,7);
console.log("ODR_G= "+this.AGread(REGval.ODR_G));
console.log("iterate thru all the AG settings");
for (var x in REGval)
console.log(x,this.AGread(REGval[x]));
return 1;
};//end run
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
How to Bobble the Bits in the Registers
The Vogan poetry in the datasheet for the LSM9DS1 can be found here:
http://www.st.com/content/ccc/resource/technical/document/datasheet/1e/3f/2a/d6/25/eb/48/46/DM00103319.pdf/files/DM00103319.pdf/jcr:content/translations/en.DM00103319.pdf
In section six the register mapping is described. This lets us write a table to reference the registers in the code. For example the gyro and accelerometer registers are defined:
In Section7 of the datasheet the bits in the Accelerometer and gyroscope registers are described.
For example:
7.12 CTRL_REG1_G (10h) Angular rate sensor Control Register 1.
| Bit7 | Bit6| Bit5| Bit4| Bit3| Bit2| Bit1| Bit0|
| --- | --- | --- | --- | --- | --- | --- | --- |
|ODR_G2 |ODR_G1 |ODR_G0 |FS_G1 |FS_G0 |0(1)| BW_G1 |BW_G0 |
Bits 5, 6, and 7 control the output data rate of the gyro.
Bits 3, and 4 select the full scale of the gyro.
Bit 2 is always 0, and
Bits 0 and 1 select the gyro bandwidth.
Setting up a data structure to bobble the bits
And setup functions to read or write the bits
The following tests to see if the communication is working, performs a reset, writes and then reads a value to the ODR bits and then iterates thru the data structure to read and display all the bits.
The output looks like this:
1 Attachment