【问题标题】:Verilog code that copies an input square wave signal to an output signal?将输入方波信号复制到输出信号的 Verilog 代码?
【发布时间】:2022-01-13 01:13:45
【问题描述】:

我想知道是否有人可以帮助我?我不知道如何表达这个问题,但我基本上是在尝试编写一个程序,从方波输入信号生成方波输出信号,匹配输入信号的占空比和频率。基本上,输出只是复制输入。用图形来总结我所说的,这是我制作的一张图片:

Link to diagram

这不是我的最终目标,但足以让我继续前进。我很难弄清楚如何使用输入。我有一个信号发生器产生输入方波信号,并将其发送到输入引脚。我尝试用数学方法计算占空比,然后只是尝试将输出分配给一个设置为等于时钟信号每个上升沿上的输入的 reg,但它不起作用。

这是我的代码。它具有生成 1 Hz 信号的额外功能,但这只是从早期学习如何创建 pwm 中得出的。您可以忽略“pwm_reg”和“pwm”输出。 “pwm2”输出旨在复制“apwm”输入:

`timescale 1ns / 1ps
module duty_cycle_gen(
    input clk,
    input rst_n,
    input apwm,
    output pwm,
    output pwm2
);
    // Input clock is 250MHz
    localparam CLOCK_FREQUENCY = 250000000;
    // Counter for toggling of clock
    integer counter = 0;
    reg pwm_reg = 0;
    assign pwm = pwm_reg;
    reg apwm_val;
always @(posedge clk) begin
        if (!rst_n) begin
            counter <= 8'h00;
            pwm_reg <= 1'b0;
        end
        else begin 
            apwm_val <= apwm;
            // If counter is zero, toggle pwm_reg
            if (counter == 8'h00) begin
                pwm_reg <= ~pwm_reg;
                // Generate 1Hz Frequency
                counter <= CLOCK_FREQUENCY/2 - 1;  
                
            end 
            // Else count down
            else 
                counter <= counter - 1; 
            end
            $display("counter : %d", counter);
            
        end
assign pwm2 = apwm_val;
endmodule

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: verilog fpga soc


【解决方案1】:

这是一个带有测试台的简单示例。 (我喜欢在分配时使用一个小的延迟,#1,以帮助捕获因果关系以进行调试):

module example(
    input wire clk,
    input wire in,
    output reg out);

    always @(posedge clk)
      begin
        out <= #1 in;
      end

endmodule // example

module example_test();
    reg chip__clk;
    reg chip__in;
    wire chip__out;
    reg [10:0] count;

    example ex(
        chip__clk,
        chip__in,
        chip__out);

    initial
      begin
        $dumpvars();
        count <= #1 0;
      end

    always @(count)
      begin
        count <= #1 (count + 1);
        if (count == 1000)
          begin
            $display("RESULT=PASS:0 @ done");
            $finish_and_return(0);
          end
        if ((count == 60) & (chip__out != 1))
          begin
            $display("RESULT=FAIL:1 @ chip.out not raised");
            $finish_and_return(1);
          end
        if ((count == 30) & (chip__out != 0))
          begin
            $display("RESULT=FAIL:1 @ chip.out not lowered");
            $finish_and_return(1);
          end
        chip__in <= #1 count[5];
        chip__clk <= #1 count[1];
      end

endmodule // example_test

它的工作原理是将in 信号视为在更高频率clk 的时间尺度上可以被视为恒定的东西。

如果您的in 时钟是一个可能有噪声的外部信号,具有较小的延迟延迟,您可以尝试使用以高频clk 运行的小型fifo 来稳定它:

module example(
    input wire clk,
    input wire in,
    output reg out);

    reg [1:0] buffer;

    always @(posedge clk)
      begin
        out <= #1 buffer[1];
        buffer[1] <= #1 buffer[0];
        buffer[0] <= #1 in;
      end

endmodule // example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2021-12-27
    相关资源
    最近更新 更多