please help in adxl345

**hi am a new user to adxl345 accelerometer and i use it in graduation project to give output displacement i am really cant know a lot of things about it
first i use spi interface so i use only 4 pins cs ,sdio,sdo,sclk,i think 4 wire

second i want to know if the registers in the accelerometer is defoult cofigured that i can use to connect it directly to pic as i use 18f448 and make operation in pic

third the operation in pic is to convert the digital bits to coresponding current and then double integeration
am i write ??? please help me if you have any codes can help me in either configuring the accelerometer or in other operations

finally i am very sorry to disturb you all but really i am new in using this and i am really in great problem due to time limited !!!plz help me **

Here are the datasheets for the PIC18F448 link] and ADXL345 link]

The register defaults are in the accelerometer datasheet, but by default they’re set to for 4-wire SPI, so you would connect _CS to _SS, SDI to SDO, SDO to SDI, and SCL to SCK. Just make sure that when you’re configuring the SPI on the PIC, you select a clock divider such that the SPI clock isn’t faster than 5 MHz and that the clock polarity and phase are set properly.

You don’t need to convert to current; in the datasheet, the scales are listed as mg/LSB (milli-standard gravities per LSB), so just multiply the reading from the accelerometer by that scale, then divide by 1000 to get the number of gs of acceleration the accelerometer is measuring. You are correct that to get displacement you would then doubly integrate the sensor reading. Maybe use one of the PICs timers to compute delta time. Of course, integrating over large ranges of time will lead to large errors as noise accumulates.

I wasn’t able to find any PIC code for the ADXL345, but there is a listing for the Arduino here that might at least provide an outline of the command/response. You might also try asking on the SparkFun forums, there appear to be a few people using the ADXL345, whereas this forum is pretty specialized toward the hardware that the FIRST Robotics Competition includes with their kits of parts, which so far have been almost exclusively interfaced through analog or parallel digital interfaces.

Good luck,
–Ryan

We have written code to double integrate the ADXL345.
You can find the code here for the cRIO (http://proj.titanrobotics.net/hg/Frc/2010/code/file/e44126e46aee/inc/TrcAccel.h)
But as Ryan said, it will cumulate error when you integrate it over time. Therefore, you must use caution when doing it. We reset the accumulator right before we start the PID control (which is what we use the accelerometer for). This will clear the cumulated error and start fresh. It works reasonably well for us. We also implemented Zero-G calibration to eliminate DC offset of the outputs.

thankssssssssssssssssssssssssss a lot really you both helped me alot i was really in a trouble and i will work on this data and hoping to acheive the result soon and sorry if i make a disturbance again as i will of course face trouble :slight_smile: but in adxl345 i found delay time between samples is 5us between two samples i think this is the time use in the integeration (base) to calculate area under the curve … is this right or i use some of pic timer as rayan said to know exactly the time between samples …

The datasheet says the maximum output data rate is 3200 Hz, which would put the integration time base at about 312.5us, unless I’m misinterpreting something.

the challenge is synchronizing the PIC and the accelerometer. if you configure one of the interrupt out lines of the accelerometer to assert each time a new sample is ready, and run the SPI at a high enough data rate to be able to transfer each sample and then process it on the PIC in less time than it takes the accelerometer to read one sample, then you can use the accelerometer’s timing. If you don’t get the timing fast enough, you risk dropping samples, and then your integration will be off. It would be even better to make use of the ADXL345’s FIFO buffer, but you still have to make sure you can process the samples fast enough otherwise the buffer will overflow.

My suggestion to use the PIC’s timer was in response to your question about using the accelerometer with its registers in their default state, as neither the interrupts or FIFO are enabled by default. So I was assuming that you would just be reading from the accelerometer periodically, and as such you wouldn’t have an accurate timing from the accelerometer, so you would have to use timing from the PIC.

–Ryan

The code I quoted was designed to be independent of sample loss. Since it is determining the time between samples and use it as the interval for each integration, if you lose a sample, it will integrate with double the interval the next time. So it won’t throw you off. I wouldn’t assume a fixed sampling rate. I would figure out if the system has a hardware timer that you can read that will give you an accurate timestamp.

Good point. That’s basically what I was suggesting with using the PIC’s timer. AFAIK, there’s nothing as convenient as the GetMsecTime() function on the PIC platform, so just reading and resetting one of the timers when each sample is received is as close as you can get. It’s probably still beneficial to try to synchronize the PIC reading samples as they’re received, because there can still be oversampling problems.

–Ryan

thanks for all your efforts to help me and i will put all your point infront of me and i will care about it of course that i think i couldnt have been able to start the project without your help ryan and mikets … and i wish god help me in my first and important project

Hi,

I am developing a buoy to measure the ocean wave heights with the ADXL345 and a dsPIC. Until now i was not able to communicate with the adxl345 via the SPI interface. Can anyone tell the correct SPI configuration for the dsPICs or PICs?

Any help will be much appreciated.
Best regards.