一、三角波发生器逻辑设计
所需软件
Verilog编程软件:Lattice Diamond(3.11.0.396.4_Diamond_x64)
Verilog仿真软件:ModelSim SE-64 10.2c(modelsim-win64-10.2c-se)
方法:用always语句块、组合逻辑实现三角波发生器逻辑设计
符号图:
Verilog代码:
Part1:Test14_tri_gen.v文件(Verilog工程文件)
//2020-06-06
//最简单的状态机(三角波发生器)
module Test14_tri_gen(
clk,
res,
d_out
);
input clk;
input res;
output[8:0] d_out; //输出结果(299需要占用9bit)
reg state; //定义状态机寄存器,因只有两个状态,所以占用1bit就足够了
reg[8:0] d_out; //299需要占用9bit
always@(posedge clk or negedge res)
if(~res) begin //如果复位为低
state<=0;d_out<=0; //状态机进入零状态,d_out也为0
end
else begin
case(state) //状态机
0: begin d_out<=d_out+1; //上升
if(d_out==299) begin //跳转条件(计数器+1,加到299,跳转到1状态)
state<=1; //1状态
end
end
1: begin d_out<=d_out-1; //下降
if(d_out==1) begin //跳转条件(计数器-1,减到1,跳转到0状态)
state<=0; //0状态
end
end
endcase
end
endmodule
Part2:Test14_tri_gen_tb.v文件(Verilog仿真文件)
//2022-06-06
//最简单的状态机(三角波发生器)-testbench of Test14_tri_gen
`timescale 1ns/10ps
module Test14_tri_gen_tb;
reg clk_in,res_in;
wire[8:0] d_out;
Test14_tri_gen U1(
.clk(clk_in), //异名例化
.res(res_in),
.d_out()
);
initial begin
clk_in<=0;res_in<=0;
#17 res_in<=1;
#8000 $stop; //系统时钟周期为10ns,实现1-300、300-1计数,遍历运行一遍需要6000ns
end
always #5 clk_in<=~clk_in; //设置系统时钟周期为10ns
endmodule
仿真波形:
注意事项
1、因本状态机只有两个状态,所以占用1bit就足够了;
2、因本三角波的取值范围为1-300,故占用9bit即可满足计数;
3、因设定了系统时钟周期为10ns,故实现1-300、300-1计数,遍历运行一遍需要6000ns。