2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
6 --------------------------------------------------------------------------------
7 -- This entity is a generator of PWM signal.
9 -- PWM bit width is configurable by PWM_WIDTH generic parameter.
11 -- In practice, to generate PWM signal component must be fed by a counter vector
12 -- (pwm_cnt input) of appropriate width and also period! (One counter can be
13 -- used by several PWM generators and generated signals are then synchronized.)
15 -- The period of the input counter vector should be by 1 smaller than the count
16 -- of all possible PWM values, i.e. counter vector should never reaches the
17 -- maximum possible PWM value, and counting should start from 0. E.g. when full
18 -- range of PWM values is used, binary counter should be reset just one step
19 -- before it overflows. In such a case, when PWM value is maximal, output keeps
22 -- PWM value is buffered and any change is propageted to the output by the next
23 -- pwm period - 'pwm_cyc' must by feed.
24 --------------------------------------------------------------------------------
32 din : in std_logic_vector (PWM_WIDTH-1 downto 0);
36 pwm_cnt : in std_logic_vector (PWM_WIDTH-1 downto 0);
37 pwm_cyc : in std_logic; -- Indicate new pwm period
41 --------------------------------------------------------------------------------
43 architecture behavioral of pwm is
45 -- Register accessible from bus
46 signal reg : std_logic_vector (PWM_WIDTH-1 downto 0) := (others => '0');
47 -- Compare value during pwm cycle, loaded from 'reg' when new period begins.
48 signal cmp : std_logic_vector (PWM_WIDTH-1 downto 0) := (others => '0');
50 --------------------------------------------------------------------------------
54 -- Peripheral register
55 PWM_REGISTER : process (clk, reset)
57 if rising_edge(clk) then
59 reg <= (others => '0');
61 if we = '1' and sel = '1' then
69 -- Generation of PWM signal
70 -- When 'pwm_cyc' is high then new 'cmp' is loaded and 'counter' is reset
71 -- with next clk edge. Pwm output is delayed by one clock.
72 PWM_GEN : process (clk, reset)
74 if rising_edge(clk) then