-
Notifications
You must be signed in to change notification settings - Fork 1
/
DigitalClockGenerator.vhd
290 lines (260 loc) · 8.45 KB
/
DigitalClockGenerator.vhd
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
--License:
-- DigitalClockWithFPGA is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- DigitalClockWithFPGA is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with DigitalClockWithFPGA. If not, see <http://www.gnu.org/licenses/>.
--
--Description:
-- This software can ported to any FPGA board to display two adjustable digital
-- clocks with adjustable colors. The code was tested on Xilinx Spartan 3E and
-- Altera DE0 boards.
--
--Author:
-- Mustafa Ozcelikors
-- thewebblog.net
-- github.com/mozcelikors
-- <[email protected]>
-- Metin Kundakcioglu
-- <[email protected]>
--
--Note:
-- This code may contain Turkish commenting.
library ieee;
use ieee.std_logic_1164.all;
use IEEE.NUMERIC_STD.ALL;
entity DigitalClockGenerator is
Port (
Clock : in std_logic;
Reset : in std_logic;
AdjustHourButton : in std_logic;
AdjustMinuteButton : in std_logic;
RotaryCCWOut : in std_logic;
RotaryCWOut : in std_logic;
DigitH1 : out std_logic_vector(3 downto 0);
DigitH2 : out std_logic_vector(3 downto 0);
DigitM1 : out std_logic_vector(3 downto 0);
DigitM2 : out std_logic_vector(3 downto 0);
DigitS1 : out std_logic_vector(3 downto 0);
DigitS2 : out std_logic_vector(3 downto 0)
);
end entity DigitalClockGenerator;
architecture DigitalClockGenerator of DigitalClockGenerator is
--Hesaplama ve comparison icin asagidaki sinyalleri kullanacagiz
signal second : integer range 0 to 59 := 0;
signal minute : integer range 0 to 59 := 0;
signal hour : integer range 0 to 23 := 0;
--Digitleri ayri ayri gostermek icin asagidaki sinyalleri kullanacagiz
signal seconddigit1 : integer range 0 to 5 := 0;
signal seconddigit2 : integer range 0 to 9 := 0;
signal minutedigit1 : integer range 0 to 5 := 0;
signal minutedigit2 : integer range 0 to 9 := 0;
signal hourdigit1 : integer range 0 to 2 := 0;
signal hourdigit2 : integer range 0 to 9 := 0;
signal clock_counter : integer range 0 to 50000000 := 0;
signal adjusthour_button_state : std_logic := '0'; --state reg (0: Idle, 1: Adjust)
signal adjustminute_button_state : std_logic := '0'; --state reg (0: Idle, 1: Adjust)
signal AdjustHourButtonOld: std_logic := '0';--buton sample reg
signal AdjustMinuteButtonOld: std_logic := '0';--buton sample reg
signal first_time_flag : std_logic := '1';
signal DigitM1_buf : integer;
signal DigitM2_buf : integer;
signal DigitS1_buf : integer;
signal DigitS2_buf : integer;
signal DigitH1_buf : integer;
signal DigitH2_buf : integer;
begin
--Adjust hour butonunun stateini bir registerda tutalim
process(Clock)is begin
if(rising_edge(Clock))then
if(first_time_flag='1')then
adjusthour_button_state <= '0';
end if;
AdjustHourButtonOld <= AdjustHourButton;
if(AdjustHourButtonOld='0' and AdjustHourButton='1')then --AdjustHourButton rising edge
if(adjusthour_button_state ='1')then
adjusthour_button_state <= '0'; --State: Idle(saat calisiyor, ayarlama yok)
elsif(adjusthour_button_state ='0')then
adjusthour_button_state <= '1'; --State: AdjustHour
end if;
end if;
end if;
end process;
--Adjust minute butonunun stateini bir registerda tutalim
process(Clock)is begin
if(rising_edge(Clock))then
if(first_time_flag='1')then
adjustminute_button_state <= '0';
end if;
AdjustMinuteButtonOld <= AdjustMinuteButton;
if(AdjustMinuteButtonOld='0' and AdjustMinuteButton='1')then --AdjustHourButton rising edge
if(adjustminute_button_state='1')then
adjustminute_button_state <= '0'; --State: Idle(saat calisiyor, ayarlama yok)
elsif(adjustminute_button_state='0')then
adjustminute_button_state <= '1'; --State: AdjustHour
end if;
end if;
end if;
end process;
process(Clock) is begin
if(rising_edge(Clock))then
---ADJUST-START-------------------------------------------------------------------????????????????
if(adjustminute_button_state='1')then
if(RotaryCCWOut='1' and RotaryCWOut='0')then
if(minute<59)then
minute <= minute + 1;
if(minutedigit2 = 9)then
minutedigit2 <= 0;
minutedigit1 <= minutedigit1 + 1;
else
minutedigit2 <= minutedigit2 + 1;
end if;
else
minute <= 0;
minutedigit1 <= 0;
minutedigit2 <= 0;
end if;
elsif(RotaryCCWOut='0' and RotaryCWOut='1')then
if(minute>0)then
minute <= minute - 1;
if(minutedigit2 = 0)then
minutedigit2 <= 9;
minutedigit1 <= minutedigit1 - 1;
else
minutedigit2 <= minutedigit2 - 1;
end if;
else
minute <= 0;
minutedigit1 <= 0;
minutedigit2 <= 0;
end if;
end if;
end if;
if(adjusthour_button_state='1')then
if(RotaryCCWOut='1' and RotaryCWOut='0')then
if(hour<23)then
hour <= hour + 1;
if(hourdigit2 = 9)then
hourdigit2 <= 0;
hourdigit1 <= hourdigit1 + 1;
else
hourdigit2 <= hourdigit2 + 1;
end if;
else
hour <= 0;
hourdigit1 <= 0;
hourdigit2 <= 0;
end if;
elsif(RotaryCCWOut='0' and RotaryCWOut='1')then
if(hour>0)then
hour <= hour - 1;
if(hourdigit2 = 0)then
hourdigit2 <= 9;
hourdigit1 <= hourdigit1 - 1;
else
hourdigit2 <= hourdigit2 - 1;
end if;
else
hour <= 0;
hourdigit1 <= 0;
hourdigit2 <= 0;
end if;
end if;
end if;
---ADJUST-FINISH------------------------------------------------------------------
if(Reset='0')then
second <= 0;
seconddigit1 <= 0;
seconddigit2 <= 0;
minute <= 0;
minutedigit1 <= 0;
minutedigit2 <= 0;
hour <= 0;
hourdigit1 <= 0;
hourdigit2 <= 0;
clock_counter <= 0;
elsif(clock_counter = 50000000)then
if(first_time_flag='1')then
second <= 0;
seconddigit1 <= 0;
seconddigit2 <= 0;
minute <= 0;
minutedigit1 <= 0;
minutedigit2 <= 0;
hour <= 0;
hourdigit1 <= 0;
hourdigit2 <= 0;
first_time_flag <= '0';
end if;
-- Asil saat isleri
if(second < 59) then
second <= second + 1;
if(seconddigit2 = 9)then
seconddigit2 <= 0;
seconddigit1 <= seconddigit1 + 1;
else
seconddigit2 <= seconddigit2 + 1;
end if;
else
second <= 0;
seconddigit1 <= 0;
seconddigit2 <= 0;
if(minute < 59) then
minute <= minute + 1;
if(minutedigit2 = 9)then
minutedigit2 <= 0;
minutedigit1 <= minutedigit1 + 1;
else
minutedigit2 <= minutedigit2 + 1;
end if;
else
minute <= 0;
minutedigit1 <= 0;
minutedigit2 <= 0;
if (hour < 23) then
hour <= hour + 1;
if(hourdigit2 = 9)then
hourdigit2 <= 0;
hourdigit1 <= hourdigit1 + 1;
else
hourdigit2 <= hourdigit2 + 1;
end if;
else
hour <= 0;
hourdigit1 <= 0;
hourdigit2 <= 0;
end if;
end if;
end if;
clock_counter <= 0;
else
clock_counter <= clock_counter + 1;
end if;
end if;
end process;
--Saat Ciktisi
DigitH1_buf <= hourdigit1;--hour / 10;
DigitH2_buf <= hourdigit2;--hour mod 10;
--Dakika Ciktisi
DigitM1_buf <= minutedigit1;--minute / 10;
DigitM2_buf <= minutedigit2;--minute mod 10;
--Saniye Ciktisi
DigitS1_buf <= seconddigit1;--second / 10;
DigitS2_buf <= seconddigit2;--second mod 10;
--Saat Ciktisi
DigitH1 <= std_logic_vector(to_unsigned(DigitH1_buf,DigitH1'length));
DigitH2 <= std_logic_vector(to_unsigned(DigitH2_buf,DigitH2'length));
--Dakika Ciktisi
DigitM1 <= std_logic_vector(to_unsigned(DigitM1_buf,DigitM1'length));
DigitM2 <= std_logic_vector(to_unsigned(DigitM2_buf,DigitM2'length));
--Saniye Ciktisi
DigitS1 <= std_logic_vector(to_unsigned(DigitS1_buf,DigitS1'length));
DigitS2 <= std_logic_vector(to_unsigned(DigitS2_buf,DigitS2'length));
end architecture DigitalClockGenerator;