Mega, SPI and a digital potentiometer

I'm a bit stuck here and can't seem to work this out. (aka Another dumb noobie question)

I've hooked a AD5206 digital pot on my shiny new Mega. I used the tutorial http://www.arduino.cc/en/Tutorial/SPIDigitalPot and worked out the SPI pin address's for the mega (code below if anyone else gets stuck)

Now its up and running and have leds fading all over the place as it should via the tutorial. (so far so good)

SO, now i need to split it into individual addresss for each channel so i can set each of their resistance but after 3 hours of mucking about i can't seem to work it out. (i'm a dumb noobie and admit it..lol)

Once i work out how to split it into channels i'll be running it off a table (for lack of a better word) i.e. if x =2, channel1=5k channel2=2k and so on to channel6 and if x=5, channel1=7k channel2=4k etc etc

Hopefully 1 of you wise grand masters could point me in the right direction and help a mere grasshopper whilst i still have some sanity :-/

#define DATAOUT 51//MOSI
#define DATAIN 50//MISO - not used, but part of builtin SPI
#define SPICLOCK  52//sck
#define SLAVESELECT 53//ss

byte pot=0;
byte resistance=0;

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

void setup()
{
  byte i;
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
  // SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 (fastest)
  SPCR = (1<<SPE)|(1<<MSTR);
  clr=SPSR;
  clr=SPDR;
  delay(10);
  for (i=0;i<6;i++)
  {
    write_pot(i,255);
  }
}

byte write_pot(int address, int value)
{
  digitalWrite(SLAVESELECT,LOW);
  //2 byte opcode
  spi_transfer(address);
  spi_transfer(value);
  digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}

void loop()
{
     write_pot(pot,resistance);
     delay(10);
     resistance++;
     if (resistance==255)
     {
       pot++;
     }
     if (pot==6)
     {
       pot=0;
     }
}

Is this code the one that is working?
If so then I am not sure what you mean by channels, what more do you expect?
Channel and address look the same to me, am I missing something?

Hi Mike
As i said its kind of a dumb question
This code works as per the tutorial. The example code loops through all the channels (6) on the AD5206 fading them as it goes.
For the life of me i can't work out what part of the code sends the command to all 6 channels. What i can gather is that each channel on the AD5206 has an individual "address" so you can set its individual resistance.
What i need to achieve is;
"channel 1" = 1k (represented as 0 to 255 in the spec sheet)
"channel 2" = 2k " "
etc
Instead of looping through all 6 as per the tutorial
Thanks for your time to look at this Mike

you can just use the write_pot() function to do what you want.

pot0 address: 0
...
pot5 address: 5

Hey Madworm
Now i think i get it!
So if i define each channel like;
byte pot0=0;
byte resistance0=128;
and so on (pot1=1; etc)
I can set them in the write_pot() function?

Here is the basic code if anyone gets stuck with this sort of thing. I was expecting some weird and wonderful "00x1, stand on 1 foot and cough to the east" addressing sort of thing. So basically over complicating something that was so easy. One day i'll get my head around all this :wink:

Many thanks Madworm and Grumpy_Mike!

#define DATAOUT 51//MOSI
#define DATAIN 50//MISO - not used, but part of builtin SPI
#define SPICLOCK  52//sck
#define SLAVESELECT 53//ss

byte pot=0;
byte resistance=0;

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

void setup()
{
  byte i;
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
  // SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 (fastest)
  SPCR = (1<<SPE)|(1<<MSTR);
  clr=SPSR;
  clr=SPDR;
  delay(10);
  for (i=0;i<6;i++)
  {
    write_pot(i,255);
  }
}

byte write_pot(int address, int value)
{
  digitalWrite(SLAVESELECT,LOW);  //2 byte opcode
  spi_transfer(address);
  spi_transfer(value);
  digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}

void loop()
{
     write_pot(0,180); // pot number then resistance 0 to 225
     write_pot(1,50);
     write_pot(2,225);
     write_pot(3,180);
     write_pot(4,50);
     write_pot(5,225);
     delay(10);
     
}