]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - pwm.vhd
Added pwm_min component.
[fpga/pwm.git] / pwm.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5
6 --------------------------------------------------------------------------------
7 -- This entity is a generator of PWM signal.
8 --
9 -- PWM bit width is configurable by PWM_WIDTH generic parameter.
10 --
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.)
14 --
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
20 -- log. 1.
21 --
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 --------------------------------------------------------------------------------
25
26 entity pwm is
27   generic (
28     PWM_WIDTH : integer);
29   port (
30     clk     : in  std_logic;
31     reset   : in  std_logic;
32     din     : in  std_logic_vector (PWM_WIDTH-1 downto 0);
33     we      : in  std_logic;
34     -- PWM interface
35     pwm_cnt : in  std_logic_vector (PWM_WIDTH-1 downto 0);
36     pwm_cyc : in  std_logic;            -- Indicate new pwm period
37     pwm     : out std_logic);
38 end pwm;
39
40 --------------------------------------------------------------------------------
41
42 architecture behavioral of pwm is
43
44   -- Register accessible from bus
45   signal reg : std_logic_vector (PWM_WIDTH-1 downto 0) := (others => '0');
46   -- Compare value during pwm cycle, loaded from 'reg' when new period begins.
47   signal cmp : std_logic_vector (PWM_WIDTH-1 downto 0) := (others => '0');
48   
49 --------------------------------------------------------------------------------
50
51 begin
52
53   -- Peripheral register
54   PWM_REGISTER : process (clk, reset)
55   begin
56     if rising_edge(clk) then
57       if reset = '1' then
58         reg <= (others => '0');
59       else
60         if we = '1' then
61           reg <= din;
62         end if;
63       end if;
64     end if;
65   end process;
66
67
68   -- Generation of PWM signal
69   -- When 'pwm_cyc' is high then new 'cmp' is loaded and 'counter' is reset 
70   -- with next clk edge. Pwm output is delayed by one clock.
71   PWM_GEN : process (clk, reset)
72   begin
73     if rising_edge(clk) then
74       if reset = '1' then
75         pwm <= '0';
76
77       else  
78         if pwm_cyc = '1' then
79           cmp <= reg;
80         end if;
81         
82         if pwm_cnt < cmp then
83           pwm <= '1';
84         else
85           pwm <= '0';
86         end if;
87       end if;
88     end if;
89   end process;
90
91 end behavioral;
92