You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Verilog异步时钟设计:双异步输入时钟周期比较模块设计问询

Verilog异步时钟周期比较模块设计

模块需求

输入信号

  • 高速系统时钟 system_clk:频率需远高于P、Q时钟频率,保证采样精度
  • 异步时钟输入 P:50%占空比,与Q频率接近且存在相位差
  • 异步时钟输入 Q:50%占空比,与P频率接近且存在相位差
  • 额外补充低电平复位信号 rst_n:数字逻辑必备控制信号,不需要可自行移除

输出信号

  • PeqQ:高电平表示P与Q周期相等
  • PleQ:高电平表示P的周期小于Q的周期
  • PgeQ:高电平表示P的周期大于Q的周期

设计思路

  1. 异步输入同步:采用两级寄存器将异步输入的P、Q同步到system_clk时钟域,避免亚稳态问题
  2. 边沿检测:对同步后的P、Q信号做上升沿检测,作为周期计数的重置触发信号
  3. 周期测量:用高速系统时钟分别计数P、Q两个上升沿之间的时钟数,得到两个时钟的周期量化值
  4. 比较输出:对两个周期量化值做对比,可配置±1的误差容忍范围,抵消相位差导致的单次采样偏差,最终生成对应输出信号

完整Verilog实现代码

module period_compare (
    input        system_clk,
    input        rst_n,
    input        P,
    input        Q,
    output reg   PeqQ,
    output reg   PleQ,
    output reg   PgeQ
);

// 异步输入两级同步逻辑
reg [1:0] P_sync;
reg [1:0] Q_sync;
always @(posedge system_clk or negedge rst_n) begin
    if(!rst_n) begin
        P_sync <= 2'b00;
        Q_sync <= 2'b00;
    end else begin
        P_sync <= {P_sync[0], P};
        Q_sync <= {Q_sync[0], Q};
    end
end

// 上升沿检测逻辑
wire P_posedge = P_sync[1] & ~P_sync[0];
wire Q_posedge = Q_sync[1] & ~Q_sync[0];

// 周期计数与锁存寄存器
reg [15:0] P_cnt;
reg [15:0] Q_cnt;
reg [15:0] P_period;
reg [15:0] Q_period;

// P时钟周期测量逻辑
always @(posedge system_clk or negedge rst_n) begin
    if(!rst_n) begin
        P_cnt <= 16'd0;
        P_period <= 16'd0;
    end else begin
        if(P_posedge) begin
            P_period <= P_cnt;
            P_cnt <= 16'd1;
        end else begin
            P_cnt <= P_cnt + 1'b1;
        end
    end
end

// Q时钟周期测量逻辑
always @(posedge system_clk or negedge rst_n) begin
    if(!rst_n) begin
        Q_cnt <= 16'd0;
        Q_period <= 16'd0;
    end else begin
        if(Q_posedge) begin
            Q_period <= Q_cnt;
            Q_cnt <= 16'd1;
        end else begin
            Q_cnt <= Q_cnt + 1'b1;
        end
    end
end

// 周期比较与输出逻辑
reg [1:0] valid_flag;
always @(posedge system_clk or negedge rst_n) begin
    if(!rst_n) begin
        valid_flag <= 2'b00;
        PeqQ <= 1'b0;
        PleQ <= 1'b0;
        PgeQ <= 1'b0;
    end else begin
        if(P_posedge) valid_flag[0] <= 1'b1;
        if(Q_posedge) valid_flag[1] <= 1'b1;
        
        if(valid_flag == 2'b11) begin
            // 允许±1计数误差,不需要可自行删除误差判断条件
            if((P_period == Q_period) || (P_period == Q_period + 1'b1) || (Q_period == P_period + 1'b1)) begin
                PeqQ <= 1'b1;
                PleQ <= 1'b0;
                PgeQ <= 1'b0;
            end else if(P_period < Q_period) begin
                PeqQ <= 1'b0;
                PleQ <= 1'b1;
                PgeQ <= 1'b0;
            end else begin
                PeqQ <= 1'b0;
                PleQ <= 1'b0;
                PgeQ <= 1'b1;
            end
        end
    end
end

endmodule

使用注意事项

  • system_clk频率建议为P、Q最高频率的10倍以上,采样频率越高,周期测量精度越高
  • 如果P、Q频率差极小,可以修改逻辑为连续采样多次周期取平均值后再比较,降低单次采样误差
  • 计数器位宽可根据P、Q的最大周期值调整,避免计数溢出

内容的提问来源于stack exchange,提问作者Michael Wasef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 21:27:03