Verilog 从入门到入土 第1篇 简单模块的编写

目录

前言

一、组合逻辑电路

1. 3-8译码器

2.编码器

3.三态门

二、时序逻辑电路

1. 计数器

2.移位寄存器

3.序列脉冲发生器

4.D触发器

三丶序列机

总结


前言

这里给出了简单模块的编写如图,由于电路比较简单,文章只给出代码,具体原理我附上参考链接,一看就明白了

一、组合逻辑电路

1. 3-8译码器

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.编码器

译码器的反向处理,多用于控制场景

编码器与译码器(FPGA)

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

三态门

三态门真值表
AselY
任意1A
任意03'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触发器

像咬着自己尾巴的贪吃蛇

移位寄存器原理

移位寄存器(FPGA实现)

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触发器

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


总结

目标是为了做项目来准备的,简单模块是用来复习巩固使用,整理博客只是为了方便自己存档,看得不舒服不要喷博主

复杂电路中需要用到简单电路都是调用即可,一般不会出现错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值