当前位置:网站首页>FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)
FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)
2022-07-30 10:29:00 【居安士】
继续牛客网刷题,这篇是计数器部分:

目录
计数器是非常基本的使用,没有计数器就无法处理时序。
简易秒表

这一题实现了秒和分的计数器,当秒计数器从1-60,分计数器+1,当分计数器=60,清零分计数器。用一段always就可以实现,代码如下:
module count_module(
input clk,
input rst_n,
output reg [5:0]second,
output reg [5:0]minute
);
[email protected](posedge clk or negedge rst_n)begin
if(rst_n == 1'b0)begin
second <= 6'd0;
minute <= 6'd0;
end
else begin
if(minute==6'd59)begin
minute <= 6'd60;
end
else if(second == 6'd60)begin
second <= 6'd1;
minute <= minute + 1'b1;
end
else begin
second <= second + 1'b1;
minute <= minute;
end
end
end
endmodule这里需要注意的是计数器的初值,比如计数器60次一清零,如果清零时为计数器赋0,那么清零的条件应该写60-1(0~59是60个数)如果清零时为计数器赋1,那么清零的条件应该写60
可置位计数器

题目要求编写一个十六进制计数器,即输出数值number每次变化1,在0-15之间循环。输出set信号的值为一时,将输出数值num置为set_num。
module count_module(
input clk,
input rst_n,
input set,
input [3:0] set_num,
output reg [3:0]number,
output reg zero
);
reg [3:0]num;
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
zero <= 1'd0;
end
else if (num == 4'd0)
begin
zero <= 1'b1;
end
else
begin
zero <= 1'b0;
end
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
num <= 4'b0;
end
else if(set)
begin
num <= set_num;
end
else
begin
num <= num + 1'd1;
end
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
number <= 1'd0;
end
else
begin
number <= num;
end
endmodule加减计数器

题目要求编写一个十进制计数器,即输出数值number每次变化1,在0-9之间循环。根据mode信号的值,更改输出数值number的变化方向。当mode=1,计数器递增。当mode=0,计数器递减。可以将mode作为判断条件,使用if-else语句实现不同的变化方向。
注意,这一题的zero信号是用时序逻辑来赋值,利用number的过程值num进行判断;
- 题目的testbench要求输出都延后一个时钟,因此增加一个寄存器tmp来暂存计数。
- 根据mode来判断计数何时跳变;zero输出时刻在tmp为0的下一周期,因此用tmp==0来判断。
module count_module(
input clk,
input rst_n,
input mode,
output reg [3:0]number,
output reg zero
);
reg [3:0]num;
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
zero <= 1'd0;
end
else if (num == 4'd0)
begin
zero <= 1'b1;
end
else
begin
zero <= 1'b0;
end
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
num <= 4'b0;
end
else if(mode)
begin
if(num == 9)
num <= 0;
else
num <= num + 1'd1;
end
else if(!mode)
begin
if(num == 0)
num <= 9;
else
num <= num - 1'd1;
end
else num <= num;
always @(posedge clk or negedge rst_n)
if (!rst_n)
begin
number <= 4'd0;
end
else
begin
number <= num;
end
endmodule从上面三道题可以看出,计数器基本都使用 if-else 语句进行实现,使用if-else语句时,要注意代码是从上到下执行,如果if里面的语句可以实现,就不会跳转到else,因此,应该把条件判断的部分(不容易实现的部分)写在最前面,以此类推。
边栏推荐
- 360 released a future-oriented EDR to protect the security of government and enterprise user terminals in an all-round way
- Array of Shell System Learning
- SST-Calib:结合语义和VO进行时空同步校准的lidar-visual外参标定方法(ITSC 2022)
- 神经网络学习笔记4——自动编码器(含稀疏,堆叠)(更新中)
- Materialist Dialectics - Conditionalism
- spark udf 接受并处理 null值.
- Beyond Stream Processing !第四届实时计算 Flink 挑战赛启动,49 万奖金等你来拿!
- Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- WEB3之路(一)-- solidity学习笔记
- Study Notes 10--Main Methods of Local Trajectory Generation
猜你喜欢

PyQt5 - Drawing different types of straight lines

WARN: Establishing SSL connection without server's identity verification is not recommended when connecting to mysql

Matplotlib--plot markers

jmeter接口压力测试-(二)

By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)

第3章 信息收集

Meikle Studio - see the actual combat notes of Hongmeng equipment development five - drive subsystem development

电压跟随器不要随便加

Detailed explanation of JVM memory layout, class loading mechanism and garbage collection mechanism

(C语言)文件操作
随机推荐
JCL learning
SST-Calib: A lidar-visual extrinsic parameter calibration method combining semantics and VO for spatiotemporal synchronization calibration (ITSC 2022)
XYplorer 23多语言,最好的管理软件之一
Container Technology - A Simple Understanding of Kubernetes Objects
Classes and Objects - 6 Default Member Functions
梅科尔工作室-看鸿蒙设备开发实战笔记五——驱动子系统开发
idea2021+Activiti [the most complete note one (basic use)]
关于verilog的时延研究
Array of Shell System Learning
4、yolov5-6.0 ERROR: AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘ 解决方案
Soft test system architects introductory tutorial | system operation and software maintenance
梅科尔工作室-看鸿蒙设备开发实战笔记四——内核开发
Redis Desktop Manager 2022.4.2 released
在机器人行业的专业人士眼里,机器人行业目前的情况如何?
【HMS core】【FAQ】HMS Toolkit典型问题合集1
[Deep Learning] (Problem Record)
- Linear Regression - Small Batch Stochastic Gradient Descent [AGC] Growth Service 2 - In-App Message Example
2022全球数字经济大会人工智能专场:AI安全受高度关注
多线程保证单个线程开启事务并生效的方案
TestNg整合Retry代码