Compiling on the UNO but not on the DUE

Hi to everyone,

I have the following problem.

I was using Arduino UNO and I wanted to switch a complete sketch to the DUE. If I compile the sketch selecting first as board the arduino UNO everything works well.

If I compile the sketch selecting the arduino DUE board I have some errors.

One of the error for example is due to the thing that I´m not declaring a variable but I´m just using it, as below:

SPDR = dacSPI0;

This is the first line in which the variable SPDR appears and the compiler tells me that this variable is undefined.

Could you explain why this is happening? The compiler is different?

Hello f.schiano,

In several aspects, UNO and DUE libraries are different given that they are referenced to their MCUs (AVR and SAM respectively). The SPDR or SPI data register you mention, belongs to the on-chip SPI controller of the AVR and is identified/declared different from the ATSAM3X8E, where two SPI controllers are used. Even though the Arduino/Atmet tried to adapt DUE to the Arduino environment, sometimes it is necessary to do a 'porting' like when using timer registers or like in your case. Indeed, their IDEs are different too. If you can share the UNO sketch you are trying to run in your DUE, we may help with with the porting. Regards!

Thanks for your reply.

The following is the sketch I´m trying to port:

       // write out through DAC A
      void write_valueX(int sample)
      {
        // splits int sample in to two bytes
        byte dacSPI0 = 0;
        byte dacSPI1 = 0;
        byte SPDR = 0;
        byte SPSR = 0;
        byte SPIF = 0;
        
        dacSPI0 = (sample >> 8) & 0x00FF; //byte0 = takes bit 15 - 12
        dacSPI0 |= (1 << 7);    // A/B: DACa or DACb - Forces 7th bit  of    x to be 1. all other bits left alone.
        dacSPI0 |= 0x10;
        dacSPI1 = sample & 0x00FF; //byte1 = takes bit 11 - 0
        dacSPI0 |= (1<<5);  // set gain of 1
        digitalWrite(SLAVESELECT0,LOW);
        SPDR = dacSPI0;			  // Start the transmission
        while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
        {
        };
       
        SPDR = dacSPI1;
        while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
        {
        };
        digitalWrite(SLAVESELECT0,HIGH);
        //delay(2);
      }
       
      // write out through DAC B
      void write_valueY(int sample)
      {
        // splits int sample in to two bytes
        byte dacSPI0 = 0;
        byte dacSPI1 = 0;
        byte SPDR = 0;
        byte SPSR = 0;
        byte SPIF = 0;
        
        dacSPI0 = (sample >> 8) & 0x00FF; //byte0 = takes bit 15 - 12
        dacSPI0 |= 0x10;
        
        dacSPI1 = sample & 0x00FF; //byte1 = takes bit 11 - 0
        dacSPI0 |= (1<<5);  // set gain of 1
        
        digitalWrite(SLAVESELECT0,LOW);
        SPDR = dacSPI0;			  // Start the transmission
        while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
        {
        };
        
        SPDR = dacSPI1;
        while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
        {
        };
        digitalWrite(SLAVESELECT0,HIGH);
        //delay(2);
      }
      
      
      ////////////// DAC Functions ////////////////////
      void write_note(int i) {
        write_valueY(i);
        write_valueX(i);
      }
       
      // **************************************************
      // SPI for DAC
       
      void SPI_setup(){
        byte SPCR = 0;
   
        byte clr;
        pinMode(DATAOUT, OUTPUT);
        pinMode(SPICLOCK,OUTPUT);
        pinMode(SLAVESELECT0,OUTPUT);
        digitalWrite(SLAVESELECT0,HIGH); //disable device
       
        SPCR = (1<<SPE)|(1<<MSTR) | (0<<SPR1) | (0<<SPR0);
        clr=SPSR;
        clr=SPDR;
        Serial.println("SETUP DAC COMPLETED!");
        delay(10);
      }

Of course this is just the interesting part for you to help me otherwise the entire sketch will be too big!! :slight_smile:

Thanks a lot,
Fab.

Have a look at this link http://forum.arduino.cc//index.php?topic=157203.msg1424519#msg1424519

It focuses on issues to do with DUE and SPI. Its more about using it in slave mode which you do not need but there are, hopefully, loads of info to resolve your issue.