Go Down

Topic: How to connect 2 SPI devices ( SD card and ADXL345 sensor) using the same pins ? (Read 3881 times) previous topic - next topic

muangau

Hi
I'd like to connect the ADXL345 sensor and SD card into the Mega 2560.  Each card works ok. individually.  They use these SPI pins:
- pin 50  ----> SDO (MISO)
- pin 51  ----> SDA  (MOSI)
- pin 52  ----> SCL (SCK)
- pin 53  ----> SS  (CS)
I understand that the SPI can work as master and slave and can share these pins except pin 53, one has to be assigned into different pin.  I tried pin 10 without success.
How should I connect them together ?
Many thanks
Tony



PaulS

Quote
I tried pin 10 without success.

You tried it how? Where is your code?
The art of getting good answers lies in asking good questions.

CrossRoads

You are connecting them correctly - both get SCK, MISO, and MOSI in parallel.
Each needs its own Chip Select - only one CS can be low at a time while transfers are occurring with that device.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

SurferTim

The SD card is SPI mode0 and the ADXL345 is SPI mode3. You must change the mode to access the ADXL345, then change it back for the SD.

As PaulS stated, post your code.

CrossRoads

Also keep in mind that both devices require 3.3V interfaces, while the Mega is 5V, so level shifting will be required.  SD card will also need a 3.3V supply, I don't believe the regulator on the Mega can put out enough current.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

muangau


You are connecting them correctly - both get SCK, MISO, and MOSI in parallel.
Each needs its own Chip Select - only one CS can be low at a time while transfers are occurring with that device.


You are right.  I turned both devices HIGH that caused the problem. It is working now.
Does it mean that if I have several SPI devices connect in parallel, only 1 SPI device be activated at a time ?
Many thanks for your advise.

Tony


Grumpy_Mike

Quote
Does it mean that if I have several SPI devices connect in parallel, only 1 SPI device be activated at a time ?

Yes

SurferTim

Just FYI: The ADXL345 is a SPI MODE3 device. The datasheet is very specific, and states CPOL=1 and CPHA=1 only. It mentions no compatibility with MODE0 devices like the SD. Starting on page 15 here:
http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
...and the SPI modes are covered here:
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

If it works on MODE0, you may just be lucky. It is not a documented feature of that IC.

edit: You can change those SPI settings "on the fly" so they can be used in the same sketch.
They must be changed while both SS pins are HIGH. That is also in the ADXL345 datasheet.

muangau

@SurferTim : thanks a lot for your info.  It is really big help.
@CrossRoads: thanks for your advise.  I haven't checked the accuracy of the data of the sensor but it seems to be in the range it should be.  When I turned high both devices, the sensor produced very high numbers (out of range).

Thank you all for your valued advises.
Tony

SurferTim

Quote
When I turned high both devices, the sensor produced very high numbers (out of range).

If this means you still need help , post the code you are trying.

Go Up