SPI with Multiple CS's

Hi all,

I am trying to use an arduino to talk to a DAC and then recieve data from an ADC, both using SPI. I can talk to either one if I use CS = pin 10, but when I try to use a different pin for one of them, to allow me to talk to them back and forth in the same program, it doesn’t work. I have read on the forums about this problem, particularly here (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283870594) and here (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284605439/6 reply#7), but I think I’m still confused on the setup of multiple CS’s. So, the SS pin on the arduino is always pin 10, and that must be set to output before initializing SPI. I do that. But I want to be able to use, say pin 9 to control the ADC and pin 10 to control the DAC. It seems like all that’s happening is that pin 9 goes LOW, but the SPI.transfer() doesn’t work properly. I have to be missing some other initialization for pin 9 or something…??

Thanks!

Here’s my code:
(I didn’t show the control function that’s shown in setup bc it doesn’t show anything other than an SPI.transfer )

#include <SPI.h>
/*
pin 10 - CS
pin 11 - MOSI
pin 12- MISO
pin 13 - SCLK
*/
const int DACcs = 9;
const int ADCcs = 10;

/*
Bits
23 22 21 20 19…0
R/W [Address] [Data…]
Control Address: 010
Data Address: 001
*/

void setup() {
pinMode(DACcs,OUTPUT);
pinMode(ADCcs,OUTPUT);
digitalWrite(DACcs,HIGH);
digitalWrite(ADCcs,HIGH);
Serial.begin(9600);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16); //16MHZ / 64 = 250KHz
SPI.setDataMode(SPI_MODE1);
control();
TxData(6,100,100);
}

void loop() {
RxADC();
}
void TxData(int msb, int mid, int lsb){
msb = msb + B00010000;
digitalWrite(DACcs,LOW);
SPI.transfer(byte(msb));
SPI.transfer(byte(mid));
SPI.transfer(byte(lsb));
digitalWrite(DACcs,HIGH);
//digitalWrite(ldac,LOW); //create falling edge
//digitalWrite(ldac,HIGH);
//printBinary(val);
}
void RxADC(){
/*
Keep adcCS LOW for 32 SCLK cycles, read in SDATAA and SDATAB
Format → 00[SDATAA]0000[SDATAB]00 ←
SDATAA and SDATAB each 12bits with two 0’s padding = 2 bytes
*/
int Mag = 0;
int Phase = 0;
digitalWrite(ADcs,LOW);
unsigned d1 = SPI.transfer(100);
unsigned d2 = SPI.transfer(0);
unsigned d3 = SPI.transfer(0);
unsigned d4 = SPI.transfer(0);
digitalWrite(ADCcs,HIGH);
}

nesportskid:

const int ADCcs = 10;

digitalWrite(ADcs,LOW);

See the difference? You spelled your own variable name wrong. Left out a C.

Haha yeah I did notice that. I think that came up from trying to make more "nice looking" code with meaningful names and I went too fast on that one. It works now. Thanks.