Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tick Counter Verilog

I was wondering how can i write a verilog program for a tick counter. When the fast input is low, the output tick is high for one cycle every 150 ms (every 7500000 cycles) The clk period is 20ns. If the fast input is high, tick should go high for one cycle every other clock cycle.

I'm thinking that I should count the clk cycles and use the count to output tick as high when the number of cycles are met but I can't seem to get it to work.

heres my code:

module tick_counter(
  input  clk,
  input  reset,
  input  fast,
  output reg tick
);

reg count;

always @(posedge clk) begin
  count <= count + 1;
  if((fast == 1)&&(count == 2)) begin
    tick <= 1;
  end
  else if(fast == 0)&&(count == 7500000)) begin
    tick <= 1;
  end
end
endmodule
like image 711
intensified Avatar asked Oct 06 '22 17:10

intensified


1 Answers

Your counter is only 1 bit wide, you have not included a reset, You also do not zero the counter when required. The ==2 would just be a phase shift of == 7500000. Try :

module tick_counter(
  input  clk,
  input  reset,
  input  fast,
  output reg tick
);

reg [22:0] count;

always @(posedge clk or negedge reset) begin
  if (~reset) begin
    count <= 'd0;
    tick  <=   0;
  end
  else begin
    if((fast == 1)&&(count == 2)) begin
      tick  <= 1;
      count <= 'd0;
    end
    else if(fast == 0)&&(count == 7500000)) begin
      tick  <= 1;
      count <= 'd0;
    end
    else begin
      tick  <= 0;
      count <= count + 1;
    end
  end
end
endmodule

Or something like the following might synthesise smaller:

reg  [22:0] count;

wire [22:0] comp = (fast) ? 23'd2: 23'd7500000 ;
wire        done = count >= comp               ;

always @(posedge clk or negedge reset) begin
  if (~reset) begin
    count <= 'd0;
    tick  <=   0;
  end
  else begin
    if(done) begin
      tick  <= 1;
      count <= 'd0;
    end
    else begin
      tick  <= 0;
      count <= count + 1;
    end
  end
end
like image 96
Morgan Avatar answered Oct 13 '22 07:10

Morgan