【verilog】模十计数器


文章目录


前言

进行 FPGA 模十计数器 实验

  • 仿真结果

代码

  • 主代码
go 复制代码
// module
module count(
	clk,
	rst_n,
	count,
	clk1,
	led
	);


// def io
input clk;
input rst_n;
output reg [3:0] count;
output reg clk1;
output reg [7:0] led;

// always@ part, or main()
always @(posedge clk or negedge rst_n)
begin 
	if (~rst_n)

		begin
			count <= 4'b0000;
			clk1 <= 1'b0;
		end

	else if (count < 4'b0100)
		begin
			count <= count + 1;
			clk1 <= 1'b0;
		end

	else if (count < 4'b1001)
		begin
			count <= count + 1;
			clk1 <= 1'b1;
		end

	else if (count == 4'b1001)
		begin
			count <= 4'b0000;
			clk1 <= 1'b0;
		end
	
// end of whole begin
end




// --------------------------------------- always @ count --------------------------------------- //
always @(count) // todo: write the counter part
begin
	case(count)
		4'd0: led <= 8'b0000_0001;
		4'd1: led <= 8'b0000_0010;
		4'd2: led <= 8'b0000_0100;
		4'd3: led <= 8'b0000_1000;
		4'd4: led <= 8'b0001_0000;
		4'd5: led <= 8'b0010_0000;
		4'd6: led <= 8'b0100_0000;
		4'd7: led <= 8'b1000_0000;
		4'd8: led <= 8'b1111_1111;
		4'd9: led <= 8'b0000_0000;

		// default
		default: led <= 8'b0000_0000;
	endcase
end

endmodule
  • 测试代码
go 复制代码
`timescale 1ns/1ns

// module
module count_tst();
	// distribute value & name
	reg clk;
	reg rst_n;

	wire [3:0] count;
	wire clk1;
	wire [7:0] led;
	
	parameter period = 2;
	
	// init
	initial begin
		clk = 1'b0;
		rst_n = 1'b0;
		
		// delay
		#20 rst_n = 1'b1;
	end

	always
	begin
		#(period/2) clk = ~clk;
	end

	count u0(
		.clk(clk),
		.rst_n(rst_n),
		.count(count),
		.clk1(clk1),
		.led(led)
		);

endmodule

相关推荐
星华云4 小时前
[FPGA]Spartan6 Uart固定波特率读写JY901P惯导模块
fpga开发·verilog·jy901p·惯导模块
s090713614 小时前
ZYNQ无SD卡纯NAND Flash启动Linux全攻略
linux·fpga开发·zynq·nand flash启动
jjinl16 小时前
AG32VF407RGT6 开发流程记录
fpga开发
FPGA小迷弟16 小时前
FPGA面试题汇总整理(一)
学习·fpga开发·verilog·fpga
Z22ZHaoGGGG16 小时前
verilog 资源占用少的滤波方法
fpga开发
S&Z346317 小时前
[SZ901]FPGA 下载器硬件介绍
fpga开发
GateWorld19 小时前
FPGA内部模块详解之四 算力引擎——数字信号处理单元(DSP Slice)深度解析
fpga开发·dsp
weiyvyy20 小时前
嵌入式硬件接口开发的核心原则
驱动开发·单片机·嵌入式硬件·fpga开发·硬件架构·硬件工程
Kong_199420 小时前
芯片开发学习笔记·二十一——primetime静态时序分析
fpga开发·芯片开发
S&Z346321 小时前
[SZ901] 多路FPGA 网络下载器总览
网络·fpga开发