搜索
bottom↓
回复: 1

[分享] 延时器的写法

[复制链接]

出0入0汤圆

发表于 2010-8-31 17:50:13 | 显示全部楼层 |阅读模式
module test
(
    CLK, RSTn,
         Pin_Out
);

      input CLK;
          input RSTn;
          output [3:0]Pin_Out;
          
          /****************************************/
         
         parameter T1MS = 16'd19_999;
         
         /***************************************/
         
         reg [15:0]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 [9:0]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 [3:0]i;
        reg [3:0]rPin_Out;
        reg [9:0]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[0], rPin_Out[3:1] }; 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

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

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

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2010-8-31 23:47:53 | 显示全部楼层
感谢分享
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 17:31

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表