|
本帖最后由 lcytms 于 2016-11-3 21:02 编辑
好了锯齿波的物理原理既然清楚了,就来建模仿真吧。
建立新工程文件夹pulse。
编写pulse.v文件。
module pulse(clk, rst_n, led);
input clk, rst_n;
output [3:0] led;
reg [31:0] count5us, count5ms, count5s; //, count_duty
reg flag5us, flag5us_r, flag5ms, flag5s;
reg dutysawtooth;
reg switch5s;
parameter T5us = 250; // 5us/20ns = 250
parameter CNT1k = 1000; // 1000
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
count5us <= 0;
flag5us <= 0;
end
else
begin
if (count5us < T5us - 1)
begin
count5us <= count5us + 1;
flag5us <= 0;
end
else
begin
count5us <= 0;
flag5us <= 1;
end
end
end
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
flag5us_r <= 0;
else
flag5us_r <= flag5us;
end
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
count5ms <= 0;
flag5ms <= 0;
end
else
begin
if (!flag5us)
flag5ms <= 0;
else
begin
if (count5ms < CNT1k - 1)
begin
count5ms <= count5ms + 1;
flag5ms <= 0;
end
else
begin
count5ms <= 0;
flag5ms <= 1;
end
end
end
end
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
count5s <= 0;
flag5s <= 0;
end
else
begin
if (!flag5ms)
flag5s <= 0;
else
begin
if (count5s < CNT1k - 1)
begin
count5s <= count5s + 1;
flag5s <= 0;
end
else
begin
count5s <= 0;
flag5s <= 1;
end
end
end
end
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
dutysawtooth <= 0;
else if (flag5us)
begin
if (count5ms <= count5s) //需要重点理解的地方,对应第n个周期占空比为n‰
dutysawtooth <= 1;
else
dutysawtooth <= 0;
end
end
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
switch5s <= 0;
else
begin
if (flag5s)
switch5s <= ~switch5s;
else
switch5s <= switch5s;
end
end
wire dutysawtooth_o;
assign dutysawtooth_o = dutysawtooth & flag5us_r;
////////////////////////////////////////////////////////////
wire dutytriangle;
assign dutytriangle = switch5s ? ~dutysawtooth : dutysawtooth;
wire dutytriangle_o;
assign dutytriangle_o = dutytriangle & flag5us_r;
////////////////////////////////////////////////////////////
// assign led = {3'b111, ~dutysawtooth_o};
assign led = {~dutytriangle, 2'b11, ~dutysawtooth};
// assign led = {4{~dutytriangle}};
// assign led = {4{~dutysawtooth}};
endmodule
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?我要注册
x
|