EmbDev.net

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


von Saraswathy S. (saras015)


Rate this post
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.
1
module Counter_Design
2
#(parameter N = 25)
3
(
4
    input clk,
5
    input rst,
6
    input enable,
7
  output reg[31:0] Final_value,
8
    output reg trig_out
9
    );
10
reg [31:0]counter;
11
reg [31:0] counter_result;
12
reg temp1;
13
wire detect;
14
wire temp2;
15
16
always @(posedge clk or posedge rst) begin
17
  if (rst) begin
18
    // reset
19
    counter <= 0;
20
  end
21
  else if (counter == N-1) begin
22
    counter <= 0;
23
  end
24
  else if (enable==1'b1)
25
  begin
26
    counter <= counter + 1;
27
  end
28
end
29
 
30
always @(posedge clk or posedge rst) 
31
begin
32
  if (rst) begin
33
    // reset
34
    trig_out <= 0;
35
  end
36
  else if (counter == N-1) begin
37
    trig_out <= !trig_out;
38
  end
39
end
40
always @(posedge clk or posedge rst)  
41
begin
42
  if (rst) 
43
  begin
44
    // reset
45
    trig_out <= 0;
46
  end
47
    else begin
48
      temp1 <= trig_out;
49
     end
50
end
51
assign detect=trig_out & ~temp1;
52
 assign temp2=temp1&&clk;
53
54
always @(posedge temp2 or posedge rst)  
55
begin
56
  if (rst) 
57
  begin
58
     counter_result<=0;   
59
  end
60
    else begin
61
      counter_result<=counter_result+1;
62
     end
63
end
64
always @(posedge detect or posedge rst) 
65
 begin
66
    if(rst)
67
    begin
68
    Final_value<=0; 
69
    end
70
    else if (counter == N-1) begin
71
    Final_value<=counter_result;
72
    end
73
end
74
endmodule

von Duke Scarring (Guest)


Attached files:

Rate this post
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:
1
module counter_tb;
2
    
3
    // testbench signals
4
    reg tb_clk;
5
    reg tb_rst;
6
    reg tb_enable;
7
    wire [31:0] tb_final_value;
8
    wire tb_trig_out;
9
10
11
    // design under test
12
    Counter_Design DUT (
13
        .clk            ( tb_clk),
14
        .rst            ( tb_rst),
15
        .enable         ( tb_enable),
16
        .Final_value    ( tb_final_value),
17
        .trig_out       ( tb_trig_out)
18
    );
19
20
    // stimuli
21
    initial
22
    begin
23
        tb_clk = 0;
24
        tb_rst = 1;
25
        tb_enable = 0;
26
        # 7 tb_rst = 0;
27
        # 55 tb_enable = 1;
28
        # 99 tb_enable = 0;
29
        # 135 tb_enable = 1;
30
        # 1000 $finish;
31
    end
32
    
33
    // clock generator
34
    always
35
        #5 tb_clk = ~tb_clk;
36
37
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

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
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:
1
always @(posedge clk or posedge rst)  
2
begin
3
  if (rst) 
4
  begin
5
     counter_result<=0;   
6
  end
7
    else begin
8
      // do the clock-enabling here
9
      if (clock_enable)
10
         counter_result<=counter_result+1;
11
      end 
12
     end
13
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
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.