?? pwm.txt
字號:
module top(clk,rst,code_pulse,pwm_out);
input clk;//1mhz
input rst;
output pwm_out;
input code_pulse;碼盤的脈沖
reg [31:0] duty_cycle_r;
reg [31:0] counter;
reg pwm_out;
parameter clock_divide_r = 32'd2853;
//PWM利用碼盤的脈沖寬度來調整PWM脈沖。每收到5個碼盤脈沖,進行依次PWM調整。
reg[3:0] code_count;
always @ (posedge code_pulse or negedge rst)
begin
if(!rst)code_count<=4'b0000;
else begin
code_count<=code_count+1;
if(code_count==5)
code_count<=4'b0000;
end
end
//在每個調整周期內,記錄需要的時間。
reg[3:0] code_count_time;
reg[3:0] code_count_time2;
always @ (posedge clk1m or negedge rst)
begin
if(!rst)begin code_count_time<=4'b0000;code_count_time2<=4'b0000;end
else begin
if(code_count>=0 && code_count<5)
code_count_time<=code_count_time+1;
if(code_count==5)
begin code_count_time2<=code_count_time;
code_count_time<=4'b0000;end
end
end
//PWM調整進程,步徑為100個時鐘
always @ (posedge clk1m or negedge rst)
begin
if(!rst) duty_cycle_r<= 32'd2231;
if(code_count_time2>=3000)
begin duty_cycle_r<=duty_cycle_r +32'd100;
code_count_time2<=4'b0000;end
end
//計數器進程
always @(posedge clk or negedge rst)
begin
if (!rst)begin
counter <= 0;
end
else
begin
if (counter >= clock_divide_r)
counter <= 0;
else
counter <= counter + 1;
end
end
//PWM的控制輸出進程
always @(posedge clk or negedge rst)
begin
if (!rst)begin
pwm_out <= 0;
end
else
begin
if (counter <= duty_cycle_r)
pwm_out <= 1'b1;
else
pwm_out <= 1'b0;
end
end
endmodule
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -