SPI does not work, please help!!

Hello guys,
I’m quiet new to this forum. I’m programing arduino due to control my DSP (select channels) using spi, although the code I use seems to be right and it converts, but as i press a button for it to send code trough it does nothing, all pins are always high except MISO its low (checked with scope). The SPI is configured to work with DSP. Any ideas why?
I use SPI header and pin 4 for ss. I was first thinking its the board because I wired it wrong as it is very unclear witch one to use ICSP or ISP so i bough another board.

#include <SPI.h>

int CD;
int aux;
int ps4;
int PC;



void setup() {
  
 pinMode(30, INPUT);//STATING INPUTS
 pinMode(31, INPUT);
 pinMode(32, INPUT);
 pinMode(33, INPUT);
  pinMode(4, OUTPUT);//STATING INPUTS
 pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
 
  SPI.begin(4);
  SPI.setClockDivider(4, 16);//SETUP SPI SPEED 3Mhz yh slow... if waited for ppl to config 22Mhz...
  SPI.setDataMode(4, SPI_MODE3); //setup SPI type
  SPI.setBitOrder(4, MSBFIRST);
  //PULL CLACH 3 TIMES turn dsps SPI on.
  digitalWrite(4, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(4, HIGH);


}

void loop() {
 
if(digitalRead(30) == HIGH)
CD++;
if (CD>1);
CD==0;
if(digitalRead(31) == HIGH)
aux++;
if (aux>1);
aux==0;
if(digitalRead(32) == HIGH)
ps4++;
if (ps4>1);
ps4==0;
if(digitalRead(33) == HIGH)
PC++;
if (PC>1);
PC==0;



   
   if (CD==1 ) {
 aux==0;
 ps4==0;
 PC==0;
     
     SPI.transfer(4, 0b0000000, SPI_CONTINUE);//chip adress
    SPI.transfer(4, 0xF000, SPI_CONTINUE);//sub adress
    SPI.transfer(4, 0x0060, SPI_CONTINUE ); //mute_1 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00); //mute_1 muted

    SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0061, SPI_CONTINUE ); //mute_2 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00); //mute_2 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0062, SPI_CONTINUE ); //mute_3 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00); //mute_3 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x005F, SPI_CONTINUE ); //mute_0 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 unmuted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_0 unmuted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 unmuted
    SPI.transfer(4, 0x01); //mute_0 unmuted
  };
  if (aux==1 ) {
 CD==0;
 ps4==0;
 PC==0;
     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE); //subadress
    SPI.transfer(4, 0x0061, SPI_CONTINUE ); //mute_2 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00); //mute_2 muted

   SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0062, SPI_CONTINUE ); //mute_3 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00); //mute_3 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x005F, SPI_CONTINUE ); //mute_0 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00); //mute_0 muted

    SPI.transfer(4, 0b0000000, SPI_CONTINUE); 
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0060, SPI_CONTINUE ); //mute_1 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 inmuted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_1 unmuted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 unmuted
    SPI.transfer(4, 0x01); //mute_1 unmuted
  };
  if (ps4==1) {
 CD==0;
 aux==0;
 PC==0;
     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x005F, SPI_CONTINUE ); //mute_0 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00); //mute_0 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0060, SPI_CONTINUE ); //mute_1 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00); //mute_1 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0062, SPI_CONTINUE ); //mute_3 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 muted
    SPI.transfer(4, 0x00); //mute_3 muted

    SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0061, SPI_CONTINUE ); //mute_2 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 unmuted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_2 unmuted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 unmuted
    SPI.transfer(4, 0x01); //mute_2 unmuted

  };

  if (PC==0 ) {
 CD==0;
 ps4==0;
 aux==0;
     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x005F, SPI_CONTINUE ); //mute_0 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_0 muted
    SPI.transfer(4, 0x00); //mute_0 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0060, SPI_CONTINUE ); //mute_1 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_1 muted
    SPI.transfer(4, 0x00); //mute_1 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0061, SPI_CONTINUE ); //mute_2 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_2 muted
    SPI.transfer(4, 0x00); //mute_2 muted

     SPI.transfer(4, 0b0000000, SPI_CONTINUE);
    SPI.transfer(4, 0xF000, SPI_CONTINUE);
    SPI.transfer(4, 0x0062, SPI_CONTINUE ); //mute_3 adr
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 unmuted
    SPI.transfer(4, 0x00, SPI_CONTINUE ); //mute_3 unmuted
    SPI.transfer(4, 0x00 , SPI_CONTINUE ); //mute_3 unmuted
    SPI.transfer(4, 0x01); //mute_3 unmuted
  };


}

may be anyone knows any good books/web sites to help learning c?

Hi andrius950

    SPI.transfer(4, 0xF000, SPI_CONTINUE); //subadress
    SPI.transfer(4, 0x0061, SPI_CONTINUE ); //mute_2 adr

Unless it's different on a Due, transfer() sends one byte only. So you will need to split up each of the above statements into two.

  digitalWrite(4, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(4, HIGH);

There is note on the Arduino Due SPI page that says ...

NB : once SPI.begin() is called, the declared (CS) pin will not be available as a general purpose I/O pin

http://arduino.cc/en/Reference/DueExtendedSPI

This could mean that your digitalWrite() statements will not work.

 pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);

These are handled by the SPI library so not needed in your code.

None of this may explain why you are seeing no outputs at all :( Maybe you could write a short test program that does the SPI.begin() and other set up and then just repeatedly sends one byte. Even without your DSP connected, you should see clock, MOSI and CS on your scope.

Regards

Ray

Thanks Ray. I was sending too many bits at a time that probably made everything to freeze i made a small test and split them and it worked, do you know when I split bits should i state spi_last at the end of each adress or there is no need because it is one long bit of information?

Kind regards, Andrew

andrius950: do you know when I split bits should i state spi_last at the end of each adress or there is no need because it is one long bit of information?

It depends on the protocol that the other end is expecting. However, in general I would say that by releasing the CS (by omitting SPI_CONTINUE on the last byte) that the other end will know that the transmission is finished.

Thanks Morgan.

I have split the bites but now came across a new problem i got

SPI.transfer(4, 0xF, SPI_CONTINUE);//sub adress
     SPI.transfer(4, 000, SPI_CONTINUE);//sub adress

from SPI.transfer(4, 0xF000, SPI_CONTINUE);//sub adress

and it comes up with an error saying error: invalid suffix "F" on integer constant How can i avoid that? i have no clue what F actually stands for, except that it is a part of location of the actual mute.

You have to be careful setting pinMode with SPI on the Due, the rules are different from the Uno and pinMode can detach the pin from the SPI hardware so read the docs and start from a known working Due example. The Due also has a fixed idea of which pins can be CS and they must be configured appropriately even if you don't connect them and fake it with other pins.

"0xF" should work in standard C. The prefix "0x" means the number following is hexadecimal. In this case, the decimal equivalent is 15.

Try: "0x0F" or "15" (without the quotes, of course.)

Also if you are converting "0xf000" into two bytes, the first should be "0xf0" and NOT "0xf" as you have written (in the same way that base 10 values "30" is not the same as "3"). Susan