module pinlvji (clk, rst_n, wave, zhankongbi, pinlv); 
 
        input clk; 
        input rst_n; 
        input wave; 
         
        output reg [6:0] zhankongbi; 
        output reg [19:0] pinlv; 
         
        reg [25:0] t_low_count; 
        reg [25:0] t_high_count; 
         
        reg [25:0] t_low; 
        reg [25:0] t_high; 
         
        reg state; 
         
        always @ (*) 
                begin 
                        zhankongbi = 100*t_high/(t_high + t_low); 
                        pinlv = 1_000_000_000/(t_high*10 + t_low*10); 
                end 
         
        always @ (posedge clk) 
                begin 
                        if (rst_n == 0) 
                                begin 
                                        t_low <= 0; 
                                        t_high <= 0; 
                                        t_low_count <= 0; 
                                        t_high_count <= 0; 
                                        state <= 0; 
                                end 
                        else 
                                begin 
                                        case (state) 
                                                0 : begin 
                                                                if (wave == 1) 
                                                                        begin 
                                                                                t_high_count <= t_high_count + 1; 
                                                                                state <= 0; 
                                                                        end 
                                                                else 
                                                                        begin 
                                                                                t_high <= t_high_count; 
                                                                                t_high_count <= 0; 
                                                                                state <= 1; 
                                                                        end 
                                                        end 
                                                 
                                                1 : begin 
                                                                if (wave == 0) 
                                                                        begin 
                                                                                t_low_count <= t_low_count + 1; 
                                                                                state <= 1; 
                                                                        end 
                                                                else 
                                                                        begin 
                                                                                t_low_count <= 0; 
                                                                                t_low <= t_low_count; 
                                                                                state <= 0; 
                                                                        end 
                                                        end 
                                         
                                                default : state <= 0; 
                                                 
                                        endcase 
                                end 
                end 
 
 
endmodule  |