71 lines
2.1 KiB
Verilog
71 lines
2.1 KiB
Verilog
`timescale 1ns / 1ps
|
||
//////////////////////////////////////////////////////////////////////////////////
|
||
// Company:
|
||
// Engineer:
|
||
//
|
||
// Create Date: 2026/03/22 18:53:43
|
||
// Design Name:
|
||
// Module Name: pulse_freq_10ms
|
||
// Project Name:
|
||
// Target Devices:
|
||
// Tool Versions:
|
||
// Description:
|
||
//
|
||
// Dependencies:
|
||
//
|
||
// Revision:
|
||
// Revision 0.01 - File Created
|
||
// Additional Comments:
|
||
//
|
||
//////////////////////////////////////////////////////////////////////////////////
|
||
|
||
|
||
`timescale 1ns / 1ps
|
||
|
||
module pulse_freq_10ms #(
|
||
parameter CLK_FREQ = 50_000_000, // 系统时钟频率(Hz),默认50MHz
|
||
parameter WINDOW_MS = 10 // 测量时间窗口(ms),默认10ms
|
||
) (
|
||
input clk, // 系统时钟
|
||
input rst_n, // 异步复位,低有效
|
||
input vin, // 输入方波
|
||
output reg [19:0] freq, // 时间窗口内脉冲计数(如果为1302,就是130.2k)
|
||
output reg valid // 测量完成有效脉冲
|
||
);
|
||
|
||
// 计算窗口计数最大值
|
||
localparam WINDOW_CNT = (CLK_FREQ / 1000) * WINDOW_MS - 1;
|
||
|
||
reg [31:0] cnt_window; // 窗口计时器(使用32位以防大数值)
|
||
reg [19:0] pulse_cnt; // 脉冲计数器
|
||
reg vin_sync1, vin_sync2;
|
||
wire vin_rise;
|
||
|
||
// 边沿检测
|
||
always @(posedge clk) begin
|
||
vin_sync1 <= vin;
|
||
vin_sync2 <= vin_sync1;
|
||
end
|
||
assign vin_rise = vin_sync1 & ~vin_sync2;
|
||
|
||
// 核心逻辑:窗口计数 + 脉冲计数 + 锁存输出
|
||
always @(posedge clk or negedge rst_n) begin
|
||
if (!rst_n) begin
|
||
cnt_window <= 0;
|
||
pulse_cnt <= 0;
|
||
valid <= 0;
|
||
end else begin
|
||
valid <= 0; // 默认无效
|
||
if (cnt_window == WINDOW_CNT) begin // 窗口时间到
|
||
freq <= pulse_cnt; // 输出计数值
|
||
valid <= 1;
|
||
cnt_window <= 0;
|
||
pulse_cnt <= 0;
|
||
end else begin
|
||
cnt_window <= cnt_window + 1;
|
||
if (vin_rise) pulse_cnt <= pulse_cnt + 1;
|
||
end
|
||
end
|
||
end
|
||
|
||
endmodule |