sine wave

Jobi-Wan:
When your sine wave stops and resumes, it doesn't pick up where it left off, but where it would have been if it hadn't stopped. So it looks like your code keeps running, but the DAC output stops updating for a while.

Did you leave the /LDAC pin floating?

Yes all unused pins are floating. Excellent point. I will keep it low and see what happens. Thank you!

[edit] It now works as expected. The problem was the latch pin that should be kept at logic level low (ground) if the DAC is to update the analog output level on each rising edge if the chip select pin (CS)

Just a starter, this function bit bangs a number passed to it. You don't need to write a function to output each individual number.

void SPIbang(int val){
  for( int i=0; i<8; i++) {
    if( (val & 0x80) == 0) {
      digitalWrite(SDIPIN, LOW); 
    }
      else {
      digitalWrite(SDIPIN, HIGH);
      }
     digitalWrite(SCKPIN,HIGH); // toggle clock
     digitalWrite(SCKPIN,LOW);
  val = val << 1;
  }
}

Now write your top level code to take the numbers from an array of the sin values. Hint you can precalculate them in the setup function once using the sin function and then use a for loop to pick out the next value to pass to the bit bang function.

I don't understand anything of that but I found this thread and was thinking about a different approach.

Since the MCP4801 use a 16bit data word, would it be possible to use something like this?

PORTB = PORTB & B11111011;  // bit2 low
SPI.transfer16(0x3FE0); // send the data to the chip using the SPI-function -  0011111111100000
PORTB = PORTB | B00000100;  // bit2 high

Is transfer16 capable of accepting a HEX value written like that?

I gave transfer16 a try today and it went very well. The code went from almost 70% of the amount available on my UNO to 7% !!! And that includes a library that wasn't there before.

The frequency went from 45Hz to 630Hz and there are still possible improvements to make. (I still use digitalwrite for the chip select).

This has been extremely educational despite the disheartening comments. We all have different levels of experience and I wouldn't have gotten this far and learned so much if it wasn't for my initial and very crude bit banging approach. It gave me a very good understanding what SPI actually does and it did work as expected, albeit a bit slow.

#include <SPI.h>

const int CS = 10; // ENABLE/CS/SS/Chip select/Slave select
const int SDI = 11; // MOSI/SDI/Data
//const int SCK_ = 11;
// MISO is not used


void setup() {
  pinMode(CS, OUTPUT);
  pinMode(SDI, OUTPUT);
  pinMode(SCK, OUTPUT);

  SPI.begin();  // initialize SPI:

}

void loop() 

{
 // delay(1000);
  
  onetwoeight();
  onethreefive();
  onefourtwo();
  onefivezero();
  .......
  sevenseven();
  eightfour();
  nineone();
  nineeight();
  onezerofive();
  oneonethree();
  onetwozero(); 
}

void onetwoeight() //128

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1800); // 128 0001 10000000 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onethreefive() //135

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1870); // 135 0001 10000111 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onefourtwo() //142

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x18E0); // 142 0001 10001110 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onefivezero() //150

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1960); // 150 0001 10010110 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onefiveseven() //157

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x19D0); // 157 0001 10011101 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onesixfour() //164

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1A40); // 164 0001 10100100 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onesevenone() //171

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1AB0); // 171 0001 10101011 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void oneseveneight() //178

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1B20); // 178 0001 10110010 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void oneeightfive() //185

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1B90); // 185 0001 10111001 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onenineone() //191

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1BF0); // 191 0001 10111111 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onenineeight() //198

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1C60); // 198 0001 11000110 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twozerofour() //204

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1CC0); // 204 0001 11001100 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twozeronine() //209

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1D10); // 209 0001 11010001 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twoonefive() //215

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1D70); // 215 0001 11010111 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twotwozero() //220

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1DC0); // 220 0001 11011100 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twotwofive() //225

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1E10); // 225 0001 11100001 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twothreezero() //230

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1E60); // 230 0001 11100110 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twothreefour() //234

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1EA0); // 234 0001 11101010 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twothreeeight() //238

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1EE0); // 238 0001 11101110 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofourone() //241

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1F10); // 241 0001 11110001 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofourfive() //245

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1F50); // 245 0001 11110101 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofourseven() //247

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1F70); // 247 0001 11110111 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofivezero() //250

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1FA0); // 250 0001 11111010 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofivetwo() //252

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1FC0); // 252 0001 11111100 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofivethree() //253

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1FD0); // 253 0001 11111101 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofivefour() //254

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1FE0); // 254 0001 11111110 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void twofivefive() //255

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1FF0); // 255 0001 11111111 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onetwozero() //120

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1780); // 120 0001 01111000 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void oneonethree() //113

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1710); // 113 0001 01110001 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void onezerofive() //105

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1690); // 105 0001 01101001 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void nineeight() //98

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1620); // 98 0001 01100010 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void nineone() //91

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x15B0); // 91 0001 01011011 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void eightfour() //84

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1540); // 84 0001 01010100 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void sevenseven() //77

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x14D0); // 77 0001 01001101 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void sevenzero() //70

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1460); // 70 0001 01000110 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void sixfour() //64

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1400); // 64 0001 01000000 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}

void fiveseven() //57

{

digitalWrite(CS, LOW); //initialize the chip
SPI.transfer16(0x1390); // 57 0001 00111001 0000
digitalWrite(CS, HIGH); // de-initialize the chip
}