MCP42XXX digital potentiometer

I’m trying to control an MCP42100 from my arduino nano. I’ve been using a combination of http://www.arduino.cc/en/Tutorial/SPIDigitalPot and little-scale: Four MIDI Controlled Digital Pots via Arduino as a tutorial for this. My requirement for this doesn’t have the potentiometer changing value rapidly so code wise I’m trying to get simple as opposed to efficient.

The datasheet is here http://www.datasheetcatalog.org/datasheet/microchip/11195c.pdf

So the circuit I have has CS going to arduino pin 2, SCK to 4. SI to 3, SHDN and RS are both at +5. I’ve got leds hooked up the pot outputs.

I’m trying to use the code from one of the sites linked before (slightly tweaked and posted below) but unfortunately the potentiometers dont appear to be doing anything. I’m really just after help to get the simplest code to control the pots possible, the examples on the sites seem like overkill when looking at the arduino Spi library.

/* 4 MIDI Controlled Digital Potentiometers

by Sebastian Tomczak
24 May 2008
*/

/* INITIALISATION */
// Pins
int ss1 = 2;
int ss2 = 13;
int clk = 4;
int mosi = 3;

// Variables and bytes
byte pot0 = B00010001; // write to pot 0
byte pot1 = B00010010; // write to pot 1
int baud = 31250;
byte data;
byte channel;
byte ccnumber;
byte ccvalue;
int flag_previous = 0;
/* flag_previous meanings:
-1 = note off status
-2 = note off pitch
0 = no action / waiting
1 = note on status
2 = pitch
3 = cc status
4 = cc number
*/

/* SETUP */
void setup() {
Serial.begin(baud);
pinMode(ss1, OUTPUT);
pinMode(ss2, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(mosi, OUTPUT);
digitalWrite(ss1, HIGH);
digitalWrite(ss2, HIGH);
}

/* MAIN PROGRAM */
void loop () {
if(Serial.available() > 0) {
data = Serial.read();

// deal with cc data
if((data >= 0xb0) && (data < 0xc0) && (flag_previous == 0)) {
channel = data & B00001111;
flag_previous = 3;
}
else if((data < 0x80) && (flag_previous == 3)) {
ccnumber = data;
flag_previous = 4;
}
else if((data < 0x80) && (flag_previous == 4)) {
ccvalue = data;
doCC(channel, ccnumber, ccvalue);
flag_previous = 0;
}
// done with cc data
}
}

/* FUNCTIONS */
void spi_transfer(byte working) {
for(int i = 1; i <= 8; i++) {
if (working > 127) {
digitalWrite (mosi,HIGH);
}
else {
digitalWrite (mosi, LOW);
}
digitalWrite (clk,HIGH);
working = working << 1;
digitalWrite(clk,LOW);
}
}

void spi_out(int ss, byte cmd_byte, byte data_byte) {
digitalWrite (ss, LOW);
spi_transfer(cmd_byte);
spi_transfer(data_byte);
digitalWrite(ss, HIGH);
}

void doCC(byte channel, byte ccnumber, byte ccvalue) {
if(ccnumber == 1) {
if(channel == 0) {
spi_out(ss1, pot0, ccvalue);
}
else if(channel == 1) {
spi_out(ss1, pot1, ccvalue);
}
else if(channel == 2) {
spi_out(ss2, pot0, ccvalue);
}
else if(channel == 3) {
spi_out(ss2, pot1, ccvalue);
}
}
}

Was finally able to get a result out of the ic, code still needs some work to make it more user friendly but at least is goes

int ss1 = 2;
int clk = 4;
int mosi = 3;

// Variables and bytes
byte pot0 = B00010001; // write to pot 0
byte pot1 = B00010010; // write to pot 1

void setup()
{ Serial.begin( 9600 );

pinMode( ss1, OUTPUT );
pinMode( clk, OUTPUT );
pinMode( mosi, OUTPUT );

pinMode( ledPin, OUTPUT );

digitalWrite( ss1, HIGH );
digitalWrite( clk, HIGH );

Serial.println( “Ready.” );
}

void loop()
{ digitalWrite( ss1, LOW );
Serial.println( “LOW” );

spi_transfer( B00010011 );
spi_transfer( 0 );

digitalWrite( ss1, HIGH );
delay( 1000 );

digitalWrite( ss1, LOW );
Serial.println( “HIGH” );

spi_transfer( B00010011 );
spi_transfer( 255 );

digitalWrite( ss1, HIGH );
delay( 1000 );
}

void spi_transfer( byte working )
{ int i;

digitalWrite( clk, LOW );

for( i=1; i<=8; ++i )
{ if( working > 127 )
digitalWrite( mosi, HIGH );
else
digitalWrite( mosi, LOW );

digitalWrite( clk, HIGH );
working = working << 1;
digitalWrite( clk, LOW );
}
}