Using a Mega2560 Writing my first SPI library to become more familiar with the protocol. Was having a hard time as it appeared that the Arduino was refusing to send any data, oscilloscope seemed to confirm this was the voltages would not fluctuate. As a sanity check I tried the Arduino example, which worked and I was able to see the data on the oscilloscope. I compared my code to the example and from trial and error I found that the SPI library was not working when delay or Serial.print was added to the code (was using them for debugging). Changing the delay from 1000 to 1 and slowing down the bus seems to resolve this, increasing delay or bus speed causes it to fail again. What can I do to work around this?
#include <SPI.h>
void setup() {
Serial.begin(9600);
SPI.begin();
delay(100);
}
int i = 0;
void loop() {
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); // Using MCP2515 at 10 Mhz
SPI.transfer(i++ % 255); // Changing data to help show code is working on scope
SPI.endTransaction();
delay(1); // Delay of 1 breaks SPI
}