]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - pwm.vhd
Added counter and pwm modules.
[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);
46   -- Compare value during pwm cycle, loaded from 'reg' when new period begins.
47   signal cmp : std_logic_vector (PWM_WIDTH-1 downto 0);
48   
49 --------------------------------------------------------------------------------
50
51 begin
52
53   -- Peripheral register
54   PWM_REGISTER : process (clk, reset)
55   begin
56     if reset = '1' then
57       reg <= (others => '0');
58
59     elsif rising_edge(clk) then
60       if we = '1' then
61         reg <= din;
62       end if;
63     end if;
64   end process;
65
66
67   -- Generation of PWM signal
68   -- When 'pwm_cyc' is high then new 'cmp' is loaded and 'counter' is reset 
69   -- with next clk edge. Pwm output is delayed by one clock.
70   PWM_GEN : process (clk, reset)
71   begin
72     if reset = '1' then
73       pwm <= '0';
74       
75     elsif rising_edge(clk) then
76       if pwm_cyc = '1' then
77         cmp <= reg;
78       end if;
79       
80       if pwm_cnt < cmp then
81         pwm <= '1';
82       else
83         pwm <= '0';
84       end if;
85     end if;
86   end process;
87
88 end behavioral;
89