module sonic_detect_1(clk,rst_n,trig_1,echo_1,distance_1); input clk,rst_n; input echo_1; //from sonic power output trig_1; //to sonic power output [23:0]distance_1; reg [23:0]distance_reg_1; //produce trig signal //period 60ms reg test_1; reg [21:0]cnt_period; always @(posedge clk ) begin if(!rst_n) begin cnt_period<=0; end else if(cnt_period==22'd3000000) begin cnt_period<=0; end else cnt_period<=cnt_period+1'b1; end assign trig_1=((cnt_period>=24'd100)&(cnt_period<=24'd599))?1:0; //detect echo signal,compute the distance wire start_1,finish_1; reg [23:0]cnt; //compute by the max distance,max time assign start_1=echo_reg1&~echo_reg2; //posedge assign finish_1=~echo_reg1&echo_reg2; //negedge reg echo_reg1,echo_reg2; always @(posedge clk) begin if(!rst_n) begin echo_reg1<=0; echo_reg2<=0; end else begin echo_reg1<=echo_1; echo_reg2<=echo_reg1; end end parameter idle=2'b00; parameter state1=2'b01; parameter state2=2'b10; reg [1:0]state; always @(posedge clk) begin if(!rst_n) begin state<=idle; cnt<=0; end else begin case(state) idle: begin if(start_1) state<=state1; else state<=idle; end state1: begin if(finish_1) state<=state2; else begin cnt<=cnt+1'b1; state<=state1; end end state2: begin cnt<=0; distance_reg_1<=cnt; state<=idle; end default: state<=idle; endcase end end assign distance_1=distance_reg_1; endmodule 然后是另外两个模块 sonic_detect_2,sonic_detect_3。