Academic Hardware Products (myDAQ, myRIO)

cancel
Showing results for 
Search instead for 
Did you mean: 

ADXL345 via SPI myRIO

So, a while back I had asked for assistance regarding communitcating the myRIO to the ADXL345 accelerometer. While it was a success, I thought that perhaps it would be good to try to get the SPI interface working with it as well in case I should need it. I've so far been able to commnicate to the accelerometer and read data from it; however, it seems that the data comes out in a very strange way unlike how it was with the I2C interface. I can't seem to get seperate values for X,Y,and Z directions. In fact, sometimes it seems to just give me a square wave which I know is incorrect. I'm not sure if this is an error with my code or if there is just some random noise/loose wiring that is causing this issue. The data sheet for the accelerometer is below. The SPI details are on page 8 and I'm using the 4-wire connection. I also will attach what I have so far (slightly simplified).

 

https://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf

 

Thanks in advanced!

0 Kudos
Message 1 of 25
(12,423 Views)

Hi,

 

I believe you have the join numbers function wired to the wrong terminals (it seems they are up side down). Take a look at this article for details (Community: ADXL 345 Sparkfun-- I2c--LabVIEW--Arduino).

 

Regards,

Alejandro C. | National Instruments
0 Kudos
Message 2 of 25
(12,407 Views)

Yes, I actually relized this today before I saw you post and fixed it accordingly. I tried moving some things around, but really didn't get anywhere over the past few hours. It just seems I'm getting a lot of random values (I resricted my reads for from the X registers just to make things easier for now). I also forgot to note last time that I am getting a waring with the code -363017 which states:

 

LabVIEW myRIO:  The requested frequency is outside the range of supported frequencies and has been coerced to the closest supported frequency within the range.

 

I know that the ADXL345 has a maximum SPI clock speed of 5 MHz while the myRIO is 4Mhz. The data sheet doesn't say that 5Mhz must be used, it is just the maximum that it can go to. Also, I tried to not even write to the first two set of address registers (x2D and x31) which initializes the accelerometer and I ended up getting the same jibberish that is read from the accelerometer. Any ideas? Thanks!

0 Kudos
Message 3 of 25
(12,401 Views)

Since myRIO is the Master on this scenario I don’t see a problem with the clocks (the ADXL345 should be able to work at 4 MHz). Perhaps you need to play around with the configuration for example try to invert the clock polarity to “Low”, remove the “to unsinged byte integer” function (perhaps you are coercing data), try inverting the significant bits...

Alejandro C. | National Instruments
0 Kudos
Message 4 of 25
(12,377 Views)

I apologize for being silent for a while. I hadn't been able to touch this project for a bit, but now I hope to continue it. I did try messing around with those setting for a while and also changing some register values to see if I could get anything, but I didn't make any headway. Even though I tell the accelerometer to read from all the registers, it seems that it just gives me one value for all of them which makes me think that some initialized value isn't writing correctly. I did think of something. I've had the CS tied to ground as I was under the impression it wasn't really needed unless you had multiple devices. Even still, it may seem like starting it high then going to a low may be necessary. I'm thinking about breaking up the initialized writes and have a DIO pin start on a high and then go to a low when writing. It would then return to a high before going to the next write. Do you think this could possibly be the issue and that this approach could fix it? Thanks!

0 Kudos
Message 5 of 25
(12,328 Views)

Hey DragoTheWise,

 

I'm pretty sure you need to use CS with the ADXL345.  Check out page 16 of the datasheet.

 

Some SPI devices use CS to simple enable a chip, however most use it for latching data.  

 

For example most SPI devices will sit idle, ignoring everything on its SPI lines until CS goes low.  This tells the chip to start listening.  As you shift in bytes its going to process the data and once CS goes high it will latch and execute some internal commands based on the data you've just transferred.  

 

When using myRIO you'll need to use a digital output for CS as you mentioned. 

 

Let us know how it goes!

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

0 Kudos
Message 6 of 25
(12,324 Views)

Alright. I took a few days to really try my hand at this. Using the DIO is indeed necessary; however, I ended up with the same result. I did some searching on the web to see what else I could find from the arduino side of things as this particular sensor is used a lot with it. I found one particular source that I thought would help. The user was getting -1,-1,-1 as their output which is exactly what I'm getting. Further down in the posting, one suggested to change the values for the registers to read from 32~37 to B2~B7. The reasoning for doing this was to set the first bit while reading from a register. I tried this and still got the same result. In looking at other arduino code and the datasheet, I also noticed some small delays were required and so inserted them in as well.

 

At this point, I'm a bit at a loss where to go from here. And as another note, I used the "Highlight Excution" function through Labview to make sure the DIO was working properly as well. Even with this slowed down, I continued to get -1,-1,-1 but I could clearly see that the DIO was in fact working. I've attached what I have right now, and I tried to make it look neat and compact as best as I could with the large blue boxes 🙂 Thanks!

 

 

 

 

0 Kudos
Message 7 of 25
(12,164 Views)

Hey,

 

I think you have some of the register values wrong and the multi byte data read seem off.

 

Here is my first stab at it, I don't have the sensor to tests with so let me know if this works.  I'll explain the differences once we figure out if this works.

 

0.PNG

 

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

0 Kudos
Message 8 of 25
(12,150 Views)

Hello Sammy,

 

I tried the code out and I still get -1,-1,-1 as the output. I played around with what you had to see if I could make any headway, but I didn't make any. I see you put a delay before going to a low. Was this to make sure that the previous data was processed on the accelerometer? I know in the timing diagram on the data sheet is mentions a small delay after CS goes low. I did try this, and I didn't have any change happen.

 

 

0 Kudos
Message 9 of 25
(12,140 Views)

Hey,

 

Can you look at the binary values being returned by the read operation (create u8 indicators on the index array without joining the numbers).  What are the values?  Do they change as you rotate the accelerometer?

 

Thanks!

 

-Sam K

LabVIEW Hacker

Join / Follow the LabVIEW Hacker Group on google+

0 Kudos
Message 10 of 25
(12,123 Views)