Triangle Wave Frequency Generator Due

I was wondering if anyone had ideas about how to create a triangle wave frequency generator where the frequency is hard coded. I also need the max frequency to be at least 20 kHz. I was trying to use the code here: http://arduino.cc/en/Tutorial/DueSimpleWaveformGenerator but I am not sure it it possible. I am also using the Due DAC pins. I am new to the Due but I have used the Uno before. Any help would be great!

I also need the max frequency to be at least 20 kHz.

You need to know how many samples you want at that maximum frequency, that will give you the sample frequency and allow you to calculate the maximum increment / decrement of the output value per sample.

Once you have that you set up an interrupt on a timer at that frequency and on each interrupt have the interrupt service routine increment or decrement the output value and then send it to the D/A.

I'm in the middle of starting my little synth project that will output, among other things, a triangle wave (sawtooth, square, sine, and pulse as well). The project you linked to generates its wave forms based on a lookup table. I was going to do it that way, but In the short term, I opted to calculate the wave form by formula.

It isn't tested yet, but I start with a phase counter that is a float with a value between zero and one. Its incremented using a step:

const int sampleRate = 32000; //output sample rate

float waveAPhase1 = 0;
float pitch1 = 440; //pitch in hz
float phaseStep1 = pitch/sampleRate; //how many 32khz steps in a 400hz wave

With that setup, I just need a formula to calculate the waveform I want based on the phase (I guess this is radians). This is a formula I came up with for a triangle wave that crosses zero at 0/1 and .5, and starts positive.

y = abs(4.0*((waveAPhase1+.75)-floor(waveAPhase1+.75))-2.0)-1.0;

its a little more complicated than it needs to be, partly because I wanted all my waves to be positive for the first half of the wave and negative for the second. Also, It should generate a value from -1 to 1, which will have to be scaled and possible offset to positive before being output depending on your bit depth. (note that is could probably be simplified with fmod(). I started using % before I found out it doesn't work for floats, and devised the above floor solution before discovering fmod())

Then everytime I generate another sample, I just have to step the phase and chech that I haven't passed 1:

waveAPhase1 = waveAPhase1 +phaseStep;

if (waveAPhase1 > 1) {
     waveAPhase1 = waveAPhase1 - 1;
}

None of this code is tested. Its all loaded on my Due, but I'm waiting on my scope to see if it outputs correctly. I currently don't have the tools or the electronics knowledge to test.

also, let me add that Grumpy_Mike's post is basically how I'm scheduling the output, but it didn't seem like the timer interrupt arduino code was implemented for the due, so rather than digging into the core timer setup, I just snagged this code:

which implements timers for the Due, and uses the same syntax, i.e.:

Timer4.attachInterrupt(handler).start(1000);

Forget about using floats in any of this, use fixed point variables, these can be implemented by simple ints.

(I guess this is radians)

No it is in arbitrary units depending on the size of the fractional part of the int and the waveforms increment number.