Pages: [1]   Go Down
Author Topic: Attiny85 and SPI to talk to MCP411000  (Read 684 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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!
Logged

Pages: [1]   Go Up
Jump to: