当前位置:网站首页>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,因此,应该把条件判断的部分(不容易实现的部分)写在最前面,以此类推。
边栏推荐
- Alibaba Cloud OSS Object Storage
- 类和对象—6个默认成员函数
- Database dirty reads, non-repeatable reads, phantom reads and corresponding isolation levels
- RandLA-Net复现记录
- The configuration process and related syntax of writing markdown format notes in vscode
- Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- Re16: Read the paper ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation
- Redis Desktop Manager 2022.4.2 released
- Neural Network Study Notes 3 - LSTM Long Short-Term Memory Network
- 【AGC】增长服务2-应用内消息示例
猜你喜欢
typescript入门之helloworld
实现web实时消息推送的7种方案
[Deep Learning] (Problem Record)
- Linear Regression - Small Batch Stochastic Gradient Descent nacos实战项目中的配置
360 released a future-oriented EDR to protect the security of government and enterprise user terminals in an all-round way
图像去噪——Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images
Do you really understand the 5 basic data structures of Redis?
【 HMS core 】 【 Analytics Kit] [FAQ] how to solve the payment amount in huawei pay analysis shows zero problem?
flowable workflow all business concepts
(C语言)文件操作
随机推荐
【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
实现web实时消息推送的7种方案
Neural Network Study Notes 4 - Autoencoder (including sparse, stacked) (updated)
Adaptive Control - Simulation Experiment 1 Designing Adaptive Laws Using Lyapunov's Stability Theory
Understanding of deadlock
2022全球数字经济大会人工智能专场:AI安全受高度关注
New in GNOME: Warn users when Secure Boot is disabled
Re16: Read the paper ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation
神经网络学习笔记3——LSTM长短期记忆网络
再有人问你分布式事务,把这篇扔给他
Container Technology - A Simple Understanding of Kubernetes Objects
Neural Network Study Notes 3 - LSTM Long Short-Term Memory Network
Drag and drop events, dataTransfer, getBoundingClientRect
Multithreading--the usage of threads and thread pools
Mysterious APT Attack
Some commands of kubernetes
软考 系统架构设计师 简明教程 | 系统运行与软件维护
Online target drone prompt.ml
[100 Solidity Skills] 1. Contract reentrancy attack
Classes and Objects - 6 Default Member Functions