ATtiny 85 and SPI

Hello Guys,

I am quite new to Arduino and digital electronics in General.
Newly I have to work with a Digital Potentiometer (MSP4142) and write a small Code to test how it should work with arduino:

Here it is: It makes a Led connected to Digital 10 blink every 250 mS.

#include <SPI.h> // necessary library
int ss= 10; // using digital pin 10 for SPI slave select
int del=250; // used for various delays
 
void setup()
{
  pinMode(ss, OUTPUT); // we use this for SS pin
  SPI.begin(); // wake up the SPI bus.
  SPI.setBitOrder(MSBFIRST);
  // our MCP4162 requires data to be sent MSB (most significant byte) first
}
 
void setValue(int value)
{
  digitalWrite(ss, LOW);
  SPI.transfer(0); // send command byte
  SPI.transfer(value); // send value (0~255)
  digitalWrite(ss, HIGH);
}
 
void loop()
{
    setValue(255);
    delay(del);
    setValue(0);
    delay(del);
}

But (unfortunately) for my project, I want to work with an ATtiny chip. I have the ATtiny 85. I have been reading a lot on internet about how to hook up ATtiny and DigiPot (thru SPI) and I am totally Lost.

I mean, I think I have to connect :

SCK > Digi2, PIN7 Attiny
SDI > Digi0, PIN5 Attiny

And then I define CS as Digi1, PIN6 Attiny - I don't need the SDO.

It ain't working, It wont even compile in Arduino.

Can anyone tell me if it is at least possible?
Or do I have to use any other ATtiny Chip? If yes which one?

Thanks a Million in Advance for your help.

Clément

You may be able to use a software based SPI library that will work with the tiny85. Attached is one I wrote for UNO/MEGA that compiles for tiny85 but has not been tested on it.

SPIsoft_beta.zip (20.3 KB)

Attiny85 doesnt have hardware SPI, but it has a USI (universal serial interface) which can be programmed to use SPI. Look in google for "attiny usi spi", there are some examples that will help you. (You may want to set attiny speed to 8 mhz.)

Hello both of you!
First of all thanks for your replies :slight_smile:

I found this Tiny SPI library (to use SPI with Arduino) : GitHub - JChristensen/tinySPI: Arduino hardware SPI library for ATtiny44/84, 45/85, 461/861, 2313/4313.

I downloaded it, and based on the example I rewrote my code:

#include <tinySPI.h>     // necessary library
int del=300; // used for various delays

const int LATCH_PIN = 0;           //storage register clock (slave select)
const int CLOCK_PIN = 2;           //shift register clock
const int DATA_PIN = 1;            //data in
 
void setup(void)
{
    //pullups on for unused pins to minimize power consumption
    //pinMode(3, INPUT_PULLUP);
    //pinMode(4, INPUT_PULLUP);    
    
    //set up the pins for software SPI
    pinMode(CLOCK_PIN, OUTPUT);    
    pinMode(DATA_PIN, OUTPUT);
    pinMode(LATCH_PIN, OUTPUT); 
    digitalWrite(LATCH_PIN, HIGH);
}
 
void setValue(int value)
{
  digitalWrite(LATCH_PIN, LOW);
  
  //SPI.begin(); 
  //SPI.transfer(myByte);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value);
  
  digitalWrite(LATCH_PIN, HIGH);
}
 
void loop()
{
    setValue(255);
    delay(del);
    setValue(0);
    delay(del);
}

If you look, you'll see that I am never using the SPI command. Instead of, I used this ShiftOut command. I don't know if it's a standart command, or if its from this TinySPI library.

Unfortunately, after a lot's of try, it is still not working.

can it be because I use a number from 0 to 255 as "value"?
Should I send this information in binar?

Thanks a lot in advance for any help or anything to put me on the way :-/

I found my mistake!
I just added a shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, 0); to initialise the transfert!
now its working :slight_smile:

thanks a lot for your help!!

Thanks for sharing the answer too.