目录
前言
这里给出了简单模块的编写如图,由于电路比较简单,文章只给出代码,具体原理我附上参考链接,一看就明白了
一、组合逻辑电路
1. 3-8译码器
module decoder_3_8(
A,
Y
);
//-------port
input wire [2:0]A;
output reg [7:0]Y;
//------main
always@(*)
case(A)
3'b000: Y = 8'b0000_0001;
3'b001: Y = 8'b0000_0010;
3'b010: Y = 8'b0000_0100;
3'b011: Y = 8'b0000_1000;
3'b100: Y = 8'b0001_0000;
3'b101: Y = 8'b0010_0000;
3'b110: Y = 8'b0100_0000;
3'b111: Y = 8'b1000_0000;
default:Y = 8'b0000_0000;
endcase
endmodule
2.编码器
译码器的反向处理,多用于控制场景
module coder_8_3(
A,
Y
);
//-------port
input wire [7:0]A;
output reg [2:0]Y;
always@(*)
case(A)
8'b0000_0001: Y = 3'b000;
8'b0000_0010: Y = 3'b001;
8'b0000_0100: Y = 3'b010;
8'b0000_1000: Y = 3'b011;
8'b0001_0000: Y = 3'b100;
8'b0010_0000: Y = 3'b101;
8'b0100_0000: Y = 3'b110;
8'b1000_0000: Y = 3'b111;
8'b0000_0000: Y = 3'b000;
default:Y = 3'b000;
endcase
endmodule
3.三态门
使能的时候实现电路的逻辑功能,不使能时为 高阻态
在verilog中表示为 z
A | sel | Y |
---|---|---|
任意 | 1 | A |
任意 | 0 | 3'bzzz |
module coder_8_3(
A,
sel,Y
);
//-------port
input wire [2:0]A;input wire sel;
output [2:0]Y;
//高电平使能assign Y = (sel?A:3'bzzz);
endmodule
二、时序逻辑电路
1. 计数器
作为Verilog学习中绕不开的内容,这可以说是经典中的经典
电路可以理解为多个JK触发器串联
计数器FPGA实现
这里给出最常用的普通计数器
module counter(
input clk_100m,
input rst_n
);
reg [3:0] cnt;
always@(posedge clk_100m or negedge rst_n)
if(!rst_n)
cnt <= 4'd0;
else
cnt <= cnt + 1'b1;
endmodule
2.移位寄存器
和环形计数器的样子很像,不过单元换成了D触发器
像咬着自己尾巴的贪吃蛇
module shift_register_right(
clk,
din,
dout
);
// port
input clk;
input [15:0] din;
output [15:0] dout;reg [15:0] dout;
// main
always @(posedge clk) begin
dout <= {din[0], din[15:1]};
end
endmodule
3.序列脉冲发生器
一般用来做单通道的脉冲发生器也就是可以控制占空比和频率的PWM
用到了参数和分频器
module Test1
#(
parameter MCNT = 50_000_000 - 1,
parameter M_DUTY = 12_500_000
)
(
Clk ,
Reset_n ,
led
);
//----------Port
input Clk ;
input Reset_n ;
output reg led;
// Reg
reg [25:0]Count;//------main
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
Count <= 0;
else if(Count == MCNT)
Count <= 0;
else
Count <= Count + 1'd1;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
led <= 1'b0;
else if(Count == 0)
led <= 1'b1;
else if(Count == M_DUTY)
led <= 1'b0;
endmodule
4.D触发器
有脉冲电平就保存下一个输入的值
module dff(clk,clr,rst,d,q);
//clr清0,rst复位
//-------port
input clk,clr,rst,d;
output q;
reg q;//--------main
always@(posedge clk or posedge clr)
begin
if(clr==1'b1)q<=1'b0;
else if(rst==1'b1)q<=1'b1;
else q<=d;
end
endmodule
三丶序列机
这里用LED的亮灭来写一个例子
4个LED,做一个状态机流水灯
module led_twinkle_4
(
clk,
Reset_n,
Led
);
//------Port
input clk ;
input Reset_n ;
output reg [3:0]Led ;// parameter
reg [24:0]count;
parameter N_CNT = 25_000_000;
reg [1:0]state;
//分频器
always@(posedge clk or negedge Reset_n)
if(!Reset_n)
count <= 25'd0;
else if(count == N_CNT)
count <= 25'd0;
else
count <= count + 1'd1;
always@(posedge clk or negedge Reset_n)
if(!Reset_n)
state <= 1'b0;
else if(count == N_CNT)
state <= state + 1 ;always@(posedge clk or negedge Reset_n)
if(!Reset_n)
Led <= 1'b0;
else begin
case(state)
3'd0: Led <= 4'b0001;
3'd1: Led <= 4'b0010;
3'd2: Led <= 4'b0100;
3'd3: Led <= 4'b1010;
default: Led <= 4'b0000;
endcase
end
endmodule
总结
目标是为了做项目来准备的,简单模块是用来复习巩固使用,整理博客只是为了方便自己存档,看得不舒服不要喷博主
复杂电路中需要用到简单电路都是调用即可,一般不会出现错误