forked from ryancor/FPGA-ClkDivider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_up.v
80 lines (69 loc) · 1.64 KB
/
counter_up.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
`timescale 1ns / 1ps
////////////////////////
///
////////////////////////
module counter_up(
input clk,
output reg [3:0] led,
input press,
output sound
);
// For LEDs
reg slow_clk;
reg [22:0] counter;
reg [3:0] next_led;
// For Buzzer
reg [21:0] tone; // if you wanted to assign tone to one press
// you would wire it instead declare as reg
wire [3:0] time_stamp;
wire [3:0] b_leds = led;
assign b_leds = time_stamp;
always @* begin
case(time_stamp)
4'd0: tone = press ? 22'd000000 : 22'd125000;
4'd1: tone = press ? 22'd000000 : 22'd75000;
4'd2: tone = press ? 22'd000000 : 22'd50000;
4'd3: tone = press ? 22'd000000 : 22'd30000;
4'd4: tone = press ? 22'd000000 : 22'd125000;
4'd5: tone = press ? 22'd000000 : 22'd75000;
4'd6: tone = press ? 22'd000000 : 22'd50000;
4'd7: tone = press ? 22'd000000 : 22'd30000;
default: tone = press ? 22'd000000 : 22'd125000;
endcase
end
tone_generator clk_wiz_0_inst(
.clk(clk),
.counter(tone),
.sound(sound)
);
clock_divider clk_wiz_0_inst_time(
.clk(clk),
.counter(time_stamp)
);
initial begin
next_led = 4'b0000;
led = 4'b0000;
slow_clk = 1'b0;
counter = 0;
end
always @ (posedge clk) // 27Mhz
begin
counter = counter + 1;
if (counter == 0)
begin
slow_clk = ~slow_clk; // 1Hz
end
end
always @ (posedge slow_clk)
begin
next_led <= led + 1;
if (next_led > 4'b1111) // if all LEDs are on
begin
next_led = 4'b0000; // reset LED
end
end
always @ (next_led)
begin
led = next_led;
end
endmodule