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

Subversion Repositories pwm_with_dithering

[/] [pwm_with_dithering/] [trunk/] [src/] [pwm_pipelined.vhd] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 TeroS
----------------------------------------------------------------------------------
2
-- Company:              Aboa Space Research Oy (ASRO)
3
-- Engineer:             Tero Säntti
4
-- 
5
-- Create Date:    15:26:48 01/27/2021 
6
-- Design Name:          PWM
7
-- Module Name:    pwm_pipelined - Behavioral 
8
-- Target Devices: None / non-specific
9
-- Tool versions:  None / non-specific
10
-- Description:          Dithered PWM with pipelining to increase maximum clock 
11
--                                               frequency. Somewhat larger and added latency during input
12
--                                               changes. Not sensitive to input changes during operation.
13
--
14
-- Revision: 
15
-- Revision 0.01 - File Created
16
-- Additional Comments: 
17
--
18
----------------------------------------------------------------------------------
19
library IEEE;
20
use IEEE.STD_LOGIC_1164.ALL;
21
use IEEE.NUMERIC_STD.ALL;
22
use IEEE.STD_LOGIC_UNSIGNED.ALL;
23
 
24
entity pwm_pipelined is
25
    Generic (
26
                                bits: integer:=16;
27
                                dithering:integer:=5
28
                                );
29
    Port ( clk : in  STD_LOGIC;
30
           set : in  STD_LOGIC_VECTOR(bits-1 downto 0);
31
           o   : out  STD_LOGIC);
32
end pwm_pipelined;
33
 
34
architecture Behavioral of pwm_pipelined is
35
signal o_i:std_logic:='0';
36
signal cnt:std_logic_vector(bits-1 downto 0):=(others => '0');
37
signal zeros:std_logic_vector((bits-dithering)-1 downto 0):=(others => '0');
38
signal ones:std_logic_vector((bits-dithering)-1 downto 0):=(others => '1');
39
signal target:std_logic_vector((bits-dithering) downto 0);
40
signal target_i:std_logic_vector((bits-dithering) downto 0);
41
signal reversed_cnt_top:std_logic_vector((dithering)-1 downto 0);
42
signal inc:std_logic;
43
signal trigger:std_logic;
44
 
45
function reverse_and_rebase_bit_order(a: in std_logic_vector)
46
return std_logic_vector is
47
  variable result: std_logic_vector(a'high-a'low downto 0);
48
begin
49
  for i in a'RANGE loop
50
    result(a'high-i) := a(i);
51
  end loop;
52
  return result;
53
end;
54
 
55
begin
56
 
57
-- output mapping:
58
o <= o_i;
59
 
60
normal: if dithering = 0  generate
61
 
62
zeros <= (others => '0');
63
ones  <= (others => '1');
64
 
65
doit:process(clk)
66
begin
67
if rising_edge(clk) then
68
        cnt <= cnt + 1;
69
        o_i <= o_i;
70
        trigger <= '0';
71
        if trigger = '1' then o_i <= '1'; end if;
72
        if '0' & cnt=target then o_i <= '0'; end if;
73
        if cnt(bits-1 downto 0) = ones then target <= '0' & set; trigger <= '1'; end if;
74
end if;
75
end process;
76
end generate;
77
 
78
dithered: if dithering > 0  generate
79
 
80
reversed_cnt_top <= reverse_and_rebase_bit_order(cnt(bits-1 downto bits-dithering));
81
 
82
pipelining:process(clk)
83
begin
84
if rising_edge(clk) then
85
        if (reversed_cnt_top < set(dithering-1 downto 0)) then inc <= '1'; else inc <= '0'; end if;
86
        target_i <= ('0' & set(bits-1 downto dithering)) + inc;
87
end if;
88
end process;
89
 
90
doit:process(clk)
91
begin
92
if rising_edge(clk) then
93
        cnt <= cnt + 1;
94
        o_i <= o_i;
95
        trigger <= '0';
96
        if trigger = '1' then o_i <= '1'; end if;
97
        if ('0' & cnt((bits-dithering)-1 downto 0)) = target then o_i <= '0'; end if;
98
        if cnt((bits-dithering)-1 downto 0) = ones then target <= target_i; trigger <= '1'; end if;
99
end if;
100
end process;
101
end generate;
102
 
103
end Behavioral;
104
 

powered by: WebSVN 2.1.0

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