OpenCores
URL https://opencores.org/ocsvn/irig_regenerator/irig_regenerator/trunk

Subversion Repositories irig_regenerator

[/] [irig_regenerator/] [trunk/] [rtl/] [pwm_pack.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jclaytons
--------------------------------------------------------------------------
2
-- Package of Pulse Width Modulation (PWM) Components
3
--
4
--
5
 
6
library ieee;
7
use ieee.std_logic_1164.all;
8
use ieee.numeric_std.all;
9
 
10
package pwm_pack is
11
 
12
component pwm_simple
13
  generic (
14
    PERIOD     : natural := 800; -- Number of sys_clk_en per PWM cycle
15
    COUNT_BITS : natural := 10   -- LOG2(PERIOD)
16
  );
17
  port (
18
    -- Reset, System Clock and Clock Enable
19
    sys_rst_n  : in  std_logic;
20
    sys_clk    : in  std_logic;
21
    sys_clk_en : in  std_logic;
22
 
23
    -- Input Data
24
    dat_i      : in  signed((COUNT_BITS-1) downto 0);
25
 
26
    -- Output Signal
27
    dat_o      : out std_logic
28
  );
29
end component;
30
 
31
component pwm_unsigned
32
  generic (
33
    PERIOD     : natural := 800; -- Number of sys_clk_en per PWM cycle
34
    COUNT_BITS : natural := 10   -- LOG2(PERIOD)
35
  );
36
  port (
37
    -- Reset, System Clock and Clock Enable
38
    sys_rst_n  : in  std_logic;
39
    sys_clk    : in  std_logic;
40
    sys_clk_en : in  std_logic;
41
 
42
    -- Input Data
43
    dat_i      : in  unsigned((COUNT_BITS-1) downto 0);
44
 
45
    -- Output Signal
46
    dat_o      : out std_logic
47
  );
48
end component;
49
 
50
end pwm_pack;
51
 
52
package body pwm_pack is
53
end pwm_pack;
54
 
55
-------------------------------------------------------------------------------
56
-- Simple PWM unit
57
-------------------------------------------------------------------------------
58
--
59
-- Author: John Clayton
60
-- Update: Oct. 14, 2013 Added this header.  Created description and code.
61
--
62
-- Description
63
-------------------------------------------------------------------------------
64
-- This is a quite simple pulse width modulator (PWM) unit.  It contains a
65
-- counter which increments at the sys_clk_en rate.  When the counter value
66
-- exceeds the value of the dat_i input, the output is driven high.  When
67
-- the counter reaches its terminal value, the counter resets and the output
68
-- is driven low once again.
69
--
70
-- In order to use a signed input with an unsigned up-counter, the dat_i
71
-- input is first converted into an unsigned quantity.  This is done by
72
-- adding 2**(COUNT_BITS-1) to the input.
73
--
74
 
75
library ieee;
76
use ieee.std_logic_1164.all;
77
use ieee.numeric_std.all;
78
 
79
entity pwm_simple is
80
  generic (
81
    PERIOD     : natural := 800; -- Number of sys_clk_en per PWM cycle
82
    COUNT_BITS : natural := 10   -- LOG2(PERIOD)
83
  );
84
  port (
85
    -- Reset, System Clock and Clock Enable
86
    sys_rst_n  : in  std_logic;
87
    sys_clk    : in  std_logic;
88
    sys_clk_en : in  std_logic;
89
 
90
    -- Input Data
91
    dat_i      : in  signed((COUNT_BITS-1) downto 0);
92
 
93
    -- Output Signal
94
    dat_o      : out std_logic
95
  );
96
 
97
end pwm_simple;
98
 
99
architecture beh1 of pwm_simple is
100
 
101
-- Constants
102
  constant adjustment : unsigned(COUNT_BITS-1 downto 0) := to_unsigned(2**(COUNT_BITS-1),COUNT_BITS);
103
 
104
-- Signals
105
  signal count        : unsigned(COUNT_BITS-1 downto 0);
106
  signal dac_val      : unsigned(COUNT_BITS-1 downto 0);
107
 
108
-----------------------------------------------------------------------------
109
begin  -- beh1
110
 
111
dac_val <= adjustment + unsigned(dat_i);
112
 
113
  process (sys_clk, sys_rst_n)
114
  begin  -- process
115
    if sys_rst_n='0' then -- asynchronous reset (active low)
116
      count <= to_unsigned(1,count'length);
117
      dat_o <= '0';
118
    elsif sys_clk'event and sys_clk='1' then -- rising clock edge
119
      if (sys_clk_en='1') then
120
        count <= count+1;
121
        if (count=PERIOD) then
122
          count <= to_unsigned(1,count'length);
123
        end if;
124
        if (count>=dac_val) then
125
          dat_o <= '1';
126
        else
127
          dat_o <= '0';
128
        end if;
129
      end if;
130
    end if;
131
  end process;
132
 
133
 
134
end beh1;
135
 
136
-------------------------------------------------------------------------------
137
-- Simple PWM unit, unsigned version
138
-------------------------------------------------------------------------------
139
--
140
-- Author: John Clayton
141
-- Update: Oct. 14, 2013 Copied pwm_simple, and modified it slightly.
142
--
143
-- Description
144
-------------------------------------------------------------------------------
145
-- This is a quite simple pulse width modulator (PWM) unit.  It contains a
146
-- counter which increments at the sys_clk_en rate.  When the counter value
147
-- exceeds the value of the dat_i input, the output is driven high.  When
148
-- the counter reaches its terminal value, the counter resets and the output
149
-- is driven low once again.
150
--
151
 
152
library ieee;
153
use ieee.std_logic_1164.all;
154
use ieee.numeric_std.all;
155
 
156
entity pwm_unsigned is
157
  generic (
158
    PERIOD     : natural := 800; -- Number of sys_clk_en per PWM cycle
159
    COUNT_BITS : natural := 10   -- LOG2(PERIOD)
160
  );
161
  port (
162
    -- Reset, System Clock and Clock Enable
163
    sys_rst_n  : in  std_logic;
164
    sys_clk    : in  std_logic;
165
    sys_clk_en : in  std_logic;
166
 
167
    -- Input Data
168
    dat_i      : in  unsigned(COUNT_BITS-1 downto 0);
169
 
170
    -- Output Signal
171
    dat_o      : out std_logic
172
  );
173
 
174
end pwm_unsigned;
175
 
176
architecture beh1 of pwm_unsigned is
177
 
178
-- Constants
179
 
180
-- Signals
181
  signal count : unsigned(COUNT_BITS-1 downto 0);
182
 
183
-----------------------------------------------------------------------------
184
begin  -- beh1
185
 
186
  process (sys_clk, sys_rst_n)
187
  begin  -- process
188
    if sys_rst_n='0' then -- asynchronous reset (active low)
189
      count <= to_unsigned(0,count'length);
190
      dat_o <= '0';
191
    elsif sys_clk'event and sys_clk='1' then -- rising clock edge
192
      if (sys_clk_en='1') then
193
        count <= count+1;
194
        if (count=PERIOD-1) then
195
          count <= to_unsigned(1,count'length);
196
        end if;
197
        if (count>=dat_i) then
198
          dat_o <= '1';
199
        else
200
          dat_o <= '0';
201
        end if;
202
      end if;
203
    end if;
204
  end process;
205
 
206
 
207
end beh1;
208
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.