I would like to send data via SPI communication from nano to Teensy 4.1 but i receive only zeros. Thanks for any suggestions.
Code for analysion.
Teensy 4.1:
#include <SPI.h>
const int slaveSelectPin = 10; // Set your SS pin
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(slaveSelectPin, INPUT_PULLUP); // Set SS pin as input with pull-up resistor
// Set up SPI communication
SPI.begin();
}
void loop() {
// Check if SS pin is low (indicating data is available)
if (digitalRead(slaveSelectPin) == HIGH) {
// Read data from SPI
byte data = SPI.transfer(2);
// Print received data to serial monitor
Serial.println(data);
// Wait for SS pin to go high again (indicating end of transmission)
}
}
Arduino Nano:
#include <SPI.h>
void setup() {
// Initialize SPI communication
SPI.begin();
// Set Slave Select pin as OUTPUT
pinMode(SS, OUTPUT);
// Select the Slave
digitalWrite(SS, LOW);
}
void loop() {
// Send 1 via SPI
digitalWrite(SS, HIGH);
SPI.transfer(5);
delay(1000);
}
well me and my mate we were doing it in a rush any suggestion for a code I have no idea how it should look like, also i read i guess here: SPI Arduino Library, connecting SPI devices to Teensy that teensy cant be a slave so you need some library but even after that it didnt work
well i found code to do it with arduino to arduino but when the teensy is the slave and i use this code :
#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;
void setup (void) {
Serial.begin (115200);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect) // SPI interrupt routine
{
byte c = SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}
void loop (void) {
if (process) {
process = false; //reset the process
Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
}
}
i get this error: error: expected constructor, destructor, or type conversion before '(' token
15 | ISR (SPI_STC_vect) // SPI interrupt routine
I've not used a Teensy but I know they are fast micros. I would try sending just 1 byte and see what happens. With slower micros like an 328P based UNO, it's easy to overwhelm the receiving SPI device with bytes. Even with a teensy, you may have to put short delays in between each byte you send.
However, the Teensy my handle SPI differently and this may not be an issue. Hopefully a Teensy user will clarify this.