我是靠谱客的博主 笨笨御姐,最近开发中收集的这篇文章主要介绍HDLbits答案更新系列8(3.2 Sequential Logic 3.2.2 Counters)前言3.2.2 Counters结语HDLbits网站链接,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

前言

3.2.2 Counters

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)

结语

HDLbits网站链接


前言

今天更新一个小节内容:计数器。计数器可以说是我们接触数字电路以后用的最频繁的模块之一了,无论是项目、应聘还是将来的工作,计数器都无处不在,希望大家能够掌握这部分内容。

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
endmodule

这道题目修改了复位条件,改成了复位为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;
always@(posedge clk)begin
if(reset)begin
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

这道题目作者想让我们设计一个12小时的时钟,题目不是很难,大家可以看我的清零和加一条件怎么设置的,这样写代码阅读性比较高,建议大家采用这种方式。

结语

今天更新一个小节,虽然都是一些比较基础的计数器,但是做起来也不都是那么容易的,最后一题时钟的设计需要掌握好各种条件,对于分和秒来说是一样的,但是小时就是另外一种写法,希望大家都能去练习一下。

HDLbits网站链接

HDLBits

最后

以上就是笨笨御姐为你收集整理的HDLbits答案更新系列8(3.2 Sequential Logic 3.2.2 Counters)前言3.2.2 Counters结语HDLbits网站链接的全部内容,希望文章能够帮你解决HDLbits答案更新系列8(3.2 Sequential Logic 3.2.2 Counters)前言3.2.2 Counters结语HDLbits网站链接所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部