G
gabor
Guest
I seem to remember a number of threads about generating a fractional
frequency
for such things as baud rate. One approach is to use DDS, usually
with a
power of two divisor. For large numbers of bits you can get good
frequency
resolution, but you always end up with cycle jitter when your output
frequency
is not a power of two division of the input frequency. Also any
fraction not
reduceable to n over a power of two will not come out exact.
This code uses DDS with a variable divisor to allow jitter free output
for integer division of the input frequency, and exact fractional
frequencies with the usual induced cycle jitter for other
frequencies. The output is relatively square and must be less than
or equal to 1/2 the input frequency. If you want to use the code
to generate a clock enable instead of a square wave, the
output rate can go up to the input frequency as noted below.
module freq_synth
(
clk,
clr,
m,
d,
q
);
input clk; // Frequency reference in
input clr; // Asynchronous reset
input [15:0] m; // Frequency multiplier
input [15:0] d; // Frequency divider
output q; // synthesized clock out
// This module takes the input reference frequency and
// generates an output frequency of m / 2d times that
// frequency. d must be greater than or equal to m.
// For 200 MHz input, the maximum output frequency is
// 100 MHz (d == m).
reg q;
reg [17:0] a, b, diff; // counters and comparators (2 extra bits)
always @*
diff = b - a; // keep track of difference
always @ (posedge clk or posedge clr)
if (clr)
begin
q <= 0;
a <= 0;
b <= 0;
end
else
begin
a <= a + m; // a counts up by multiplier (always)
if (diff[17]) // if a gets ahead of b
begin
// count up by divider value and toggle the output
b <= b + d;
// Instead of complementing q, for a clock enable you could
// set q to 1 here and clear it otherwise:
q <= !q;
end
end
endmodule
frequency
for such things as baud rate. One approach is to use DDS, usually
with a
power of two divisor. For large numbers of bits you can get good
frequency
resolution, but you always end up with cycle jitter when your output
frequency
is not a power of two division of the input frequency. Also any
fraction not
reduceable to n over a power of two will not come out exact.
This code uses DDS with a variable divisor to allow jitter free output
for integer division of the input frequency, and exact fractional
frequencies with the usual induced cycle jitter for other
frequencies. The output is relatively square and must be less than
or equal to 1/2 the input frequency. If you want to use the code
to generate a clock enable instead of a square wave, the
output rate can go up to the input frequency as noted below.
module freq_synth
(
clk,
clr,
m,
d,
q
);
input clk; // Frequency reference in
input clr; // Asynchronous reset
input [15:0] m; // Frequency multiplier
input [15:0] d; // Frequency divider
output q; // synthesized clock out
// This module takes the input reference frequency and
// generates an output frequency of m / 2d times that
// frequency. d must be greater than or equal to m.
// For 200 MHz input, the maximum output frequency is
// 100 MHz (d == m).
reg q;
reg [17:0] a, b, diff; // counters and comparators (2 extra bits)
always @*
diff = b - a; // keep track of difference
always @ (posedge clk or posedge clr)
if (clr)
begin
q <= 0;
a <= 0;
b <= 0;
end
else
begin
a <= a + m; // a counts up by multiplier (always)
if (diff[17]) // if a gets ahead of b
begin
// count up by divider value and toggle the output
b <= b + d;
// Instead of complementing q, for a clock enable you could
// set q to 1 here and clear it otherwise:
q <= !q;
end
end
endmodule