Im trying to use an Attiny85 as an SPI Slave to do some low Frequency measurements while the Master (an Arduino Mega) is connected to LabView running the Arduino Toolkit. When I say low Frequency I mean like 1kHz maximum. The Frequency to be measured is from an Flowmeter which delivers a nice Squaretype signal.The Master cannot be used for the measurement as there are multiple sensors hooked up which are polled by the LabView Software directly.

Now what I did:I first flashed an SPI Slave Code to an Arduino Nano (for testing the SPI connection and the Frquency measurement) which I had lying around.This worked flawlesly. Here is the Code:

Now I´m trying to port this Code to an Attiny85. I installed the ATtiny Core libraries and can flash the ATiny85. But I get the following error when trying to compile the code presented above:

SPI_Slave_v1.cpp: In function 'void setup()':SPI_Slave_v1:20: error: 'MISO' was not declared in this scopeSPI_Slave_v1:23: error: 'SPCR' was not declared in this scopeSPI_Slave_v1:23: error: 'SPE' was not declared in this scopeSPI_Slave_v1:26: error: 'SPIE' was not declared in this scopeSPI_Slave_v1.cpp: In function 'void SPI_STC_vect()':SPI_Slave_v1:36: error: 'SPDR' was not declared in this scopeSPI_Slave_v1.cpp: In function 'void loop()':SPI_Slave_v1:60: error: 'SS' was not declared in this scope

Now I understand that the Attiny uses USI as a basis for the SPI interface. But where and how do I declare the Pins (MISO, SPCR etc.) I need for the SPI to work properly? The "normal" SPI.h libraries for the 328 etc. do not need to be configured.However I found an example for an SPI Slave with USI on an Attiny85 here:http://avrhelp.mcselec.com/index.html?using_usi_universal_serial_int.htmSo in general I'm confident that it is going to work. I just don't know how.Any help is appreciated.

thank you for the reply.It seems like the task is more complex than I initially thought. I personally wont be able to port the above linked Code to C. I wonder why nobody else has written an SPI Slave Code for the Attiny using the Arduino IDE. I found the I²C Code for the ATtiny here:http://www.arduino.cc/playground/Code/USIi2c .However, this I²C port does not use interrupts and the main loop of the code is stuffed with I²C Communication code. As Im exploiting frequency measuremnents here (which might be scallable in the near future) I find that rather unelegant. The above given SPI Slave example does use interrupts and the main loop is used heavily for the frequency measurement.

The ATtiny85 I will be using is just supposed to constantly measure the frequency of one flowmeter. When polled for it the ATtiny85 should transfer the measured frequency via SPI to the master. I guess a low bitrate will be sufficient for my case. Thus 9600 baud should do the job.

A minimum implementation would involve an interrupt to detect changes on the SCK pin. Once in that isr, process the CS line as well as the MOSI line.

No need for that. The hardware does most of the work for you. You just select an external pin as clock input and wait for the complete byte to arrive (which you can do via polling or generate an interrupt when it's complete).

The Tiny85 datasheet has example code.

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

This is true, but for my initial test it was sufficient. The Flowmeter I´m using delivers ~2000 pulses per liter. I'm expecting a throughflow of 500ml/min max. Thus I'm expecting an input frequency of 16,666667777 Hz to start with. So the limitation to a value of 255 is more than sufficient for now.However, I'm going to implement an rpm meter in the same fasion. And for this rpm meter I will need the higher frequency measurement capabilities and consequently a interface with higher values (than 255). Note that the rpm meter will be on a separate ATtiny85.

@fungus:

I found some example Code for the ATtiny85 to set up as an SPI Slave in the Datasheet:

The code is size optimized using only eight instructions (plus return). The code exampleassumes that the DO and USCK pins have been enabled as outputs in DDRB. The value storedin register r16 prior to the function is called is transferred to the master device, and when thetransfer is completed the data received from the master is stored back into the register r16.Note that the first two instructions is for initialization, only, and need only be executed once.These instructions set three-wire mode and positive edge clock. The loop is repeated until theUSI Counter Overflow Flag is set.

But Im no good at programming and can not port this to the Arduino IDE. Can you? I guess this would be helpful for future reference as well.

I'm not sure I'd do it that way though, it blocks program flow and probably won't work very well. This is one place where an interrupt is called for, I'd do it something like this: (warning, untested code)

fungus and dhenry, thank you so much for the code and input you provided.

However I was trying for some time today and couldn´t get the ATtiny to transfer some Frequency readings via SPI. Here is a Picture of the setup I use.At the bottom right you´ll see a DSO Quad. I used the DSO Quads Wave output to generate a 10 Hz Square Signal for the ATtiny85 at its Pin 3.

However This Setup and the code which I used Did´t return any results.

Did I implement the provided SPI Code correctly ? I changed USIDR to USIBR everywhere. Was that correct?And I was wondering how to implement the SS (Slave Select) PIN. I the Original Code I posted in my first Thread the Arduinos SPI library was used for that. I guess I can`nt use that on the ATtiny85.