I must admit i didn't understand every line of code of the sketch (and i tried to!...), but finally i understood how the whole thing works and i came up with a variable frequency and different kind of waveforms.
What i wanted to do then was a realtime variable waveform switch with something like a pot or sorta.
I tried to search for something similar in the forum and google but i didn't came up with a solution yet...
The part of the code i have to work on i think is this one, because that's where i've modified it to make other waveforms working.
// TIMER1 will overflow at a 62.5KHz(Sampling frequency).
// Updates the OCR1A value and the accumulator.
// Computes the next sample to be sent to the PWM.
ISR(TIMER1_OVF_vect)
{
static uint8_t osc = 0;
// Send oscillator output to PWM
OCR1A = osc;
// Update accumulator
phaseAccumulator += phaseIncrement;
index = phaseAccumulator >> 8;
// Read oscillator value for next interrupt
osc = pgm_read_byte( &sineTable[index] );
// osc = pgm_read_byte( &squareTable[index] );
// osc = pgm_read_byte( &triTable[index] );
}
I've tried with analogRead() from a pot and an "if" construct to change the osc "value" but it doesn't work, and also the other pot for the variable stopped working.
:-? I guess that's something i'm missing in the overall process of creating the signal? any help?
Why not eliminate the switch in the ISR altogether, with a global table pointer selected in 'loop', which is what (I think) AWOL was suggesting?
Keep those ISRs lean!
That's exactly what I meant.
The global table pointer is just an "unsigned int", and you add the current value of "index" to it to get the next value.
Though, thinking about it further, for an even leaner ISR, you'd probably want to copy the current table to RAM when you changed the waveform selection.
Quicker access to table entries, at the expense of RAM.
Sorry for the delay of my reply but these have been crazy days...
By the way, i think i understand what you mean, but really i don't know how to realize it. My expertise in coding is not that great though :-[
So the wave selection has to be outside of the interrupt service routine, 'cause the fact that it is someway linked with something happening inside of the loop will freeze the execution as long as the loop keep going.
Is it right?
And i think that's why the waveform does not change while i'm pushing a button, as i'm controlling the frequency of the tone with 8 different buttons. Instead it changes if i control the frequency with just 1 pot, because i'm not getting stuck in the loop.
That's what i came up to, but i don't know if i'm right, 'cause i didnt't find a solution yet.
I tried to put the switch outside of the ISR, but it doesnt work because i don't know how to point at that value.