「Verilog学习笔记」信号发生器

专栏前言

本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

方波的实现,较为简单,只需要设置一个计数器,使输出保持10个时钟为0,跳变为20,再保持10个时钟。依次循环。可以按照如下的过程实现:cnt每个时钟加一,当cnt=19时,将wave的值置为0,同时cnt置为0;当cnt=9时,将wave的值置为20。

锯齿波的实现,即输出信号由0每次加一递增到20,然后从20跳变到0。可以按照如下的过程实现:当wave=20,将wave的值置为0,其余时刻,wave加一。

三角波的实现,类似于锯齿波,但当wave达到20时,不是跳变到0,而是以一为步长递减到0。可以设置一个指示信号up,当up的值是1时,wave每个时钟加一。当up的值为0时,则减一。

复制代码
`timescale 1ns/1ns
module signal_generator(
	input clk,
	input rst_n,
	input [1:0] wave_choise,
	output reg [4:0]wave
	);

	reg [4:0] cnt ; 
	reg up ;
	always @ (posedge clk or negedge rst_n) begin 
		if (!rst_n) begin 
			wave <= 5'd0 ; 
			cnt <= 5'd0 ; 
		end
		else begin 
			case (wave_choise) 
				2'b00 : begin 
					if (cnt == 5'd19) begin 
						wave <= 5'd0 ; 
						cnt <= 5'd0 ; 
					end
					else if (cnt == 5'd9) begin 
						wave <= 5'd20 ;
						cnt <= cnt + 1'd1 ; 
					end
					else begin 
						wave <= wave ; 
						cnt <= cnt + 1'd1 ; 
					end
				end
				2'b01 : begin
					if (wave == 5'd20) wave <= 5'd0 ; 
					else wave <= wave + 1'd1 ; 
				end
				2'b10 : begin 
					if (wave == 5'd20) begin 
						wave <= wave - 1'd1 ; 
						up <= 1'd0 ; 
					end
					else if (wave == 5'd0) begin 
						wave <= wave + 1'd1 ; 
						up <= 1'd1 ; 
					end
					else if (up) wave <= wave + 1'd1 ; 
					else wave <= wave - 1'd1 ; 
				end
				default : 
					wave <= 1'b0 ; 
			endcase
		end
	end			
  
endmodule
相关推荐
点心的游戏开发世界13 分钟前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
摇滚侠18 分钟前
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 使用 JdbcTemplate 阅读笔记 32
spring boot·笔记·后端
sunshine22 girl2 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
鱼子星_2 小时前
【C++】继承和多态(上)
c++·笔记
Kobebryant-Manba2 小时前
学习Bert微调
人工智能·学习·bert
xian_wwq3 小时前
【学习笔记】深度认知系列-第14讲 端侧AI崛起——为什么AI正在从云端走向本地
人工智能·笔记·学习
我爱cope3 小时前
【计算机网络 | 传输层3:TCP 协议概述:面向连接、可靠传输到底意味着什么?】
网络·网络协议·学习·tcp/ip·计算机网络·传输层
策案案案4 小时前
YOLO: 将AI Agents嵌入到IntelliJ IDEA
java·大数据·人工智能·笔记·yolo·microsoft·intellij-idea
代码的小搬运工5 小时前
KVO学习
学习·ios·cocoa
科恒盛远5 小时前
RFSoC XCZU47DR 100G UDP高速数据传输的实现与调试(PCIE908 板卡技术系列三)
fpga开发