Pages: [1]   Go Down
Author Topic: SPI with SD?  (Read 2419 times)
0 Members and 1 Guest are viewing this topic.
Arduinoville
Offline Offline
Newbie
*
Karma: 1
Posts: 26
Arduino is my strong friend
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I am trying to use the SPI library to talk to a sensor, then use the SD library to write data to an SD card. However, I am having mucho problems. If anyone can shed any light on the topic, I'd appreciate it.

The sensor uses SPI mode 3, and the SD card uses SPI mode 0. I know we can use something like
Code:
SPI.setDataMode(SPI_MODE3);
to change the mode, but can we change back and forth throughout the code? I am running into problems with that (SD card errors and erroneous readings from the sensor).

Also, I tried to just change the chip selects to deselect the SD card
Code:
  digitalWrite(SDcs,HIGH);
  digitalWrite(ADIScs,LOW);
then send some data to the sensor
Code:
  SPI.transfer(0x56);
  SPI.transfer(0x00);
and switch back
Code:
  digitalWrite(ADIScs,HIGH);
  digitalWrite(SDcs,LOW);

However, after this when I try to write to the SD card, I get an error! Why would sending data on SPI cause problems when the chip select is not selected?

Even stranger, it does not cause an error on the SD card if I transfer all ones... 0xFF. Does this make any sense to anyone?

Thanks
Logged

Sometimes things are as they appear

Paris
Offline Offline
Full Member
***
Karma: 2
Posts: 156
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Later, Me too I will have to control two devices with SPI protocol. It's because, I'm interesting to understand your problem and I hope help you to resolve it.

So, to be sure, if I understand, your sensor and SD card share the same MISO, MOSI, and CLK lines on your arduino board ?
And you tried to select/deselect SD card and sensor with "digitalWrite" in high/low mode ?

Quote
Also, I tried to just change the chip selects to deselect the SD card
Code:
 digitalWrite(SDcs,HIGH);
  digitalWrite(ADIScs,LOW);

Be careful, because if you SDcs is in high value, it means SDcs ignores the master. I think CS (Chip select is complementary). It's /CS, not CS. But I'm not sure.
When a device's Slave Select pin is low, it communicates with the master. When it's high, it ignores the master.

Try to inverse,  smiley


EDIT : I'm sorry I made a mistake, here in your code, it's correct. Sorry  smiley-kiss
« Last Edit: January 26, 2011, 05:23:29 pm by SamoLaw » Logged

Paris
Offline Offline
Full Member
***
Karma: 2
Posts: 156
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you give me reference of your sensor and SD card, please ?  smiley
Logged

Arduinoville
Offline Offline
Newbie
*
Karma: 1
Posts: 26
Arduino is my strong friend
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I hope I/we can figure this out and get it working for both of us! They do share the same digital lines (MISO, MOSI, CLK) and have different chip selects. I am using the ADIS16367 from Analog Devices, and the microSD breakout board from Sparkfun. I want to read several registers from the inertial measurement unit, then write them to the microSD card after a few samples from each sensor. I use the Timer1 library I found in the playground to trigger an interrupt in which I read the sensor registers and put the values in hex into a char array. I will post any important updates!
Logged

Sometimes things are as they appear

Paris
Offline Offline
Full Member
***
Karma: 2
Posts: 156
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Did you check CLK rate ?

http://www.analog.com/static/imported-files/data_sheets/ADIS16367.pdf

Page 9, table 7, there are some settings for Master processor SPI.
Logged

Arduinoville
Offline Offline
Newbie
*
Karma: 1
Posts: 26
Arduino is my strong friend
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I did check the clock rate. I am using the 8MHz Arduino Pro Mini and calling
Code:
  SPI.setClockDivider(SPI_CLOCK_DIV16);
to cut it down to 500 kHz. This is below the maximum 1MHz for calling a burst read. I posted my entire code on the other thread if you are interested http://arduino.cc/forum/index.php/topic,50010.0.html
Logged

Sometimes things are as they appear

Pages: [1]   Go Up
Jump to: