1 | `timescale 1ns / 1ps
|
2 | module stopwatch(
|
3 | input clock,
|
4 | input reset,
|
5 | input increment,
|
6 | input start,
|
7 | output [6:0] seg,
|
8 | output dp,
|
9 | output [3:0] an
|
10 | );
|
11 |
|
12 | reg [3:0] reg_d0, reg_d1, reg_d2, reg_d3; //registers that will hold the individual counts
|
13 | reg [22:0] ticker;
|
14 | wire click;
|
15 |
|
16 |
|
17 | //the mod 1kHz clock to generate a tick ever 0.001 second
|
18 |
|
19 | always @ (posedge (clock) or posedge (reset))
|
20 | begin
|
21 | if(reset)
|
22 | begin
|
23 | ticker <= 0;
|
24 | end
|
25 | else
|
26 | begin
|
27 | if (start)
|
28 | begin
|
29 | if(ticker == (100000 - 1)) //if it reaches the desired max value reset it
|
30 | ticker <= 0;
|
31 | else if (increment)
|
32 | ticker <= ticker;
|
33 | else
|
34 | ticker <= ticker + 1;
|
35 | end
|
36 | end
|
37 | end
|
38 |
|
39 | //increment a second everytime rising edge of down button
|
40 | reg [3:0] inc_temp;
|
41 | always @ (posedge (increment))
|
42 | begin
|
43 | if (reg_d3 == 9)
|
44 | inc_temp = 0;
|
45 | else
|
46 | inc_temp = reg_d3 + 1;
|
47 | end
|
48 |
|
49 | assign click = ((ticker == (100000 - 1))?1'b1:1'b0); //click to be assigned high every 0.001 second
|
50 |
|
51 | //update data start from here
|
52 | always @ (posedge (clock) or posedge (reset))
|
53 | begin
|
54 | if(reset)
|
55 | begin
|
56 | reg_d0 <= 0;
|
57 | reg_d1 <= 0;
|
58 | reg_d2 <= 0;
|
59 | reg_d3 <= 0;
|
60 | end
|
61 |
|
62 | else
|
63 | begin
|
64 | if (increment)
|
65 | begin
|
66 | reg_d3 <= inc_temp;
|
67 | reg_d0 <= reg_d0;
|
68 | reg_d1 <= reg_d1;
|
69 | reg_d2 <= reg_d2;
|
70 | end
|
71 | else if (click) //increment at every click
|
72 | begin
|
73 | if(reg_d0 == 9) //xxx9 - 1th milisecond
|
74 | begin
|
75 | reg_d0 <= 0;
|
76 | if (reg_d1 == 9) //xx99 - 10th milisecond
|
77 | begin
|
78 | reg_d1 <= 0;
|
79 | if (reg_d2 == 9) //x999 - 100th milisecond
|
80 | begin
|
81 | reg_d2 <= 0;
|
82 | if(reg_d3 == 9) //9999 - The second digit
|
83 | reg_d3 <= 0;
|
84 | else
|
85 | reg_d3 <= reg_d3 + 1;
|
86 | end
|
87 | else
|
88 | reg_d2 <= reg_d2 + 1;
|
89 | end
|
90 |
|
91 | else
|
92 | reg_d1 <= reg_d1 + 1;
|
93 | end
|
94 |
|
95 | else
|
96 | reg_d0 <= reg_d0 + 1;
|
97 | end
|
98 | else
|
99 | begin
|
100 | reg_d3 <= reg_d3;
|
101 | reg_d0 <= reg_d0;
|
102 | reg_d1 <= reg_d1;
|
103 | reg_d2 <= reg_d2;
|
104 | end
|
105 | end
|
106 |
|
107 | end
|
108 |
|
109 |
|
110 |
|
111 | //Mux for display 4 7segs LEDs
|
112 | localparam N = 18;
|
113 | reg [N-1:0]count;
|
114 | always @ (posedge clock or posedge reset)
|
115 | begin
|
116 | if (reset)
|
117 | count <= 0;
|
118 | else
|
119 | count <= count + 1;
|
120 | end
|
121 |
|
122 | reg [6:0]sseg;
|
123 | reg [3:0]an_temp;
|
124 | reg reg_dp;
|
125 | always @ (*)
|
126 | begin
|
127 | case(count[N-1:N-2])
|
128 | 2'b00 :
|
129 | begin
|
130 | sseg = reg_d0;
|
131 | an_temp = 4'b1110;
|
132 | reg_dp = 1'b1;
|
133 | end
|
134 |
|
135 | 2'b01:
|
136 | begin
|
137 | sseg = reg_d1;
|
138 | an_temp = 4'b1101;
|
139 | reg_dp = 1'b0;
|
140 | end
|
141 |
|
142 | 2'b10:
|
143 | begin
|
144 | sseg = reg_d2;
|
145 | an_temp = 4'b1011;
|
146 | reg_dp = 1'b1;
|
147 | end
|
148 |
|
149 | 2'b11:
|
150 | begin
|
151 | sseg = reg_d3;
|
152 | an_temp = 4'b0111;
|
153 | reg_dp = 1'b0;
|
154 | end
|
155 | endcase
|
156 | end
|
157 | assign an = an_temp;
|
158 |
|
159 | //update the data to display to LEDs
|
160 | reg [6:0] sseg_temp;
|
161 | always @ (*)
|
162 | begin
|
163 | case(sseg)
|
164 | 4'd0 : sseg_temp = 7'b1000000;
|
165 | 4'd1 : sseg_temp = 7'b1111001;
|
166 | 4'd2 : sseg_temp = 7'b0100100;
|
167 | 4'd3 : sseg_temp = 7'b0110000;
|
168 | 4'd4 : sseg_temp = 7'b0011001;
|
169 | 4'd5 : sseg_temp = 7'b0010010;
|
170 | 4'd6 : sseg_temp = 7'b0000010;
|
171 | 4'd7 : sseg_temp = 7'b1111000;
|
172 | 4'd8 : sseg_temp = 7'b0000000;
|
173 | 4'd9 : sseg_temp = 7'b0010000;
|
174 | default : sseg_temp = 7'b0111111; //dash
|
175 | endcase
|
176 | end
|
177 | assign seg = sseg_temp;
|
178 | assign dp = reg_dp;
|
179 | endmodule
|
I'm trying to design a stop watch, but i'm stuck at the increment thing.
The intend is when I press `increment`(a button) the `reg_d3` will
increment by one and hold it state until the button is release. I'm able
to make the clock stop when the button is pressed, but I can't update
the `reg_d3`. I always receive `[Place 30-574] Poor placement for
routing between an IO pin and BUFG`. I don't know why, I use increment
in the clkdivider just find. Please help me. Thanks