forked from p_lusterinc_xsync/xsync_fpge
5 changed files with 250 additions and 194 deletions
-
60led_test.pds
-
16source/src/input/ttl_input.v
-
148source/src/output/ttl_output.v
-
115source/src/zutils/zsimple_pll.v
-
105source/src/zutils/zutils_freq_detector_v2.v
@ -0,0 +1,105 @@ |
|||||
|
// |
||||
|
// @功能: |
||||
|
// 1. 滤波(add later) |
||||
|
// 2. 频率探测 |
||||
|
// 3. 输出灯光控制 |
||||
|
// |
||||
|
module zutils_freq_detector_v2 |
||||
|
( |
||||
|
input clk, //! 时钟输入 |
||||
|
input rst_n, //! 复位输入 |
||||
|
input pluse_input, //! 输入信号1 |
||||
|
input wire [31:0] freq_detect_bias, //! 频率偏差计数 |
||||
|
output reg [31:0]pluse_width_cnt, //! 输出捕获到的脉冲宽度 |
||||
|
output wire pluse_width_cnt_lock //! 输出捕获到的脉冲宽度锁定信号 |
||||
|
); |
||||
|
|
||||
|
|
||||
|
reg in_signal_last; |
||||
|
reg in_signal_rising_edge; //! 上升沿 |
||||
|
reg [31:0] state; //! 频率捕获状态 |
||||
|
reg [31:0] freq_detect; //! 探测到频率cache1 |
||||
|
reg [31:0] freq_detect_cnt;//! 实时频率探测计数 |
||||
|
|
||||
|
|
||||
|
//!in_signal_last 捕获 |
||||
|
always @(posedge clk or negedge rst_n) begin |
||||
|
if (!rst_n) begin |
||||
|
in_signal_last <= 0; |
||||
|
end |
||||
|
else begin |
||||
|
in_signal_last <= pluse_input; |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
//!边沿捕获 |
||||
|
always @(posedge clk or negedge rst_n) begin |
||||
|
if (!rst_n) begin |
||||
|
in_signal_rising_edge <= 0; |
||||
|
end |
||||
|
else begin |
||||
|
if (in_signal_last == 0 && pluse_input == 1) begin |
||||
|
in_signal_rising_edge <= 1; |
||||
|
end |
||||
|
else if (in_signal_last == 1 && pluse_input == 0) begin |
||||
|
in_signal_rising_edge <= 0; |
||||
|
end |
||||
|
else begin |
||||
|
in_signal_rising_edge <= 0; |
||||
|
end |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
|
||||
|
|
||||
|
assign freq_detect_stable = (freq_detect <= freq_detect_cnt + freq_detect_bias + 1 && freq_detect >= freq_detect_cnt - freq_detect_bias - 1); |
||||
|
|
||||
|
always @(posedge clk or negedge rst_n) begin |
||||
|
if (!rst_n) begin |
||||
|
state <= 0; |
||||
|
freq_detect <=0; |
||||
|
pluse_width_cnt <= 32'hffff_ffff; |
||||
|
end |
||||
|
else begin |
||||
|
case (state) |
||||
|
0: begin |
||||
|
// 频率探测中 |
||||
|
if(in_signal_rising_edge) begin |
||||
|
freq_detect <= freq_detect_cnt; |
||||
|
freq_detect_cnt <= 0; |
||||
|
if(freq_detect_stable) begin |
||||
|
state <= 1; |
||||
|
pluse_width_cnt <= freq_detect; |
||||
|
end |
||||
|
else begin |
||||
|
pluse_width_cnt <= 32'hffff_ffff; |
||||
|
end |
||||
|
end |
||||
|
else begin |
||||
|
freq_detect_cnt <= freq_detect_cnt + 1; |
||||
|
end |
||||
|
end |
||||
|
1: begin |
||||
|
// 判断频率是否发生变化,如果频率发生变化,则重新探测 |
||||
|
if(in_signal_rising_edge || freq_detect_cnt> freq_detect+ 1) begin |
||||
|
freq_detect_cnt <= 0; |
||||
|
if(!freq_detect_stable) begin |
||||
|
state <= 0; |
||||
|
pluse_width_cnt <= 32'hffff_ffff; |
||||
|
end |
||||
|
end |
||||
|
else begin |
||||
|
freq_detect_cnt <= freq_detect_cnt + 1; |
||||
|
end |
||||
|
|
||||
|
end |
||||
|
default: begin |
||||
|
state <= 0; |
||||
|
end |
||||
|
endcase |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
assign pluse_width_cnt_lock = (state == 1); |
||||
|
|
||||
|
endmodule |
Write
Preview
Loading…
Cancel
Save
Reference in new issue