3.2.2.1 Four-bit binary counter(Count15)
3.2.2.2 Decade counter(Count10)
3.2.2.3 Decade counter again(Count1to10)
3.2.2.4 Show decade counter(Countslow)
3.2.2.5 Counter 1-12(Exams/ece241 2014 q7a)
3.2.2.6 Counter 1000(Exams/ece241 2014 q7b)
3.2.2.7 4-digit decimal counter(Countbcd)
3.2.2.8 12-hour clock(Count clock)
3.2.2 Counters
3.2.2.1 Four-bit binary counter(Count15)
- module top_module (
- input clk,
- input reset, // Synchronous active-high reset
- output [3:0] q);
-
- always@(posedge clk)begin
- if(reset)begin
- q <= 4'd0;
- end
- else begin
- q <= q + 1'b1;
- end
- end
-
- endmodule
这道题是要实现一个计数到15清零的计数器,大家注意,如果不是计数到15,需要加一个清零条件。这道题目可以不用添加。
3.2.2.2 Decade counter(Count10)
- module top_module (
- input clk,
- input reset, // Synchronous active-high reset
- output [3:0] q);
-
- always@(posedge clk)begin
- if(reset)begin
- q <= 4'd0;
- end
- else if(q == 4'd9)begin
- q <= 4'd0;
- end
- else begin
- q <= q + 1'b1;
- end
- end
-
- endmodule
这道题目就需要加一个清零条件。
3.2.2.3 Decade counter again(Count1to10)
- module top_module (
- input clk,
- input reset,
- output [3:0] q);
-
- always@(posedge clk)begin
- if(reset)begin
- q <= 4'd1;
- end
- else if(q == 4'd10)begin
- q <= 4'd1;
- end
- else begin
- q <= q + 1'b1;
- end
- end
这道题目修改了复位条件,改成了复位为1,原理是相同的。
3.2.2.4 Show decade counter(Countslow)
- module top_module (
- input clk,
- input slowena,
- input reset,
- output [3:0] q);
-
- always@(posedge clk)begin
- if(reset)begin
- q <= 4'd0;
- end
- else if(slowena)begin
- if(q == 4'd9)begin
- q <= 4'd0;
- end
- else begin
- q <= q + 1'b1;
- end
- end
- end
-
- endmodule
这道题多了一个使能信号,只有使能信号为高的时候计数器才开始工作,否则就保持原来的值。
3.2.2.5 Counter 1-12(Exams/ece241 2014 q7a)
- module top_module (
- input clk,
- input reset,
- input enable,
- output [3:0] Q,
- output c_enable,
- output c_load,
- output [3:0] c_d
- ); //
-
- assign c_enable = enable;
- assign c_load = reset | ((Q == 4'd12) && enable == 1'b1);
- assign c_d = c_load ? 4'd1 : 4'd0;
-
- count4 u_counter (clk, c_enable, c_load, c_d, Q);
-
- endmodule
3.2.2.6 Counter 1000(Exams/ece241 2014 q7b)
- module top_module (
- input clk,
- input reset,
- output OneHertz,
- output [2:0] c_enable
- ); //
- wire[3:0] one, ten, hundred;
-
- assign c_enable = {one == 4'd9 && ten == 4'd9, one == 4'd9, 1'b1};
- assign OneHertz = (one == 4'd9 && ten == 4'd9 && hundred == 4'd9);
-
- bcdcount counter0 (clk, reset, c_enable[0], one);
- bcdcount counter1 (clk, reset, c_enable[1], ten);
- bcdcount counter2 (clk, reset, c_enable[2], hundred);
-
- endmodule
这两道题目都是例化作者已经写好的module从而创造一种新的计数器。
3.2.2.7 4-digit decimal counter(Countbcd)
- module top_module (
- input clk,
- input reset, // Synchronous active-high reset
- output [3:1] ena,
- output [15:0] q);
-
- reg [3:0] ones;
- reg [3:0] tens;
- reg [3:0] hundreds;
- reg [3:0] thousands;
-
- always@(posedge clk)begin
- if(reset)begin
- ones <= 4'd0;
- end
- else if(ones == 4'd9)begin
- ones <= 4'd0;
- end
- else begin
- ones <= ones + 1'b1;
- end
- end
-
- always@(posedge clk)begin
- if(reset)begin
- tens <= 4'd0;
- end
- else if(tens == 4'd9 && ones == 4'd9)begin
- tens <= 4'd0;
- end
- else if(ones == 4'd9) begin
- tens <= tens + 1'b1;
- end
- end
-
- always@(posedge clk)begin
- if(reset)begin
- hundreds <= 4'd0;
- end
- else if(hundreds == 4'd9 && tens == 4'd9 && ones == 4'd9)begin
- hundreds <= 4'd0;
- end
- else if(tens == 4'd9 && ones == 4'd9) begin
- hundreds <= hundreds + 1'b1;
- end
- end
-
- always@(posedge clk)begin
- if(reset)begin
- thousands <= 4'd0;
- end
- else if(thousands == 4'd9 && hundreds == 4'd9 && tens == 4'd9 && ones == 4'd9)begin
- thousands <= 4'd0;
- end
- else if(hundreds == 4'd9 && tens == 4'd9 && ones == 4'd9) begin
- thousands <= thousands + 1'b1;
- end
- end
-
- assign q = {thousands, hundreds, tens, ones};
- assign ena[1] = (ones == 4'd9) ? 1'b1 : 1'b0;
- assign ena[2] = (tens == 4'd9 && ones == 4'd9) ? 1'b1 : 1'b0;
- assign ena[3] = (hundreds == 4'd9 && tens == 4'd9 && ones == 4'd9) ? 1'b1 : 1'b0;
-
- endmodule
这道题目大家只要掌握好清零条件和加一条件就好了。对于我的这种写法,更好的写法应该是讲条件单独用assign写下来,这样计数器的层次感比较强,代码可阅读性更好。下面的这道题目就是这种方式。
3.2.2.8 12-hour clock(Count clock)
- module top_module(
- input clk,
- input reset,
- input ena,
- output pm,
- output [7:0] hh,
- output [7:0] mm,
- output [7:0] ss);
-
- reg pm_temp;
- reg [3:0] ss_ones;
- reg [3:0] ss_tens;
- reg [3:0] mm_ones;
- reg [3:0] mm_tens;
- reg [3:0] hh_ones;
- reg [3:0] hh_tens;
- wire add_ss_ones;
- wire end_ss_ones;
- wire add_ss_tens;
- wire end_ss_tens;
- wire add_mm_ones;
- wire end_mm_ones;
- wire add_mm_tens;
- wire end_mm_tens;
- wire add_hh_ones;
- wire end_hh_ones_0;
- wire end_hh_ones_1;
- wire add_hh_tens;
- wire end_hh_tens_0;
- wire end_hh_tens_1;
- wire pm_ding;
-
- always@(posedge clk)begin
- if(reset)begin
- ss_ones <= 4'd0;
- end
- else if(add_ss_ones)begin
- if(end_ss_ones)begin
- ss_ones <= 4'd0;
- end
- else begin
- ss_ones <= ss_ones + 1'b1;
- end
- end
- end
-
- assign add_ss_ones = ena;
- assign end_ss_ones = add_ss_ones && ss_ones == 4'd9;
-
- always@(posedge clk)begin
- if(reset)begin
- ss_tens <= 4'd0;
- end
- else if(add_ss_tens)begin
- if(end_ss_tens)begin
- ss_tens <= 4'd0;
- end
- else begin
- ss_tens <= ss_tens + 1'b1;
- end
- end
- end
-
- assign add_ss_tens = end_ss_ones;
- assign end_ss_tens = add_ss_tens && ss_tens == 4'd5;
-
- always@(posedge clk)begin
- if(reset)begin
- mm_ones <= 4'd0;
- end
- else if(add_mm_ones)begin
- if(end_mm_ones)begin
- mm_ones <= 4'd0;
- end
- else begin
- mm_ones <= mm_ones + 1'b1;
- end
- end
- end
-
- assign add_mm_ones = end_ss_tens;
- assign end_mm_ones = add_mm_ones && mm_ones == 4'd9;
-
- always@(posedge clk)begin
- if(reset)begin
- mm_tens <= 4'd0;
- end
- else if(add_mm_tens)begin
- if(end_mm_tens)begin
- mm_tens <= 4'd0;
- end
- else begin
- mm_tens <= mm_tens + 1'b1;
- end
- end
- end
-
- assign add_mm_tens = end_mm_ones;
- assign end_mm_tens = add_mm_tens && mm_tens == 4'd5;
-
- always@(posedge clk)begin
- if(reset)begin
- hh_ones <= 4'd2;
- end
- else if(add_hh_ones)begin
- if(end_hh_ones_0)begin
- hh_ones <= 4'd0;
- end
- else if(end_hh_ones_1)begin
- hh_ones <= 4'd1;
- end
- else begin
- hh_ones <= hh_ones + 1'b1;
- end
- end
- end
-
- assign add_hh_ones = end_mm_tens;
- assign end_hh_ones_0 = add_hh_ones && hh_ones == 4'd9;
- assign end_hh_ones_1 = add_hh_ones && (hh_tens == 4'd1 && hh_ones == 4'd2);
-
- always@(posedge clk)begin
- if(reset)begin
- hh_tens <= 4'd1;
- end
- else if(add_hh_tens)begin
- if(end_hh_tens_0)begin
- hh_tens <= 4'd0;
- end
- else if(end_hh_tens_1)begin
- hh_tens <= hh_tens + 1'b1;
- end
- end
- end
-
- assign add_hh_tens = end_mm_tens;
- assign end_hh_tens_0 = add_hh_tens && end_hh_ones_1;
- assign end_hh_tens_1 = add_hh_tens && end_hh_ones_0;
-
- alwaysosedge clk)begin
- if(reset)beg@(p
- in
- pm_temp <= 1'b0;
- end
- else if(pm_ding)begin
- pm_temp <= ~pm_temp;
- end
- end
-
- assign pm_ding = hh_tens == 4'd1 && hh_ones == 4'd1 && end_mm_tens;
-
- assign ss = {ss_tens, ss_ones};
- assign mm = {mm_tens, mm_ones};
- assign hh = {hh_tens, hh_ones};
- assign pm = pm_temp;
-
- endmodule
沒有留言:
張貼留言