[sdiy] Wavetable Design Update
ASSI
Stromeko at nexgo.de
Sat Feb 5 12:16:29 CET 2011
On Wednesday 02 February 2011, Matthew Smith wrote:
> My problem is that given a frequency f, I have to work out timer
> configuration values which are:
>
> * The number to count up to
> * The prescaler value, if required.
The prescaler in the ATTiny simply acts as a (binary) exponent on the count
number. That means the best frequency resolution will always be achieved by
chosing the lowest prescaler and highest count value possible. Assuming you
use CTC mode, the datasheet says:
(1+OCR1A) = f_clk / 2*N*f_out
You only have five possible values for N (1, 8, 64, 256, 1024), all powers
of 2, and that means you can just shift right by (0, 3, 6, 8, 10) bits after
figuring the ratio f_clk/2*f_out, depending on whether the result fits into
16bit or not. So your calculation in the most general case goes like this:
ratio32 = (f_clk>>1)/f_out; // to 32bit HMSB:HLSB::LMSB:LLSB
if (HMSB != 0) {
N = 1024;
ratio24 >>= 10; // if the value is still too large, tough shit
} else {
if (HLSB < 0) { // 8th bit set?
N = 256;
ratio24 >>= 8; // that's a copy LMSB->LLSB, HLSB->LMSB
}
if (HLSB == 0) // should be able to get that from the same TST
N = 1;
else {
if (HLSB & 0x38) {
ratio >>= 6;
N = 6;
} else {
ratio >>= 3;
N = 3;
}
}
OCR1A = --ratio16;
That said, for the frequency range in question you'll end up with counts
from 4778 to 3, so you can skip that funky shifting completely since the
ratio will always fit into 16bit with room to spare when you just don't use
the pre-scaler at all (N==1). Conversely this means that the resolution
isn't all that great at the higher octaves, the input frequency is just too
low for that. You've got 100ns period resolution, but let's calculate how
much you really need - highest frequency 10MHz/3, desired resolution in
musical pitch is 10cent:
: (120_ / )
: f' = ( \/ 2 ) * f_max
: ( 120 / )
: t_res = T' - T = 1/f_max - 1/f' = 1/f * (1 - \/ 1/2 )
...and that calculates to 1.75ns for the period resolution or 1140MHz that
would be required at the input of the counter. With the actual 20MHz input
you reach the desired pitch resolution 6 octaves from the top. So you will
have to do some fractional counting by switching between two (or more)
counter values to extend the useful range of frequencies to at least the
middle octaves. I don't know how fast the counter interrupts are on the
ATTiny, but it doesn't look too good for getting to the full MIDI range.
Achim.
--
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+
Factory and User Sound Singles for Waldorf rackAttack:
http://Synth.Stromeko.net/Downloads.html#WaldorfSounds
More information about the Synth-diy
mailing list