akuei2 发表于 2010-8-31 17:50:13

[分享] 延时器的写法

module test
(
    CLK, RSTn,
       Pin_Out
);

      input CLK;
          input RSTn;
          output Pin_Out;
          
          /****************************************/
       
       parameter T1MS = 16'd19_999;
       
       /***************************************/
       
       reg Count1;

       always @ ( posedge CLK or negedge RSTn )
             if( !RSTn )
                      Count1 <= 16'd0;
                  else if( Count1 == T1MS )
                      Count1 <= 16'd0;
                  else if( isCount )
                      Count1 <= Count1 + 1'b1;
                  else if( !isCount )
                      Count1 <= 16'd0;
       
    /****************************************/       
                               
    reg Count_MS;
          
       always @ ( posedge CLK or negedge RSTn )
      if( !RSTn )
                      Count_MS <= 10'd0;
                else if( Count_MS == rTimes )
                      Count_MS <= 10'd0;
                else if( Count1 == T1MS )
                      Count_MS <= Count_MS + 1'b1;
       
        /******************************************/
       
        reg i;
        reg rPin_Out;
        reg rTimes;
        reg isCount;
       
        always @ ( posedge CLK or negedge RSTn )
          if( !RSTn )
                     begin
                             i <= 4'd0;
                         rPin_Out <= 4'b1000;
                               isCount <= 1'b0;       
                               rTimes <= 10'd100;
                          end
               else
                     case( i )
                          
                       4'd0, 4'd1, 4'd2, 4'd3:
                       if( Count_MS >= rTimes ) begin rPin_Out <= { rPin_Out, rPin_Out }; isCount <= 1'b0; i <= i + 1'b1; end
                         else begin isCount <= 1'b1; rTimes <= 10'd1000; end                                               
                                       
                     4'd4:
                   begin i <= 4'd0; end               
                               
                   endcase       
       
        /******************************************/
       
        assign Pin_Out = rPin_Out;
       
        /******************************************/

endmodule

/============================================================================/

经过调试,比较稳定了,但是不是最好的~有需要的自己看着办吧!

caozhu 发表于 2010-8-31 23:47:53

感谢分享
页: [1]
查看完整版本: [分享] 延时器的写法