Interface ADC (MCP3208) w/ Arduino Mega using SPI

Hi all,
I'm new to the Arduino and these forums i'm wondering if someone could guide me how to interface the Arduino Mega with MCP3208, i know its been achieved with other versions of the Arduino and the code is available on the Arduino Playground.

But i was wondering if the same code applied to Arduino Mega since the Pins are different.

On to my 2nd Question how can i integrate multiple MCP3208s to the Arduino?

Thankyou

But i was wondering if the same code applied to Arduino Mega since the Pins are different.

The MCP3208 playground code bit-bangs SPI instead of using the ATmega SPI hardware. This means it should work exactly as written on the MEGA (but I have not tested this).

Well behaved SPI peripherals share the data bus (SCL, MISO, MOSI) but use a unique chip select for each peripheral. Just hook up data, power, and ground the same on both 3208s, but use different arduino pins for the chip select inputs.

I modified the playground code so that I could pass the chip select line to the function; code is attached below.

-j

int read_adc(byte cs, int channel)
{
  int adcvalue = 0;
  byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)

  //allow channel selection
  commandbits|=((channel-1)<<3);

  digitalWrite(cs, LOW); //Select adc
  // setup bits to be written
  for (int i=7; i>=3; i--){
    digitalWrite(DATAOUT,commandbits&1<<i);
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);    
  }

  digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
  digitalWrite(SPICLOCK,LOW);
  digitalWrite(SPICLOCK,HIGH);  
  digitalWrite(SPICLOCK,LOW);

  //read bits from adc
  for (int i=11; i>=0; i--){
    adcvalue+=digitalRead(DATAIN)<<i;
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }
  digitalWrite(cs, HIGH); //turn off device
  return adcvalue;
}

Thanks for the Tip kg4wsv, but just to be sure how exactly are you defining your CS pin like it was defined like this before.

#define CS 10 //Selection Pin

how are you now defining it now since you have more than one chip

I pass it in as a parameter to a function call. IIRC I actually defined it in an array, something like this:

#define ADC_CS1 8
#define ADC_CS2 7
#define NUM_ADCS 2
byte adc_cs_pin[NUM_ADCS] = {ADC_CS1, ADC_CS2};
...

void loop()
{
  byte adc;
  int channel, x;

  for (adc=0; adc<NUM_ADCS; adc++)
  {
    for (channel=1; channel <=8; channel++)
    {
      x = adc_read(adc_cs_pin[adc], channel);
      serial.print("adc "); serial.print(adc); serial.print(" channel "); serial.print(channel);
      serial.print(" value is "); serial.println(x);
    }
  }

  delay(2000);
}

Certainly not the only way to do it, but that's how I think about the problem.

-j