在線檢測模塊 有時鐘和脈沖兩種模式
module online_detect #(
parameter SYS_CLK_period = 100 , //系統時鐘周期
parameter DETECT_period = 100 , //檢測周期
parameter MODE_select = 1 //模式選擇
)
(
input i_sys_clk , //輸入系統時鐘
input i_sys_rst , //輸入系統復位
input i_det_sig , //輸入被測信號
output o_det_state //輸出被檢測信號
);
reg oline_flag = 1'b0 ; //在線標志,如果在線,信號正常,反之不正常
reg [31:0] cnt_1pps = 32'd0 ; //1pps的計數器
reg div_1pps = 1'b0 ; //1pps脈沖
reg [1:0] mux_reg = 2'b0 ; //檢測信號被打兩拍之后的信號
wire mux_select_sig ; //模式選擇之后的信號
wire mux_sig_pose ; //最終被檢測信號的上升沿
wire[31:0] div_param ; //誤差允許范圍
reg [31:0] pulse_cnt = 32'd0 ; //脈沖計數器
reg [3:0] online_state = 4'd0 ; //狀態機
reg runout_flag = 0 ; //溢位標志
wire [31:0] MAX_TAGE ; //誤差上限
wire [31:0] MIX_TAGE ; //誤差下限
parameter FSM_online_no = 4'd0 ; //不在線狀態
parameter FSM_MID1 = 4'd1 ; //中間狀態1
parameter FSM_MID2 = 4'd2 ; //中間狀態2
parameter FSM_online_ok = 4'd3 ; //在線狀態
parameter [31:0] DIV_1S = 32'd200 ; //1s時間間隔
parameter [31:0] DIV_F_10M = 32'd200 ; //10M信號檢測
parameter test_period = 4*DIV_1S ; //測驗周期
assign o_det_state = oline_flag ; //在線標志賦值給輸出埠
assign mux_select_sig = MODE_select ? div_1pps : i_det_sig ; //選擇在線檢測還是1pps分頻之后檢測
assign mux_sig_pose = !mux_reg[1] && mux_reg[0] ; //被檢測信號的上升沿信號
assign div_param = MODE_select ? DIV_1S : DIV_F_10M ; //選擇檢測模式是時鐘還是脈沖,這里選擇脈沖
assign MAX_TAGE = div_param + div_param/10_000_000 ; //誤差上限
assign MIX_TAGE = div_param - div_param/10_000_000 ; //誤差下限
always @ (posedge i_det_sig or posedge i_sys_rst) //i_det_sig信號1s分頻
begin
if(i_sys_rst)
begin
div_1pps <= 0 ;
cnt_1pps <= DETECT_period ;
end
else if(cnt_1pps == DIV_1S) //計數到1s時輸出一個1pps
begin
div_1pps <= 1 ;
cnt_1pps <= DETECT_period ;
end
else
begin
div_1pps <= 0 ;
cnt_1pps <= cnt_1pps + DETECT_period ;
end
end
always @ (posedge i_sys_clk) //被檢測信號打兩拍
begin
mux_reg <= {mux_reg[0],mux_select_sig} ;
end
always @ (posedge i_sys_clk or posedge i_sys_rst)//溢位標志和脈沖計數
begin
if(i_sys_rst)
begin
pulse_cnt <= SYS_CLK_period ; //脈沖計數清零
runout_flag <= 0 ; //溢位標志拉低
end
else if(mux_sig_pose)
begin
pulse_cnt <= SYS_CLK_period ; //上升沿來的時候計數器清零
runout_flag <= 0 ; //上升沿來的時候溢位標志拉低
end
else if(pulse_cnt >= test_period) //計數器大于檢測周期的時候溢位標志拉高,計數器保持不變
begin
pulse_cnt <= pulse_cnt ;
runout_flag <= 1 ;
end
else
begin
pulse_cnt <= pulse_cnt + SYS_CLK_period ;
runout_flag <= 0 ;
end
end
always @ (posedge i_sys_clk or posedge i_sys_rst) //狀態機
begin
if (i_sys_rst)
begin
online_state <= FSM_online_no ;
end
else
begin
case(online_state)
FSM_online_no :
begin
if( !runout_flag && mux_sig_pose ) //如果沒有溢位并且上升沿來到的時候
begin
if( (pulse_cnt >=MIX_TAGE) && pulse_cnt <=MIX_TAGE) //并且脈沖計數器在誤差范圍之內的時候,調到下一個狀態
begin
online_state <= FSM_MID1 ;
end
else
begin
online_state <= FSM_online_no ;
end
end
else
begin
online_state <= FSM_online_no ;
end
end
FSM_MID1 :
begin
if(runout_flag ) //如果溢位,跳到不在線狀態
begin
online_state <= FSM_online_no ;
end
else if(mux_sig_pose)
begin
if(pulse_cnt >=MIX_TAGE && pulse_cnt <=MIX_TAGE) //如果上升沿來的時候并且脈沖計數器在誤差范圍內跳到下一個狀態
begin
online_state <= FSM_MID2 ;
end
else
begin
online_state <= FSM_online_no ;
end
end
else
begin
online_state <= FSM_MID1 ;
end
end
FSM_MID2 :
begin
if(runout_flag) //如果溢位,跳到不在線狀態
begin
online_state <= FSM_online_no ;
end
else if(mux_sig_pose)
begin
if(pulse_cnt >=MIX_TAGE && pulse_cnt <=MIX_TAGE) //如果上升沿來的時候并且脈沖計數器在誤差范圍內跳到下一個狀態
begin
online_state <= FSM_online_ok ;
end
else
begin
online_state <= FSM_MID1 ; //如果上升沿沒有來回傳到上一個狀態重新檢測
end
end
else
begin
online_state <= FSM_MID2 ;
end
end
FSM_online_ok :
begin
if(runout_flag) //如果溢位,跳到不在線狀態
begin
online_state <= FSM_online_no ;
end
else if(mux_sig_pose)
begin
if((pulse_cnt >=MIX_TAGE) && (pulse_cnt <=MIX_TAGE)) //如果上升沿來的時候并且脈沖計數器在誤差范圍內跳到下一個狀態
begin
online_state <= FSM_online_ok ;
end
else
begin
online_state <= FSM_MID2 ; //如果上升沿沒有來回傳到上一個狀態重新檢測
end
end
else
begin
online_state <= FSM_MID1 ;
end
end
default :
begin
online_state <= FSM_online_no ;
end
endcase
end
end
always @ (posedge i_sys_clk ) //信號在線標志模塊
begin
case(online_state)
FSM_online_no :
begin
oline_flag <= 1'b0 ;
end
FSM_online_ok :
begin
oline_flag <= 1'b1 ;
end
default :
begin
oline_flag <= oline_flag ;
end
endcase
end
endmodule
測驗代碼部分
module online_ddetect_tb();
reg i_sys_clk ;
reg i_sys_rst ;
reg i_det_sig ;
wire o_det_state ;
initial
begin
i_sys_clk = 0 ;
i_sys_rst = 0 ;
i_det_sig = 0 ;
#100 i_sys_rst = 1 ;
#20 i_sys_rst = 0 ;
end
always # 50 i_sys_clk = ~i_sys_clk ;
always #50 i_det_sig = ~i_det_sig ;
online_detect #(
.SYS_CLK_period (100 ),//系統時鐘周期
.DETECT_period (100 ), //檢測周期
.MODE_select (1 ) //模式選擇
)
online_detect_inst (
.i_sys_clk (i_sys_clk),//輸入系統時鐘
.i_sys_rst (i_sys_rst),//輸入系統復位
.i_det_sig (i_det_sig),//輸入被測信號
.o_det_state(o_det_state) //輸出被檢測信號
);
endmodule
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/147536.html
標籤:其他
