Go Down

Topic: Attiny85 and SPI to talk to MCP411000 (Read 956 times) previous topic - next topic

thebeast

Hi, I've scanned/searched the forums for quite a while now, but I've come up empty. 

I'm trying to shrinkify a project that was written for the Arduino.  I'm using a modified version of the sample SPI code and some sniplets from http://blog.makezine.com/2009/08/19/collins-lab-guitar-pedal-modding-wi/.

I've figured out how to setup the attiny85 and send code to it.  I've even gotten the software serial library to work, but I'm having zero luck getting SPI to work.  For example, with the code below using Colin's original sketch;

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
}

returns an error:

WavePot.ino: In function 'char spi_transfer(char)':
WavePot:79: error: 'SPDR' was not declared in this scope

My question is two fold:

1: is it even possible to control this SPI digital pot with the attiny85?
2: if it is, is there just a library I can use?
   2a: if so, where is that library?

Thank you in advance!

-Ben

Delta_G

The ATtiny85 doesn't have real SPI so there isn't a SPDR to be writing to.  It uses a USI (Universal Serial Interface).  It can fake SPI in two modes.  And it can handle I2C.  The data sheet explains it pretty well. 

See this thread for how to get it going.

http://arduino.cc/forum/index.php/topic,152432.0.html

thebeast

Thanks for the quick reply.  The datasheet may explain it, but that doesn't mean that I understand it!   I'm lost. Losty-lostlost. 

Delta_G

The big difference is that instead of SPDR you have USIDR.  And you have to drive the clock yourself in software or externally.  There's no automatic clock like there is on the 328P.  And there's a different flag to check, but it works kind of the same. 


If you're doing what it looks like you're doing and sending out bytes as an SPI master, then the code in that thread should work just like what you've got there would on an arduino.

If you want to run the ATtiny as an SPI slave, that shouldn't be too difficult to develop from there. 

Nick Gammon

Quote

2: if it is, is there just a library I can use?
   2a: if so, where is that library?


I Googled "attiny85 spi library" and got 35,800 hits, if that helps.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

thebeast



I Googled "attiny85 spi library" and got 35,800 hits, if that helps.


I've gone through MOST of those search options and had little luck.  Googling your given parameters gives this search result 

No results found for "attiny85 spi library".   Most of the search results link either back to here or to other people asking about a library. 

I realized, after I rested, that I was missing the download link for the libraries posted in the forum linked above.  Embarrassingly enough, I had my browser sized weirdly and the bottom of the page was cutoff, so the page wasn't displaying attachments.  I thought the attached library was just your example-but now I've found what I was missing from your previous threads. 

Thank you folks!  I'm a bit stressed because I've had about 10 weeks of basic C instruction in my 30+ years on this spinny blue thing and I'm trying to learn as much as possible as quickly as possible.  I'm rebooting my career, getting away from small business consulting and retraining to be an electronics engineer.   It's a LOT tougher with kids, a mortgage, and a fat belly to find time to learn this stuff-especially when running a business, being a stay at home dad, and going to school full time.

I can go from "aha!!! I'm starting to get it...It's not that hard" to "I'm an idiot who should have taken programming classes in the 90's and now I'm too old to learn this stuff" in a few seconds.


thebeast


The big difference is that instead of SPDR you have USIDR.  And you have to drive the clock yourself in software or externally.  There's no automatic clock like there is on the 328P.  And there's a different flag to check, but it works kind of the same. 


If you're doing what it looks like you're doing and sending out bytes as an SPI master, then the code in that thread should work just like what you've got there would on an arduino.

If you want to run the ATtiny as an SPI slave, that shouldn't be too difficult to develop from there. 


Thank you!  I'm less exhausted and starting to get it.  It's not that complicated, but it's all new and so the learning curve is still a bit steep.

Nick Gammon

I had forgotten when I made the post above that I had written a small interface myself for the Attiny. :)

All documented here:

http://www.gammon.com.au/spi

In particular in this post: http://www.gammon.com.au/forum/?id=10892&reply=5#reply5
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

thebeast


I had forgotten when I made the post above that I had written a small interface myself for the Attiny. :)

All documented here:

http://www.gammon.com.au/spi

In particular in this post: http://www.gammon.com.au/forum/?id=10892&reply=5#reply5



It's really nicely laid out and easier to understand than I thought it would be.  Thank you!

Go Up