Hi
I found this article on using the ATTiny85 as a function generator using a interrupt-routine and a PWM output.
http://www.technoblogy.com/show?QVN
I'd like to output a sine wave, but I can't visualize how to write the code inside the interrupt routine program-loop. I want to read the amplitude values from a wave table (as suggested in the article). I get that I need to scroll through array-indexes of my wave table periodically, but how would I do the frequency calculation? Taking the wave table length, the Interrupt routine call frequency (20kHz) and a frequency division variable into account. Would it be as simple as this?
(Interrupt Frequency * clock divisor variable) / (Wave table length)
I've added a sine wave table below for reference.
const unsigned char sine[256] = {
131,132,135,137,140,143,146,149,152,155,157,160,163,166,168,171,
174,176,179,181,184,186,189,191,194,196,198,200,202,205,207,209,
211,212,214,216,218,219,221,223,224,226,227,228,229,231,232,233,
234,234,235,236,237,237,238,238,239,239,239,239,240,240,240,239,
239,239,239,238,238,237,237,236,236,235,234,233,232,231,230,229,
227,226,225,223,222,220,219,217,216,214,212,210,208,206,204,202,
200,198,196,194,192,189,187,185,182,180,177,175,173,170,167,165,
162,160,157,154,152,149,146,144,141,138,135,133,130,127,124,122,
119,116,113,111,108,105,103,100, 97, 95, 92, 89, 87, 84, 82, 79,
77, 74, 72, 69, 67, 64, 62, 60, 58, 55, 53, 51, 49, 47, 45, 43,
41, 39, 37, 36, 34, 32, 31, 29, 28, 26, 25, 24, 22, 21, 20, 19,
18, 17, 16, 15, 15, 14, 13, 13, 12, 12, 12, 11, 11, 11, 11, 11,
11, 11, 11, 12, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 19, 21,
22, 23, 24, 26, 27, 29, 30, 32, 33, 35, 37, 39, 41, 43, 45, 47,
49, 51, 53, 56, 58, 60, 63, 65, 68, 70, 73, 75, 78, 81, 83, 86,
89, 92, 94, 97, 100,103,106,108,111,114,117,120,123,126,129,130};
I intend to use this as a Low Frequency Oscillator (0.5Hz - 10Hz) and it would be neat to be able to control the sine wave frequency with one of the analog inputs. Would I need to lower the interrupt frequency in order to give the chip time to read the analog input every cycle?
Thank you for any help!