-
Notifications
You must be signed in to change notification settings - Fork 0
/
sounds.vhd
37 lines (33 loc) · 1.06 KB
/
sounds.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
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity sounds is
port(
clk, not_reset: in std_logic;
enable: in std_logic;
period: in std_logic_vector(18 downto 0);
volume: in std_logic_vector(2 downto 0);
buzzer: out std_logic
);
end sounds;
architecture generator of sounds is
signal counter, counter_next: std_logic_vector(18 downto 0);
signal pulse_width: std_logic_vector(18 downto 0);
begin
process(clk, not_reset)
begin
if not_reset = '0' then
counter <= (others => '0');
elsif clk'event and clk = '0' then
counter <= counter_next;
end if;
end process;
-- duty cycle:
-- max: 50% (18 downto 1)
-- min: 0.78% (18 downto 7)
-- off when given 0 (18 downto 0)!
pulse_width <= period(18 downto conv_integer(volume));
counter_next <= (others => '0') when counter = period else
counter + 1;
buzzer <= '1' when (enable = '1' and counter < pulse_width) else '0';
end generator;