Help with an SPI pot

Hi guys,
Im new to arduino and to these sorts of things in general (meaning SPI etc). In fact, i just received my arduino the other day. I have to say, i am very happy with it so far.

Sorry to bug, though, but i need a little hand with an SPI pot i am working with.

The pot is an MCP42100, you can find a datasheet here: http://futurlec.com.au/SFMicrochip/MCP42100.jsp

I am sure that i had it working late last night, but when i got up this morning it wasn’t working. How frustrating! So i was hoping somewhere here could tell me what i might be doing wrong. maybe i have made a silly mistake?

Thanks for your help!

Here is the Arduino code i am using (for 0007 version):

#define SLAVESELECT 10//ss
#define SPICLOCK 13//sck
#define DATAOUT 12//MOSI
#define DATAIN 11//MOSI

// command bytes

byte cmdWritePot1 = B00010001; // write data, to pot 1
byte cmdWritePot2 = B00010010; // write data, to pot 2

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
}

// Setup Function Begins Here

void setup()
{
Serial.begin(9600);
byte i;
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device

SPCR = B01111000;
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);
}

byte write_pots(int potValue11, int potValue12)
{
// spi pot chip 1
digitalWrite(SLAVESELECT,LOW); // enable chip 1
// set value for pot A
spi_transfer(cmdWritePot1); // command byte out to select pot and init write
spi_transfer(potValue11); // 8 bit pot value
// set value for pot B
spi_transfer(cmdWritePot2); // command byte out to select pot and init write
spi_transfer(potValue12); // 8 bit pot value
digitalWrite(SLAVESELECT,HIGH); // disable spi pot chip 1

}

// Main Loop Begins Here

void loop()
{

write_pots(255,0);
delay(100);

}

Here is a pic of my breadboard:

Okay, so now i think i know what i did wrong - the four SPI pins have to be 10,11,12,13 have to be CS, MOSI, MISO and SCK. So i set the pins correctly, but when i set the pot at a given value or a range of values from 0-255, it gives very strange readings - they are non-linear values that jump around but they do consistently change when i send a different value to the pot. Any ideas what could be causing this sort of problem would be appreciated.

Cheers,

hi

do you have the right hand (in the picture) power bus connected to VCC? A .1Uf bypass capacitor on the power is a good idea too.

D

Hi little-scale I looked at the datasheet and I think you have to raise the CS signal after you wrote the command byte and the value for each pot.

Basic operation in write_pots should be

byte write_pots(int potValue11, int potValue12)  { 
   // spi pot chip 1  
   digitalWrite(SLAVESELECT,LOW); // enable chip 1 
   // set value for pot A 
   spi_transfer(cmdWritePot1); // command byte out to select pot and init write 
   spi_transfer(potValue11); // 8 bit pot value 
   digitalWrite(SLAVESELECT,HIGH); // load data for pot A into the registers on chip 1 

   //now do the same for pot B
   digitalWrite(SLAVESELECT,LOW); // enable chip 1 
   // set value for pot B 
   spi_transfer(cmdWritePot2); // command byte out to select pot and init write 
   spi_transfer(potValue12); // 8 bit pot value 
   digitalWrite(SLAVESELECT,HIGH); // load data for pot B into the registers on chip 1  
  
 }

Eberhard

Thank you both for your help. i got it to work now. i added a 0.1uF cap to filter the power supply to the digital pot. i changed the spi control register to B01010000. i made_sure that i was only sending single bytes and not ints in place of the data byt for the digital pot. i also got rid of some useless things in the code. you can see the updated and working version of the sketch in the link here: http://www.milkcrate.com.au/_other/arduino/. (its an RTF file called "spi_pot_working).

Thanks again for your time, i appreciate it.

Seb (from adelaide, australia)