-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPwmGenor.v
48 lines (40 loc) · 850 Bytes
/
PwmGenor.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module PwmGenor(
clk,
rst_n,
col_n,
cycle,
duty,
out
);
parameter OUT0 = 1'b0;
parameter CYCLE_WIDTH = 8;
parameter DUTY_WIDTH = CYCLE_WIDTH;
input clk;
input rst_n;
input col_n;
input[CYCLE_WIDTH-1:0] cycle;
input[DUTY_WIDTH -1:0] duty;
output out;
reg out;
wire out0;
reg [CYCLE_WIDTH-1:0] cnt;
wire[CYCLE_WIDTH-1:0] r_cycle;
wire[DUTY_WIDTH -1:0] r_duty;
assign r_cycle = cycle - 1'b1;
assign r_duty = duty - 1'b1;
assign out0 = col_n ? OUT0 : (~OUT0);
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
cnt <= 1'b0;
out <= out0;
end else begin
cnt <= cnt + 1'b1;
if(cnt == r_duty) begin
out <= ~out0;
end else if(cnt == r_cycle) begin
out <= out0;
cnt <= 1'b0;
end
end
end
endmodule