Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating pulses of different width

I have written following code which produces pulse of different width.I want the code to produce a single pulse according to select line. If select line is

00 pulse width = 1 us , 01 pulse width = 10 us . . 11 pulse width = 1000 us

The input clock is of 10 Mhz. But according to code I am getting continuous pulse if I don't provide any other value of selection line.How can I achieve only one pulse?

    module pulse(input wire [1:0] sel , //selection lines s1 s0
        input clk,
        input rst_n,
        output reg flag,    //for checking conditions
        output reg [13:0] Q,    // output of 14 bit counter
        output reg pulse,   //output pulse
        output reg count);  //also for checking conditions

wire flag_d , count_d;

assign flag_d = ( (sel == 2'b00 | sel == 2'b01 | sel == 2'b10 | sel == 2'b11) && count == 1'b0)? 1'b1 : flag;
assign count_d = ( (sel == 2'b00 | sel == 2'b01 | sel == 2'b10 | sel == 2'b11) && count == 1'b0)? 1'b1 : count;

always @(posedge clk , negedge rst_n)
begin
    if(!rst_n)
    begin
        Q <= 14'h0;
        count <= 1'b0;
        pulse <= 1'b0;
        flag <= 1'b0;
    end
    else
    begin

        flag <= flag_d;
        count <= count_d;

        if(flag)
        begin
            case(sel)
            2'b00: Q <= 14'd11;//count from 11 to 1
            2'b01: Q <= 14'd101;//count from 101 to 1
            2'b10: Q <= 14'd1001;//count from 1001 to 1
            2'b11: Q <= 14'd10001;//count from 10001 to 1
            default: Q <= 14'd0;    
            endcase

            flag <= 1'b0;
        end
        else
        begin
            if(Q != 14'h1 && Q != 14'h0)
            begin
                Q <= Q - 14'h1;
                pulse  <= 1'b1;
            end
            else
            begin
                pulse <= 1'b0;
                count <= 1'b0;
            end
        end  
    end
end 
endmodule

Is this code in a good coding style considering the synthesis and hardware of the circuit? if not than what changes I should apply?..

like image 606
Ishita Shah Avatar asked Nov 18 '25 06:11

Ishita Shah


1 Answers

I couldn't figure out the point of flag_d and count_d. Also ( (sel == 2'b00 | sel == 2'b01 | sel == 2'b10 | sel == 2'b11) && count == 1'b0) simplifies to (count == 1'b0). sel should not be Xs or Zs.

I think you want something more like the following:

reg [13:0] next_Q;
always @* begin
  if (Q==0) begin
    case(sel)
    2'b00 : next_Q = 14'd10;
    2'b01 : next_Q = 14'd100;
    2'b10 : next_Q = 14'd1000;
    2'b11 : next_Q = 14'd10000;
    endcase
  end
  else begin
    next_Q = Q - 1;
  end
end
always @(posedge clk, negedge rst_n) begin
  if (!rst_n) begin
    pulse <= 1'b0;
    Q <= 14'd0;
  end
  else begin
    // if (Q==0) pulse <= !pulse; // high and low pulse will have equal if sel is constant
    pulse <= (Q!=0); // or high pulse based on sel, low is one clk
    Q <= next_Q;
  end
end

working example: http://www.edaplayground.com/x/GRv

like image 118
Greg Avatar answered Nov 21 '25 00:11

Greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!