-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSM.v
More file actions
76 lines (71 loc) · 1.51 KB
/
Copy pathFSM.v
File metadata and controls
76 lines (71 loc) · 1.51 KB
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
module FSM(
input CLOCK_50,
input wire [11:0] distance,
output reg [6:0] sound_volume
);
reg [2:0] sound_state;
// States
parameter distance_idle = 0;
parameter distance_five = 1;
parameter distance_ten = 2;
parameter distance_twenty = 3;
parameter distance_thirty = 4;
parameter distance_over_thirty = 5;
always @(sound_state) begin
case (sound_state)
distance_idle: begin
sound_volume <= 0;
end
distance_five: begin
sound_volume <= 100;
end
distance_ten: begin
sound_volume <= 75;
end
distance_twenty: begin
sound_volume <= 50;
end
distance_thirty: begin
sound_volume <= 25;
end
distance_over_thirty: begin
sound_volume <= 0;
end
endcase
end
always @(posedge CLOCK_50) begin
case (sound_state)
distance_idle: sound_state <= distance_five;
distance_five: begin
if (distance < 5)
sound_state <= distance_five;
else
sound_state <= distance_ten;
end
distance_ten: begin
if (distance < 10)
sound_state <= distance_ten;
else
sound_state <= distance_twenty;
end
distance_twenty: begin
if (distance < 20)
sound_state <= distance_twenty;
else
sound_state <= distance_thirty;
end
distance_thirty: begin
if (distance < 30)
sound_state <= distance_thirty;
else
sound_state <= distance_over_thirty;
end
distance_over_thirty: begin
if (distance >= 30)
sound_state <= distance_over_thirty;
else
sound_state <= distance_idle;
end
endcase
end
endmodule