N
news.optonline.net
Guest
Given the code below for a parameterized PWM generator, how can I eliminate
having the user specify the counter width. In other words, what clever math
could I use to automatically compute the required counter width
(PwmCountWidth) and make it a localparam?
Thanks...
module VRefPWM #(
parameter ClockFreq = 8000000, // Hz
PwmFreq = 62500, // Hz
PwmCountWidth = 7 //Output width (needs to be wide enough to hold
MaxCount-1(localparam)
)
(
input CLOCK,
input RESET,
input [PwmCountWidth-1:0] PWM_SET,
output reg [PwmCountWidth-1:0] PWM_COUNT,
output ROLL_OVER,
output VREF_PWM
);
localparam MaxCount = ClockFreq/PwmFreq;
// Pwm Counter
always @(posedge CLOCK, posedge RESET)
if (RESET)
PWM_COUNT <= {PwmCountWidth{1'b0}};
else begin
PWM_COUNT <= PWM_COUNT + 1;
if (PWM_COUNT == MaxCount-1)
PWM_COUNT <= {PwmCountWidth{1'b0}};
end
// Generate cycle start for current controller
assign ROLL_OVER = (PWM_COUNT == MaxCount-1);
// Generate Vref PWM
assign VREF_PWM = (PWM_COUNT <= PWM_SET);
endmodule
having the user specify the counter width. In other words, what clever math
could I use to automatically compute the required counter width
(PwmCountWidth) and make it a localparam?
Thanks...
module VRefPWM #(
parameter ClockFreq = 8000000, // Hz
PwmFreq = 62500, // Hz
PwmCountWidth = 7 //Output width (needs to be wide enough to hold
MaxCount-1(localparam)
)
(
input CLOCK,
input RESET,
input [PwmCountWidth-1:0] PWM_SET,
output reg [PwmCountWidth-1:0] PWM_COUNT,
output ROLL_OVER,
output VREF_PWM
);
localparam MaxCount = ClockFreq/PwmFreq;
// Pwm Counter
always @(posedge CLOCK, posedge RESET)
if (RESET)
PWM_COUNT <= {PwmCountWidth{1'b0}};
else begin
PWM_COUNT <= PWM_COUNT + 1;
if (PWM_COUNT == MaxCount-1)
PWM_COUNT <= {PwmCountWidth{1'b0}};
end
// Generate cycle start for current controller
assign ROLL_OVER = (PWM_COUNT == MaxCount-1);
// Generate Vref PWM
assign VREF_PWM = (PWM_COUNT <= PWM_SET);
endmodule