Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Talking directly with the accelerometer #8

Open
RipVW opened this issue Feb 21, 2019 · 0 comments
Open

Talking directly with the accelerometer #8

RipVW opened this issue Feb 21, 2019 · 0 comments
Labels
accelerometer Issues relating to the open-source accelerometer project.

Comments

@RipVW
Copy link

RipVW commented Feb 21, 2019

In general each communication with a register on the accelerometer requires two bytes. The first byte sent is the address of the register you wish to read or write. The second byte is either the contents you wish to write into the register, or, if you're reading, the contents of the register being sent back to the processor.

Here's a bit of code I use to set the range on the LIS331HH (This, and a lot of other register finagling are contained in the .txt document I attached to a comment in the "Data Storage Efficiancy" discussion.

// Write to CTRL_REG4 register located at 0x23 to set Full Scale and some defaults
    // 1xxxxxxx Set most significant bit to one to avoid updating data registers between reading the MSB and LSB of data
    // x0xxxxxx Bit for selecting big or little endian - default is 0
    // xx00xxxx Sets full scale to low range
    // xx01xxxx Sets full scale to mid range
    // xx11xxxx Sets full scale to high range
    // xxxx0000 Default values
    Wire.beginTransmission(HH);  // Talk to the 331HH
    Wire.write(0x23);  //Writes the address of the register
    // Use only one of the following three statements - this assumes most signifcant bit is 1 and lower four bits are 0
    Wire.write(0x80);  //Set Range to LOW 
    //Wire.write(0x90);  //Set Range to MED
    //Wire.write(0xB0);  //Set Range to HIGH 
    Wire.endTransmission(false); // false causes a restart to be sent

By default data is written into the registers as soon as it is available. This leads to the chance that while you're reading the upper byte of data the accelerometer is putting new information into the lower byte of data, and then when you read the lower byte, you end up with a pair of data bytes from two different readings. Setting the most significant bit to 1, tells the accelerometer not to be changing either byte until they are both read, (or both over written if no reading attempt was made.

Pleasantly enough, on p.31-32 of your datasheet is shows the register for setting the range has the same address and is organized in a similar manner. There are some differences, so please consult the datasheet.

Now, here is a bit of code showing how to read that same register to see if it really contains what was just written to it:

// Read CTRL_REG4 register
     Wire.beginTransmission(HH);  // Talk to the 331HH
     Wire.write(0x23);  //Writes the address of the register   
     Wire.endTransmission(false); // false causes a restart to be sent
     Wire.requestFrom(HH, 1);  // Request 1 byte
     if (Wire.available() > 0){
       Serial.print("CTRL_REG4:  ");
       Serial.println(Wire.read(), HEX); // Print the contents of the register
     } else {
      Serial.println("No data received");
     }  // End of if(Wire.available()

In the code above HH was previously defined as the address of the accelerometer:
#define HH 0x19 // Address for LIS331HH 6/12/24

@erichiggins erichiggins added the accelerometer Issues relating to the open-source accelerometer project. label Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accelerometer Issues relating to the open-source accelerometer project.
Projects
None yet
Development

No branches or pull requests

2 participants