vivado實現模16的計數器


 1 `timescale 1ns / 1ps
 2 module ctr_mod_16(
 3     clk,
 4     rst_n,
 5     count
 6     );
 7     input clk, rst_n;
 8     output [3:0] count;
 9     wire clk, rst_n;
10     reg [3:0] count;
11     
12     always @ (posedge rst_n  or negedge clk)
13     begin
14         if(rst_n == 0)
15             count = 4'b0000;//這里也要符合4比特的格式
16         else
17             count <= (count + 1) % 16;
18     end
19 endmodule

testbench:

 1 `timescale 1ns / 1ps
 2 module ctr_mod_16_tb;
 3 reg clk, rst_n;
 4 wire [3:0] count;
 5 initial
 6 $monitor ("count = %b", count);
 7 initial
 8     begin
 9         #0  rst_n = 1'b0;
10         #20  rst_n = 1'b1;
11     end
12 initial
13     begin
14             #0  clk = 1'b0;
15         forever
16             #10 clk = ~clk;
17     end
18 initial
19     begin
20         #320 $stop;
21     end    
22     
23 ctr_mod_16 inst(
24     .clk(clk),
25     .rst_n(rst_n),
26     .count(count)
27 );        
28 endmodule

結果為:

這里需要注意的是:initial后的#延時是相對於零時刻了,而且,這里新接觸了一個關鍵字forever,這里是實現無線次數的操作。

這里的時鍾clk取反,很巧妙,實現的clk的01變化。

另一個注意點就是always括號中的敏感變量必須是輸入信號!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM