3

I am trying to establish a communication between a sensor and a M0 -pro using SPI. The code :

    #include <SPI.h>

    unsigned int result  = 0;
    void readAngle();

    void setup() {
      Serial.begin(9600);
      SPI.setBitOrder(MSBFIRST);
      SPI.setClockDivider(SPI_CLOCK_DIV8); //sets the clock speed for data transfer, slowed down 8 times
      SPI.setDataMode (SPI_MODE1);

      digitalWrite(11,HIGH);
      pinMode(11,OUTPUT);

      SPI.begin();
      SerialUSB.println("First Try");
}

    void loop() {
      readAngle();
      SerialUSB.println(result);
      delay(500);


}

    void readAngle(){
        digitalWrite(11,LOW); //Turn SS on, start communication
        result = SPI.transfer16(0xFFFF); // the read command and the response received
        result = (result << 2) >> 2; // get rid of the last 2 bits, error(14) + parity(15) 
        digitalWrite(11,HIGH); //Turn SS of

}

The code works perfectly with an UNO, but with M0, it event doesnt print the first SerialUSB.println("First Try");. The moment i erase the lines that have SPI commands in it, the serial monitor starts working again. As i said in the titel i am using the ICSP port on M0. I use the native usb port to programm.

I think i am missing something very basic. I checked many different forum posts, but there is no indication of M0 requiring other SPI commands. Any help is much appriciated.

PS: i didnt go into detail with the sensor and its datasheet because since it works fine with uno, the cause of the problem should be it.

2 Answers2

0

Which version of the Arduino IDE are you using ? If its an older one, try to get the latest one, sometimes that solves issues like the one you have.

Dogus Ural
  • 118
  • 3
0

You must declare that the pin as an output before you use it.

Do this by using the pinMode() function, like this:

pinMode(11, OUTPUT);          // sets the digital pin 11 as output

Add the above line at the beginning of your setup() function.

sa_leinad
  • 3,118
  • 1
  • 20
  • 49