Go Down

Topic: Sine Wave Library for the Due (Read 3400 times) previous topic - next topic

RIN67630

#15
Dec 14, 2017, 11:50 pm Last Edit: Dec 16, 2017, 07:34 am by RIN67630 Reason: adding image
Tweaking it somewhat, I could get a sinusoide at ~14KHz, with 8-9 samples pro cycle.

Code: [Select]

//High Speed Sinus/Cosinus generation for Arduino Due.


int x; // Sinus wave
int y; // Cosinus wave
int i = 0;  // just to record the number of steps
int n = 2;// Changing n will change the sine frequency and its precision.
//           2   = 8-9 steps per wave 14kHz on the due
//           8   =~ 50 steps per wave
//           57  =~ 1° steps (360 steps per wave, best precision 0,1% distorsion only
//           553 = 0,1° steps (3600 steps per wave)
//           25  = ~1kHz on the due
int m; //Signal to DAC
boolean t;
long microsec;


void setup()
{
  Serial.begin(9600);
  analogWriteResolution(12);
  y = 1800;  // 10800 to get 0,77V on the due.
  x = 0;
  pinMode(4,OUTPUT);
  noInterrupts();
  }

void loop()
{
  y -= x / n;
  x += y / n;
  ++i ;
  //t = !t;             // uncomment to measure the sample speed on D4
  //digitalWrite(4,t);
  m= x + 1880;    
  analogWrite(DAC0, m);
}



Go Up