Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

verilog PWM, counter not incrementing

Status
Not open for further replies.

bobobano

Newbie level 5
Joined
Mar 6, 2008
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,361
My digital circuitry class was assigned to write the code in verilog for a PWM. Unfortunately I can't even get my code to work.

Code:
module PWM (clk,duty,period,B);

input clk,duty,period;
output reg B;
reg [8:0]count=0; //8 bit counter

always @ (negedge clk)
 begin
  if (count<duty)  assign B=1;
  else assign B=0;
  if (count>=period) count = 0; //reset count at end of period
  count = count + 1; //increment counter
  $display("%g",count); //display the current count
 end
endmodule

module testbench();

wire A;
reg clk=0;

always #1 if($time==20) $finish; //stop program after 20 cycles
always #1 clk=!clk; //alternate clock pulse


initial begin
  $monitor ("%b - %b ---- %g",A,clk,$time);
 end

PWM inst (clk,6,10,A); //duty cycle = 60%, period = 10 cycles

endmodule

From what I understand there isn't any reason the counter should not be incrementing, yet it increments to 1, and then stays there.

I am writing using Icarus for windows version 0.9.3 and it gives me no errors on compiling, even my lab teacher doesn't know why it's not working.

Help me Obiwan-Kenobi, you're my only hope.
 

Hi

I'm a VHDL guy but i have managed to fix the counting, simulation seems to be working but your duty cycle was not correct, so i have also added an else statement because you were doing count=0 and immediately count=count+1 in the same clock cycle;

Code:
module PWM (clk,duty,period,B);

input clk;
input [8:0] duty,period;
output reg B;
reg [8:0] count=0; //8 bit counter

always @ (negedge clk)
 begin
  if (count<duty)  assign B=1;
  else assign B=0;
  if (count>=period) count = 0; //reset count at end of period
  else count = count + 1; //increment counter
  $display("%g",count); //display the current count
 end
endmodule

module testbench();

wire A;
reg clk=0;

always #1 if($time==20) $finish; //stop program after 20 cycles
always #1 clk=!clk; //alternate clock pulse


initial begin
  $monitor ("%b - %b ---- %g",A,clk,$time);
 end

PWM inst (clk,6,10,A); //duty cycle = 60%, period = 10 cycles

endmodule

Alex
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top