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:
// Written by Nick Gammon
// April 2011
// what to do with incoming data
byte command = 0;
const byte counterPin = 3;
const byte counterInterrupt = 1; // = pin 3
FreqPeriodCounter counter(counterPin, micros, 0);
void setup (void)
// have to send on master in, *slave out*
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// turn on interrupts
SPCR |= _BV(SPIE);
attachInterrupt(counterInterrupt, counterISR, CHANGE);
} // end of setup
// SPI interrupt routine
byte c = SPDR;
// no command? then this is the command
command = c;
SPDR = 0;
// add to incoming byte, return result
SPDR = frq; // spit out Frquency Reading
} // end of switch
} // end of interrupt service routine (ISR) SPI_STC_vect
void loop (void)
if(counter.ready()) frq = (counter.hertz());
// if SPI not active, clear current command
if (digitalRead (SS) == HIGH)
command = 0;
} // end of loop
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 scope
SPI_Slave_v1:23: error: 'SPCR' was not declared in this scope
SPI_Slave_v1:23: error: 'SPE' was not declared in this scope
SPI_Slave_v1:26: error: 'SPIE' was not declared in this scope
SPI_Slave_v1.cpp: In function 'void SPI_STC_vect()':
SPI_Slave_v1:36: error: 'SPDR' was not declared in this scope
SPI_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.htm
So in general I'm confident that it is going to work. I just don't know how.
Any help is appreciated.