Arduino Due with SPI declaration problem

I am new to Arduino programming.when iam compiling this code for Arduino Due iam getting these list of errors.Please guide me .....
SPCR was not declared in this scope.
sketch_sep25a.ino: In function 'void setup()':
sketch_sep25a:14: error: 'SPCR' was not declared in this scope
sketch_sep25a:14: error: 'SPE' was not declared in this scope
sketch_sep25a:14: error: '_BV' was not declared in this scope
sketch_sep25a.ino: In function 'void SPI_STC_vect()':

#include <SPI.h>
 
char buf [100];
volatile byte pos;
volatile boolean process_it;
 
void setup (void)
{
Serial.begin (115200); // debugging
 
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE); 
// get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
 
// now turn on interrupts
SPI.attachInterrupt();
 
} // end of setup
 
 
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
// add to buffer if room
if (pos < sizeof buf)
{
buf [pos++] = c;
// example: newline means time to process buffer
if (c == '\n')
process_it = true;
} // end of room available
} // end of interrupt routine SPI_STC_vect
 
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
Serial.println (buf);
pos = 0;
process_it = false;
} // end of flag set
} // end of loop

Hi, for what are you using the SPI?

As far as I understood , it´s not possible to use the SPI library to address devices which need more than 8-bit word.

So be careful

lalith:
SPCR was not declared in this scope.
sketch_sep25a.ino: In function 'void setup()':
sketch_sep25a:14: error: 'SPCR' was not declared in this scope
sketch_sep25a:14: error: 'SPE' was not declared in this scope
sketch_sep25a:14: error: '_BV' was not declared in this scope
sketch_sep25a.ino: In function 'void SPI_STC_vect()':

These errors are referring to the fact that you are trying to use Atmel AVR registers in an ARM setting. The ARM compiler has no idea what SPCR is, for example.

Don't use low-level register instructions unless you really need to. And only then, have a basic understanding about what it is you are trying to do.

Just stick to the high-level SPI commands in the Due SPI lib. There's shouldn't be anything you need you won't find there.

Yeah, what pico said is right.

I will provide the code (clean and commented ) as soon as possible. This is a first version, it´s working but I have still to figure out something:

/*
DAC _MCP4922 to DUE

This sketch allows you to interface the Arduino DUE with a DAC MCP4922.
The DAC needs a 16 bit word to work. 
The first 4 bits are control bits and the remaining are bits which will contain the value you want to send.
We have 12 bit available for our message so we will have a range of values between 0 and 4095.

  Connections
  ====================================================
  
  +3.3V           > 4922 pin 1
  DUE pin 10      > 4922 pin 3   (SS - slave select)
  DUE SCK         > 4922 pin 4   (SCK - clock)
  DUE MOSI        > 4922 pin 5   (MOSI - data out)
  Ground          > 4922 pin 8   (LDAC)
  +3.3V           > 4922 pin 11  (voltage ref DAC B)
  Ground          > 4922 pin 12
  +3.3v           > 4922 pin 13  (voltage ref DAC A)
 
  4922 pin 14 DAC A > 1k resistor > SignalOUT(you can put this signal in a multimeter or oscilloscope to look at it)
  The other wire of the multimeter/oscilloscope should be at the same GND of the Arduino

*/

#include <SPI.h>

void setup() {
  // put your setup code here, to run once:
SPI.begin(10);
SPI.setBitOrder(MSBFIRST);

// Our DAC can operate at Mode 0,0 (which corresponds to mode 0 in the SPI library)
// and in mode 1,1 (which corresponds to mode 3 in the SPI library)

// I don´t know why instead the DAC is working with the following instruction??
SPI.setDataMode(10, 1);

/* 
From the datasheet we understand that the maximum frequency at which the DAC
can operate is 20MHz. The Arduino DUE has a system clock of 84MHz so if we don´t say
anything to the SPI library it won´t change the clock at which we will operate. 
84 MHz is too big for the DAC, to change this we can use the function SPI.setClockDivider(SS,divider)
This function set the clock, for the device on pin SS, to 84/divider. The divider variable 
can only be an integer. For this reason for example to have an operating clock for our device of 1MHz we 
will write: SPI.setClockDivider(SS,84); 
to have an operating clock for our device of 2MHz we will write: SPI.setClockDivider(SS,42); and so on
*/
SPI.setClockDivider(10,84);
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  // The entire message to send a 4095 number will be:
  // 0111 | 111111111111
  
  // The following 2 instructions define the 16-bit word we want to send 
  byte msg1=0b01111111; // 01111111
  byte msg2=0b11111111; // 11111111
  
  SPI.transfer(10,msg1,SPI_CONTINUE);
  SPI.transfer(10,msg2,SPI_LAST);
  delay(1000);
}

I will do a tutorial as soon as possible.

Fab.

Here's some SPI initialization code with Due as the master, which is the usual thing. (I don't know if you noticed the OP's code was trying to set up as the slave, which is unusual, but I'm not sure if the OP realized that.)

This is all that's required. Fairly simple, really. This example sets the SPI speed at 4MHz. The rest should be self explanatory.

  SPI.begin();
  SPI.setDataMode(SPI_MODE0); // MODE 0  (CPOL=0, CPHA=0); 
  SPI.setClockDivider(21);    // 4 MHz, up to 10MHz nRF24L01+ max
  SPI.setBitOrder(MSBFIRST);

  pinMode(CSN_PIN, OUTPUT); // chip select pin for this device
  pinMode(CE_PIN, OUTPUT); // chip enable pin for this device

To write a byte, and read the waiting incoming byte at the same time, call SPI.transfer(). Here it is in a wrapper I use for that:

uint8_t spi_rw(uint8_t b)
{
  return SPI.transfer(b);
}

(In case you are wondering, I use the wrapper for abstraction purposes. This is from code that compiles on a number of different platforms, all with different low-level function names, so I have wrappers around the raw library functions that keeps the main code consistent. Also, you can conveniently use this to abstract bit-banging implementations, etc.)

Here you can find my tutorial :slight_smile: Everything is working !
http://schianorobotics.altervista.org/arduino.html

The next step is to let it work also with the ethernet shield