EmbDev.net

Forum: FPGA, VHDL & Verilog Verilog : postive Edge Trigger


Author: Saraswathy S. (saras015)
Posted on:

Rate this post
0 useful
not useful
I am generating Trigger Signal and counter data in single Verilog code.

Logic (verilog): 1. Generating Trigger Signal for 500ns -Output Port 
trig_out
                 2. Generating counter data (counter_result)
                 3. collecting and Storing counter data (Final_Value) at 
every Positive edge of trigger signal.

detect Port- Positive Edge detector of trigger signal.

Please debug my code. Because i am unable to collect the edge value 
(counter value) of Trigger signal.
module Counter_Design
#(parameter N = 25)
(
    input clk,
    input rst,
    input enable,
  output reg[31:0] Final_value,
    output reg trig_out
    );
reg [31:0]counter;
reg [31:0] counter_result;
reg temp1;
wire detect;
wire temp2;

always @(posedge clk or posedge rst) begin
  if (rst) begin
    // reset
    counter <= 0;
  end
  else if (counter == N-1) begin
    counter <= 0;
  end
  else if (enable==1'b1)
  begin
    counter <= counter + 1;
  end
end
 
always @(posedge clk or posedge rst) 
begin
  if (rst) begin
    // reset
    trig_out <= 0;
  end
  else if (counter == N-1) begin
    trig_out <= !trig_out;
  end
end
always @(posedge clk or posedge rst)  
begin
  if (rst) 
  begin
    // reset
    trig_out <= 0;
  end
    else begin
      temp1 <= trig_out;
     end
end
assign detect=trig_out & ~temp1;
 assign temp2=temp1&&clk;

always @(posedge temp2 or posedge rst)  
begin
  if (rst) 
  begin
     counter_result<=0;   
  end
    else begin
      counter_result<=counter_result+1;
     end
end
always @(posedge detect or posedge rst) 
 begin
    if(rst)
    begin
    Final_value<=0; 
    end
    else if (counter == N-1) begin
    Final_value<=counter_result;
    end
end
endmodule

Author: Duke Scarring (Guest)
Posted on:
Attached files:

Rate this post
0 useful
not useful
Saraswathy S. wrote:
> Please debug my code. Because i am unable to collect the edge value
> (counter value) of Trigger signal.

At first: Write a testbench, so that others can reproducible your 
problem:
module counter_tb;
    
    // testbench signals
    reg tb_clk;
    reg tb_rst;
    reg tb_enable;
    wire [31:0] tb_final_value;
    wire tb_trig_out;


    // design under test
    Counter_Design DUT (
        .clk            ( tb_clk),
        .rst            ( tb_rst),
        .enable         ( tb_enable),
        .Final_value    ( tb_final_value),
        .trig_out       ( tb_trig_out)
    );

    // stimuli
    initial
    begin
        tb_clk = 0;
        tb_rst = 1;
        tb_enable = 0;
        # 7 tb_rst = 0;
        # 55 tb_enable = 1;
        # 99 tb_enable = 0;
        # 135 tb_enable = 1;
        # 1000 $finish;
    end
    
    // clock generator
    always
        #5 tb_clk = ~tb_clk;

endmodule

I attached a slightly modified version of your code, which update 
Final_value when the counter is reseted. Maybe I didn't understand you 
correctly with you problem.

Duke

Author: Lothar M. (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Saraswathy S. wrote:
> Because i am unable to collect the edge value (counter value) of Trigger
> signal.
You are generating that value. So it should be very easy to do any 
processing there.

> assign temp2=temp1&&clk;
> always @(posedge temp2 or posedge rst)
It is no good design practice to use gated clocks. Absolutely not! A 
hint for the beginner: use only 1 clock in the whole FPGA and do all 
enabling inside the clock processing, somewhat like this:
always @(posedge clk or posedge rst)  
begin
  if (rst) 
  begin
     counter_result<=0;   
  end
    else begin
      // do the clock-enabling here
      if (clock_enable)
         counter_result<=counter_result+1;
      end 
     end
end

> always @(posedge detect
It is no good design practice to use any random input as a clock source. 
Treat that "detect" signal as a simple input and synchronize it to the 
OneAndOnly-FPGA-Clock. Then store it in two flipflops one after the 
other and compare the values of those two flipflops to detect an edge of 
that input signal. This is called synchronous design. And thats the only 
thing that works reliably in the real world...

Saraswathy S. wrote:
> Please debug my code.
Whats the problem with it?

> Because i am unable to collect the edge value of Trigger signal.
How did you find that out? What do you expect of that code? And what do 
you get instead?
Where in your description is a repetition of the Trigger Signal? I can't 
find no one.

Saraswathy S. wrote:
> 1. Generating Trigger Signal for 500ns -Output Port trig_out
So only one Trigger pulse must be generated.
> 2. Generating counter data (counter_result)
From what? When should the counter stop?
> 3. collecting and Storing counter data (Final_Value) at every Positive
> edge of trigger signal.
According to 1. there is only 1 Trigger Signal active once for 500ns.
Shoul the trigger start later on once more? After a certain time? Or a 
certain event?

Can you draw a sketch of that timing of those few signals and the 
desired reaction?

: Edited by Moderator

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig