I am not be able to generate waves for 51.2 khz

#include "DueTimer.h"

/*

  • Sawtooth waveform generation using timer
  • Wave frequency = wf (Hz)
  • Samples per pulse = wn (num)
  • So tatal samples passed per second to O/P = wf * wn
  • i.e. timer frequncy to write out, tf = wf * wn
    */

uint32_t wf =51200 ; // Frequency of 51.2kHz
uint32_t wn = 120; // Length of saw in this program
uint32_t tf = wf * wn;

uint32_t wi;

int saw[] = {
0x22, 0x44, 0x66, 0x88, 0xaa, 0xcc, 0xee, 0x110, 0x132, 0x154,
0x176, 0x198, 0x1ba, 0x1dc, 0x1fe, 0x220, 0x242, 0x264, 0x286, 0x2a8,
0x2ca, 0x2ec, 0x30e, 0x330, 0x352, 0x374, 0x396, 0x3b8, 0x3da, 0x3fc,
0x41e, 0x440, 0x462, 0x484, 0x4a6, 0x4c8, 0x4ea, 0x50c, 0x52e, 0x550,
0x572, 0x594, 0x5b6, 0x5d8, 0x5fa, 0x61c, 0x63e, 0x660, 0x682, 0x6a4,
0x6c6, 0x6e8, 0x70a, 0x72c, 0x74e, 0x770, 0x792, 0x7b4, 0x7d6, 0x7f8,
0x81a, 0x83c, 0x85e, 0x880, 0x8a2, 0x8c4, 0x8e6, 0x908, 0x92a, 0x94c,
0x96e, 0x990, 0x9b2, 0x9d4, 0x9f6, 0xa18, 0xa3a, 0xa5c, 0xa7e, 0xaa0,
0xac2, 0xae4, 0xb06, 0xb28, 0xb4a, 0xb6c, 0xb8e, 0xbb0, 0xbd2, 0xbf4,
0xc16, 0xc38, 0xc5a, 0xc7c, 0xc9e, 0xcc0, 0xce2, 0xd04, 0xd26, 0xd48,
0xd6a, 0xd8c, 0xdae, 0xdd0, 0xdf2, 0xe14, 0xe36, 0xe58, 0xe7a, 0xe9c,
0xebe, 0xee0, 0xf02, 0xf24, 0xf46, 0xf68, 0xf8a, 0xfac, 0xfce, 0xff0
};

void tHandler() {
// Write the sawtooth wave sample to DAC1
analogWrite(DAC1, saw[wi]);

// Print saw wave in serial plotter
Serial.println(saw[wi]);
Serial.print(" ");

// Increment index to next sample or reset it if at the last sample
wi = wi + 1;
if (wi == wn) {
wi = 0;
}
}

void setup() {
// Can be changed to control phase of the O/P
wi = 0;

// Set BAUD rate for Serial stuffs
Serial.begin(9600);

// Set DAC1 for output
pinMode(DAC1, OUTPUT);

// Set resolution of DAC at 12 bits
analogWriteResolution(12);
// Reset analog pin to 0 before starting waveform
analogWrite(DAC1, 0);

// Bind TC3 with ISR, set frequency and start it
Timer2.attachInterrupt(tHandler).setFrequency((double)tf).start();

}

void loop() {
while(1) {
// delayMicroseconds(5000);// Main Loop is not required as ISR handles the waveform work.
}

}

_51.2_khz.zip (4.86 KB)

Several issues in this code:

  • Never Serial Print inside an ISR. Set a Flag inside the ISR and Serial Print in loop() to debug.

  • You set an output frequency of 51200 * 120 = 6144000 Hz ---> This is far beyond the DAC capabilities (Max 2 MHz for a single DAC output).

  • Remember Serial.print() takes several hundreds of us. BTW, set 250000 rather than a slow 9600 bauds.

I made all the changes that u said but even now I am not be able to generate wave for 51.2 khz.It is showing the waves till 10khz but when I am trying to genrate waves more than 10khz on oscilloscope its showing 10 khz only

#include "DueTimer.h"

/*

  • Sawtooth waveform generation using timer
  • Wave frequency = wf (Hz)
  • Samples per pulse = wn (num)
  • So tatal samples passed per second to O/P = wf * wn
  • i.e. timer frequncy to write out, tf = wf * wn
    */

uint32_t wf = 52000; // Frequency of 1Hz. for 51.2 kHz use 51200 and so on.
uint32_t wn = 20; // Length of saw in this program
uint32_t tf = wf * wn;

uint32_t wi; // Sample selector index

int currentSample = 0; // Start from null

int interruptHappened = 0; // Interrupt trigger flag

int sine[] = {
500,655,794,905,976,1000,976,905,794,655,
500,345,206,95,24,0,24,95,206,345
};

/*
int sine[] = {
0x7ff, 0x8d5, 0x9a9, 0xa78, 0xb40,
0xbff, 0xcb2, 0xd59, 0xdf1, 0xe77,
0xeec, 0xf4d, 0xf9a, 0xfd2, 0xff3,
0xfff, 0xff3, 0xfd2, 0xf9a, 0xf4d,
0xeec, 0xe77, 0xdf1, 0xd59, 0xcb2,
0xbff, 0xb40, 0xa78, 0x9a9, 0x8d5,
0x7ff, 0x729, 0x655, 0x586, 0x4be,
0x3ff, 0x34c, 0x2a5, 0x20d, 0x187,
0x112, 0xb1, 0x64, 0x2c, 0xb,
0x0, 0xb, 0x2c, 0x64, 0xb1,
0x112, 0x187, 0x20d, 0x2a5, 0x34c,
0x3ff, 0x4be, 0x586, 0x655, 0x729
};*/

void tHandler() {

interruptHappened = 1; // Set interrupt flag
// Obtain curren sample from the waveform's array
currentSample = sine[wi];

// Write the sawtooth wave sample to DAC1
analogWrite(DAC1, currentSample);

// Increment index to next sample or reset it if at the last sample
wi = wi + 1;
if (wi == wn) {
wi = 0;
}
}

void setup() {
// Can be changed to control phase of the O/P
wi = 0;

// Set BAUD rate for Serial stuffs
Serial.begin(250000);

// Set DAC1 for output
pinMode(DAC1, OUTPUT);

// Set resolution of DAC at 12 bits
analogWriteResolution(12);
// Reset analog pin to 0 before starting waveform
analogWrite(DAC1, 0);

// Bind TC3 with ISR, set frequency and start it
Timer3.attachInterrupt(tHandler).setFrequency((double)tf).start();

}

void loop() {
if(interruptHappened != 0) {
// Print saw wave in serial plotter
Serial.println(currentSample);
Serial.print(" ");
interruptHappened = 0; // Reset interrupt flag
}
}

I bet for another X-Y Problem....

Please, explain what is the final goal of your project, and BTW, when you post your code, do that between code tags <>

The goal of my project is to create the sine wave of 51.2khz.

Why 51.2 KHz ?

To get 51.2kHz at 20 samples per period, you'd 1.024 million conversions/s.

While I guess that's theoretically possible (the datasheet is a bit vague on whether it does 1M or 2M, max), that means you'd only have 84 cycles between each sample, and I doubt that interrupt overhead + arduino overhead + libsam overhead is small enough to allow that. 5x slower seems ... not impossible, given that the analogWrite() code completely resets the DAC for each call, plus wait states, plus calling Atmel libsam functions of questionable (but probably not any better than ASF) efficiency.

Try changing your ISR:

void tHandler() {
  // Obtain curren sample from the waveform's array
  currentSample = sine[wi];
  DACC->DACC_CDR = currentSample;    // Write direct to DACC
  // Increment index to next sample or reset it if at the last sample
  wi = wi + 1;
  if (wi == wn) {
    wi = 0;
  }
}

Or something like that. The analogWrite() in Setup() SHOULD be leaving the DAC in its configured state, ready to accept additional samples... (but be careful not to do anything else to the pin!)
You may have to re-scale your samples; I don't know if the DAC range matches the analogWrite() range (it looks like the latter does some additional scaling.)
(This is just from trying to understand the datasheet; I don't actually know anything about the DAC...)