| 118 |
118 |
|
| 119 |
119 |
architecture AtomicCounter_a of AtomicCounter is
|
| 120 |
120 |
|
| 121 |
|
function to_bcd ( bin : std_logic_vector(15 downto 0) ) return std_logic_vector is
|
|
121 |
function to_bcd ( bin : std_logic_vector(31 downto 0) ) return std_logic_vector is
|
| 122 |
122 |
variable i : integer:=0;
|
| 123 |
|
variable mybcd : std_logic_vector(19 downto 0) := (others => '0');
|
| 124 |
|
variable bint : std_logic_vector(15 downto 0) := bin;
|
|
123 |
variable mybcd : std_logic_vector(35 downto 0) := (others => '0');
|
|
124 |
variable bint : std_logic_vector(31 downto 0) := bin;
|
| 125 |
125 |
begin
|
| 126 |
|
for i in 0 to 15 loop -- repeating 16 times.
|
| 127 |
|
mybcd(19 downto 1) := mybcd(18 downto 0); --shifting the bits.
|
| 128 |
|
mybcd(0) := bint(15);
|
| 129 |
|
bint(15 downto 1) := bint(14 downto 0);
|
|
126 |
for i in 0 to 31 loop -- repeating 16 times.
|
|
127 |
mybcd(35 downto 1) := mybcd(34 downto 0); --shifting the bits.
|
|
128 |
mybcd(0) := bint(31);
|
|
129 |
bint(31 downto 1) := bint(30 downto 0);
|
| 130 |
130 |
bint(0) :='0';
|
| 131 |
131 |
|
| 132 |
132 |
|
| 133 |
|
if(i < 15 and mybcd(3 downto 0) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
133 |
if(i < 31 and mybcd(3 downto 0) > "0100") then --add 3 if BCD digit is greater than 4.
|
| 134 |
134 |
mybcd(3 downto 0) := std_logic_vector(unsigned(mybcd(3 downto 0)) + 3);
|
| 135 |
135 |
end if;
|
| 136 |
136 |
|
| 137 |
|
if(i < 15 and mybcd(7 downto 4) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
137 |
if(i < 31 and mybcd(7 downto 4) > "0100") then --add 3 if BCD digit is greater than 4.
|
| 138 |
138 |
mybcd(7 downto 4) := std_logic_vector(unsigned(mybcd(7 downto 4)) + 3);
|
| 139 |
139 |
end if;
|
| 140 |
140 |
|
| 141 |
|
if(i < 15 and mybcd(11 downto 8) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
141 |
if(i < 31 and mybcd(11 downto 8) > "0100") then --add 3 if BCD digit is greater than 4.
|
| 142 |
142 |
mybcd(11 downto 8) := std_logic_vector(unsigned(mybcd(11 downto 8)) + 3);
|
| 143 |
143 |
end if;
|
| 144 |
144 |
|
| 145 |
|
if(i < 15 and mybcd(15 downto 12) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
145 |
if(i < 31 and mybcd(15 downto 12) > "0100") then --add 3 if BCD digit is greater than 4.
|
| 146 |
146 |
mybcd(15 downto 12) := std_logic_vector(unsigned(mybcd(15 downto 12)) + 3);
|
| 147 |
147 |
end if;
|
| 148 |
148 |
|
| 149 |
|
if(i < 15 and mybcd(19 downto 16) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
149 |
if(i < 31 and mybcd(19 downto 16) > "0100") then --add 3 if BCD digit is greater than 4.
|
| 150 |
150 |
mybcd(19 downto 16) := std_logic_vector(unsigned(mybcd(19 downto 16)) + 3);
|
| 151 |
151 |
end if;
|
| 152 |
152 |
|
|
153 |
if(i < 31 and mybcd(23 downto 20) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
154 |
mybcd(23 downto 20) := std_logic_vector(unsigned(mybcd(23 downto 20)) + 3);
|
|
155 |
end if;
|
|
156 |
|
|
157 |
if(i < 31 and mybcd(27 downto 24) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
158 |
mybcd(27 downto 24) := std_logic_vector(unsigned(mybcd(27 downto 24)) + 3);
|
|
159 |
end if;
|
|
160 |
|
|
161 |
if(i < 31 and mybcd(31 downto 28) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
162 |
mybcd(31 downto 28) := std_logic_vector(unsigned(mybcd(31 downto 28)) + 3);
|
|
163 |
end if;
|
|
164 |
|
|
165 |
if(i < 31 and mybcd(35 downto 32) > "0100") then --add 3 if BCD digit is greater than 4.
|
|
166 |
mybcd(35 downto 32) := std_logic_vector(unsigned(mybcd(35 downto 32)) + 3);
|
|
167 |
end if;
|
|
168 |
|
| 153 |
169 |
end loop;
|
| 154 |
170 |
|
| 155 |
171 |
return mybcd;
|
| ... | ... | |
| 159 |
175 |
-- Counters
|
| 160 |
176 |
-- ----------------
|
| 161 |
177 |
|
| 162 |
|
signal Counter: unsigned(13 downto 0) := "00000000000000"; -- Main Counter 1 Hz, max. 9.999 kHz (binary)
|
| 163 |
|
signal CounterMaxcount: unsigned(15 downto 0) := "0000000000000000"; -- Main Counter 10 kHz, max. 655.35 MHz (binary)
|
|
178 |
signal Counter: unsigned(31 downto 0) := X"00000000"; -- Main Counter 1 Hz, max. 9.999 kHz (binary)
|
| 164 |
179 |
|
| 165 |
180 |
|
| 166 |
181 |
-- LED Display
|
| 167 |
182 |
-- -----------
|
| 168 |
183 |
|
| 169 |
|
signal NumberPom: std_logic_vector(35 downto 0) := X"000000000"; -- Variable for bin/BCD conversion
|
| 170 |
184 |
signal Number: std_logic_vector(35 downto 0) := X"000000000"; -- LED Display Input
|
| 171 |
185 |
signal Freq: std_logic_vector(31 downto 0) := X"00000000"; -- Measured Frequency
|
| 172 |
186 |
signal MuxCounter: unsigned(31 downto 0) := (others => '0'); -- LED Multiplex - Multiplex Clock Divider
|
| ... | ... | |
| 200 |
214 |
if rising_edge(LO_CLOCK) then
|
| 201 |
215 |
|
| 202 |
216 |
if (State = 3) or (State = 0) then
|
| 203 |
|
if Counter < MAXCOUNT-1 then
|
|
217 |
if DIPSW(7) = '0' then -- Half/Full frequency
|
| 204 |
218 |
Counter <= Counter + 1;
|
| 205 |
219 |
else
|
| 206 |
|
Counter <= (others => '0');
|
| 207 |
|
CounterMaxcount <= CounterMaxcount + 1;
|
|
220 |
Counter <= Counter + 2;
|
| 208 |
221 |
end if;
|
| 209 |
222 |
end if;
|
| 210 |
223 |
if (State = 1) then
|
| 211 |
|
Freq(15 downto 0) <= std_logic_vector("00"&Counter);
|
| 212 |
|
Freq(31 downto 16) <= std_logic_vector(CounterMaxcount);
|
|
224 |
Freq(31 downto 0) <= std_logic_vector(Counter);
|
| 213 |
225 |
end if;
|
| 214 |
226 |
if (State = 2) then
|
| 215 |
|
CounterMaxcount <= (others => '0');
|
| 216 |
227 |
Counter <= (others => '0');
|
| 217 |
228 |
end if;
|
| 218 |
229 |
end if;
|
| ... | ... | |
| 247 |
258 |
process (Decko)
|
| 248 |
259 |
begin
|
| 249 |
260 |
if falling_edge(Decko) then
|
| 250 |
|
if DIPSW(7) = '0' then
|
| 251 |
|
NumberPom(15 downto 0) <= to_bcd(Freq(15 downto 0))(15 downto 0); -- Half frequency
|
| 252 |
|
NumberPom(35 downto 16) <= to_bcd(Freq(31 downto 16))(19 downto 0);
|
| 253 |
|
else
|
| 254 |
|
NumberPom(15 downto 0) <= to_bcd(Freq(14 downto 1)&"0")(15 downto 0); -- Full frequency
|
| 255 |
|
NumberPom(35 downto 16) <= to_bcd(Freq(30 downto 15))(19 downto 0);
|
| 256 |
|
end if;
|
|
261 |
Number(35 downto 0) <= to_bcd(Freq(31 downto 0));
|
| 257 |
262 |
end if;
|
| 258 |
263 |
end process;
|
| 259 |
264 |
|
| 260 |
|
Number(35 downto 0) <= NumberPom(35 downto 0);
|
|
265 |
-- Number(35 downto 0) <= NumberPom(35 downto 0);
|
| 261 |
266 |
|
| 262 |
267 |
LED(7) <= Decko; -- Disply 1PPS pulse on LEDbar
|
| 263 |
268 |
LED(6 downto 4) <= (others => '0');
|