-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwm.v
80 lines (66 loc) · 1.52 KB
/
pwm.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module pwm (
input wire clk,
input wire [12:0] in,
output wire out,
output wire dir_o,
output wire escon_enable,
input wire rst);
wire [10:0] local_input;
wire heart_Beat;
reg [10:0] clk_count;
reg out_t;
assign local_input = in[10:0];
assign dir_o = in[11];
assign heartBeat = in[12];
// assign out = out_t & output_control;
assign out = out_t;
assign escon_enable = output_control;
// clock divider
localparam divide_constant = 100;
reg[32:0] divider_clk_counter;
reg clk_slower;
always@(posedge clk or posedge rst) begin
if(rst) begin
divider_clk_counter <= 0;
clk_slower <= 0;
end
else if(clk_count == divide_constant - 1)begin
divider_clk_counter <= 0;
clk_slower = ~clk_slower;
end
else
divider_clk_counter = divider_clk_counter + 1'b1;
end
always @(posedge clk)
clk_count <= clk_count+1'b1;
// heartbeat detector
reg previous_HB;
reg output_control; //negedge reset signal for pwm generation
reg[20:0] HB_counter;
always@(posedge clk) begin
if(heartBeat != previous_HB) begin
output_control <= 1;
HB_counter <= 0;
previous_HB <= heartBeat;
end
else
if(HB_counter > 4000000)
output_control <= 0;
else begin
HB_counter <= HB_counter + 1'b1;
end
end
// pwm generation
reg[10:0] pwm_counter;
always @(posedge clk) begin
if(rst || !output_control) begin
out_t <= 0;
pwm_counter <= 0;
end
else if(pwm_counter>local_input)
out_t <=0;
else
out_t <=1;
pwm_counter <= pwm_counter + 1;
end
endmodule