[time-nuts] frequency generation algorithm.
Hal Murray
hmurray at megapathdsl.net
Tue Jun 28 16:32:24 EDT 2016
dan at irtelemetrics.com said:
> I'm looking for a quick and dirty algorithm or method to generate a pulse
> train at arbitrary frequencies based on a fixed clock source. This will be
> run as code in a timer ISR in a microcontroller, so some
Google for DDS.
The hardware implementation is simple. Consider a register, as wide as you
like. Think of that register as a fraction. An overflow would be a whole
integer 1.
Each cycle, you add a number to that register. The output signal is the top
bit.
If your register is N bits wide and you add K each cycle:
Fout = Fin * K/2^N
K = 2^N * Fout / Fin
The output signal will have spurs. (unless there is only 1 bit turned on in K)
The output frequency will be rock solid, but it may not be the one you want. You can get closer by making the N bigger (register wider) and filling in more low order bits of K.
If you have a graphics background, it's the same math as Bresenham's algorithm for drawing slanted lines.
--
These are my opinions. I hate spam.
More information about the time-nuts
mailing list