Nano 33 SPI and seems to lock up?

Code Below
I am a first-time user of the Nano 33. This same code works fine on Uno/Pro Micro. If I flash it onto the Nano 33, it no longer appears as a com port and doesn't seem to be doing anything. I have to double-click the reset button to get the bootloader to start up. I am unsure how to debug my problem since I am not getting any compiler errors and do not seem to get any life out of the Nano33 with this code. I am guessing it has something to do with SPI.

Any ideas are appreciated. Thanks!

#include <SPI.h>

// Define pin numbers for Chip Select and Reset
#define csPIN  10
#define rstPIN  8


void setup() {
  byte statusByte, receivedData;
  Serial.begin(9600);
  pinMode(rstPIN, OUTPUT);
  pinMode(csPIN, OUTPUT);
  SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE3));
  SPI.begin();
  digitalWrite(rstPIN, LOW);
  delay(1000);
  digitalWrite(rstPIN, HIGH);
  SPIcycle(0x12, 0x18, statusByte, receivedData);
}

void SPIcycle(int firstbyte, int secondbyte, byte &statusByte, byte &receivedData) {
  digitalWrite(csPIN, LOW);
  delay(26);
  statusByte = SPI.transfer(firstbyte);
  receivedData = SPI.transfer(secondbyte);
  delay(26);
  digitalWrite(csPIN, HIGH);
}

void rxdata() {
  byte statusByte, receivedData;
  SPIcycle(0x00, 0x02, statusByte, receivedData);
    // Print the received status byte and data
    Serial.print("RX: ");
    Serial.print(statusByte, HEX);
    Serial.print(" ");
    Serial.println(receivedData, HEX);
}
void loop() {
 delay(200);
 rxdata(); 
}

Hi @garnerm91. I see the problem here:

If you check the documentation for SPI.beginTransaction:

https://www.arduino.cc/reference/en/language/functions/communication/spi/begintransaction/

Note that calling SPI.begin() is required before calling this one.

So you must swap the order of those two lines:

  SPI.begin();
  SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE3));

That works. Thank you. Curiously, it doesn't cause problems on Uno or Promicro. The problem makes sense, though. I was recycling an old project to test out using a level shifter. The Nano 33 was the only 3.3v board that was readily available to play with for this project. Thanks again.

You are welcome. I'm glad it is working now.

Regards, Per

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.