计数器不仅能用于对时钟脉冲计数,还可以用于分频,定时,产生节拍脉冲序列以及进行数字运算等,分为同步计数器和异步计数器,在同步计数器中,当时钟脉冲的输入时,触发器的翻转是同时发生的。而在异步计数中,触发器的翻转有先有后,不是同时发生的。
编程实现10进制计数器,具有异步复位功能,十位和个位用8421BCD码表示
module counter10_1(o_count,o_cout,i_clk,i_reset,i_load,i_datin,i_cin); output reg [7:0] o_count; output reg o_cout; input i_clk,i_reset,i_load,i_cin; input [7:0] i_datin; always @(posedge i_clk or i_reset)//Asynchronous zero setting begin if(i_reset) begin o_count <= 0;//reset o_cout <= 0; end else if(i_load) o_count <= i_datin;//Synchronous setting else if(o_count[3:0] >= 4'b1001)//gewei jinwei,shiwei jia yi begin o_count[7:4] <= o_count[7:4]+4'b0001;//shiwei plus 1 o_count[3:0] <= 0; //gewei is 0 end else if(o_count[7:4] >= 4'b1001)//shiwei jinwei begin o_count[7:0] <= 0; //all is 0 o_cout <= o_cout+1; //10bit counter jinwei signal plus 1 end else //normal count o_count[3:0] <= o_count[3:0]+4'b0001; end endmodule2.2、test module
module test_counter10; //module counter10_1(o_count,o_cout,i_clk,i_reset,i_load,i_datin,i_cin); wire [7:0] o_count_t; wire o_cout_t; reg i_clk_t,i_reset_t,i_load_t,i_cin_t; reg [7:0] i_datin_t; counter10_1 mycounter( .o_count(o_count_t), .o_cout(o_cout_t), .i_clk(i_clk_t), .i_reset(i_reset_t), .i_load(i_load_t), .i_datin(i_datin_t), .i_cin(i_cin_t)); initial //reset signal is 1--> reset load signal is 1 --> load signal begin i_datin_t[7:0]=8'b1001_1111; // zhishu signal i_clk_t=0; // clock i_reset_t = 1; // reset signal i_load_t =1; // initial zhishu signal i_cin_t = 0; // initial signal #100 i_reset_t = 0; // enter jishu type o_count = 8'b1001_1111 i_datin_t[7:0] = 8'b0000_0000; // reset initial data #80 i_load_t = 0; // enter counter type end //always #200 i_cin_t = ~i_cin_t; //jinwei signal always #20 i_clk_t = ~i_clk_t; //clock signal //always #500 i_reset_t = ~i_reset_t; //reset signal endmodule三、仿真截图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)