mcp4131 and attiny85

hey,

has anyone been able to get these two chips talking to each other? After many hours of trying every option google and this forum has suggested I haven't made much progress :/

sorry to be asking a question on my first post. Hi everyone!

What have you tried? Looks like all you need is this

int command_data;  upper 4-6 bits are command related, lower 10 are data

digitalWrite (CS, LOW);
SPI.transfer (highByte, (command_data) );
SPI.transfer (lowByte, (command_data) );
digitalWrite (CS, HIGH);

isn't that using the spi library? the attiny can't run it. i have tried using pulseout and USI. this guy managed to get four of them working with a multiplexer, i have tried to simplify his code as well --> http://fanjita.org/serendipity/archives/44-SPI-addressing-with-minimal-pins.html

In that case:

int command_data; upper 4-6 bits are command related, lower 10 are data

digitalWrite (CS, LOW);
shiftout ( dataPin, clockPin, MSBFIRST, highByte (command_data) );
shiftout ( dataPin, clockPin, MSBFIRST, lowByte (command_data) );
digitalWrite (CS, HIGH);

Mmm, that code gives me back error messages from the tiny core. Have i formatted the code correctly?

#define CS     1
#define dataPin      3
#define clockPin 2

void setup() {

pinMode(CS, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);

}

void loop() { 
  
int  command_data;
  
digitalWrite (CS, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, highByte (command_data) );
shiftOut(dataPin, clockPin, MSBFIRST, lowByte (command_data) );
digitalWrite (CS, HIGH);

}

Thanks for the pointers mate :slight_smile:

Huh! I can't get it to compile for an Uno in 1.0 either! Something odd going on with #define, I had to change the pin definitions to this:

byte CS = 2;
byte dataPin  = 3;
byte clockPin  = 4;
int command_data = 0;

void setup() {

pinMode(CS, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);

}

void loop() { 
 // command_data = whatever it's being changed to
  
digitalWrite (CS, LOW);
shiftOut (dataPin, clockPin, MSBFIRST, highByte (command_data) );
shiftOut (dataPin, clockPin, MSBFIRST, lowByte (command_data) );
digitalWrite (CS, HIGH);

}
int  command_data;
  
digitalWrite (CS, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, highByte (command_data) );
shiftOut(dataPin, clockPin, MSBFIRST, lowByte (command_data) );

command_data, being a local variable, is not initialized by the compiler, and is never explicitly valued. So, why do you want to send garbage to the mcp4131?

As i want to control that variable with an analog input voltage [from a pot], i gather i could just replace command_data with 'val' and set that as the value i want.

That code certainly uploads onto the ATTing85 alright, just going to have a tinker with it. Thanks again :)

CrossRoads: Huh! I can't get it to compile for an Uno in 1.0 either! Something odd going on with #define, I had to change the pin definitions to this:

byte CS = 2;
byte dataPin  = 3;
byte clockPin  = 4;
int command_data = 0;

void setup() {

pinMode(CS, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT);

}

void loop() { // command_data = whatever it's being changed to   digitalWrite (CS, LOW); shiftOut (dataPin, clockPin, MSBFIRST, highByte (command_data) ); shiftOut (dataPin, clockPin, MSBFIRST, lowByte (command_data) ); digitalWrite (CS, HIGH);

}

Works a treat!! Thanks!

PaulS:

int  command_data;

digitalWrite (CS, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, highByte (command_data) );
shiftOut(dataPin, clockPin, MSBFIRST, lowByte (command_data) );



command_data, being a local variable, is not initialized by the compiler, and is never explicitly valued. So, why do you want to send garbage to the mcp4131?

Its not necessary in this patch anyway, replacing it with val works fine.

EDIT:: I got it working!! crazy i spent so long to get an analog pot to fade an LED…

Here it is if anyone wants it

byte CS = 1;
byte dataPin  = 2;
byte clockPin  = 0;
byte analogPin  = 3;

int val = 0;

void setup() {

pinMode(CS, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(analogPin, INPUT);




}

void loop() { 
 // command_data = whatever it's being changed to
 
val = analogRead(analogPin);



val = map(val, 0,1024, 0,256);


digitalWrite (CS, LOW);
shiftOut (dataPin, clockPin, MSBFIRST, highByte (val) );
shiftOut (dataPin, clockPin, MSBFIRST, lowByte (val) );
digitalWrite (CS, HIGH);

delay(4);

}