-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwm_generator.v
57 lines (42 loc) · 1.45 KB
/
pwm_generator.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
49
50
51
52
53
54
55
56
57
// AstroTinker Bot : Task 1A : PWM Generator
/*
Instructions
-------------------
Students are not allowed to make any changes in the Module declaration.
This file is used to design a module which will scale down the 3.125MHz Clock Frequency to 195.125KHz and perform Pulse Width Modulation on it.
Recommended Quartus Version : 20.1
The submitted project file must be 20.1 compatible as the evaluation will be done on Quartus Prime Lite 20.1.
Warning: The error due to compatibility will not be entertained.
-------------------
*/
//PWM Generator
//Inputs : clk_3125KHz, duty_cycle
//Output : clk_195KHz, pwm_signal
module pwm_generator(
input clk_3125KHz,
input [3:0] duty_cycle,
output reg clk_195KHz, pwm_signal
);
initial begin
clk_195KHz = 0; pwm_signal = 1;
end
//////////////////DO NOT MAKE ANY CHANGES ABOVE THIS LINE//////////////////
reg [2:0] counter = 0; // counts 0 to 7
reg [4:0]pwd_count=0;
always @ (posedge clk_3125KHz) begin
if (!counter) clk_195KHz = ~clk_195KHz; // toggles clock signal
counter = counter + 1'b1; // increment counter // after 7 it resets to 0
end
always @ (posedge clk_3125KHz) begin
pwd_count=pwd_count+1'b1;
if (pwd_count==16)begin
pwd_count=0;
end
if ((pwd_count > 0)&(pwd_count <= duty_cycle)) begin
pwm_signal = 1'b1;
end
else
pwm_signal = 1'b0;
end
//////////////////DO NOT MAKE ANY CHANGES BELOW THIS LINE//////////////////
endmodule