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_small.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:    11:43:23 02/03/2021 
6
-- Design Name:          PWM
7
-- Module Name:    pwm_pipelined_small - 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
--                                               Modified by removing one register (target_i) to give smaller
14
--                                               size with slight speed trade-off.
15
--
16
-- Revision: 
17
-- Revision 0.01 - File Created
18
-- Additional Comments: 
19
--
20
----------------------------------------------------------------------------------
21
library IEEE;
22
use IEEE.STD_LOGIC_1164.ALL;
23
use IEEE.NUMERIC_STD.ALL;
24
use IEEE.STD_LOGIC_UNSIGNED.ALL;
25
 
26
entity pwm_pipelined_small is
27
    Generic (
28
                                bits: integer:=16;
29
                                dithering:integer:=5
30
                                );
31
    Port ( clk : in  STD_LOGIC;
32
           set : in  STD_LOGIC_VECTOR(bits-1 downto 0);
33
           o   : out  STD_LOGIC);
34
end pwm_pipelined_small;
35
 
36
architecture Behavioral of pwm_pipelined_small is
37
signal o_i:std_logic:='0';
38
signal cnt:std_logic_vector(bits-1 downto 0):=(others => '0');
39
signal zeros:std_logic_vector((bits-dithering)-1 downto 0):=(others => '0');
40
signal ones:std_logic_vector((bits-dithering)-1 downto 0):=(others => '1');
41
signal target:std_logic_vector((bits-dithering) downto 0);
42
signal reversed_cnt_top:std_logic_vector((dithering)-1 downto 0);
43
signal inc:std_logic;
44
signal trigger:std_logic;
45
 
46
function reverse_and_rebase_bit_order(a: in std_logic_vector)
47
return std_logic_vector is
48
  variable result: std_logic_vector(a'high-a'low downto 0);
49
begin
50
  for i in a'RANGE loop
51
    result(a'high-i) := a(i);
52
  end loop;
53
  return result;
54
end;
55
 
56
begin
57
 
58
-- output mapping:
59
o <= o_i;
60
 
61
normal: if dithering = 0  generate
62
 
63
zeros <= (others => '0');
64
ones  <= (others => '1');
65
 
66
doit:process(clk)
67
begin
68
if rising_edge(clk) then
69
        cnt <= cnt + 1;
70
        o_i <= o_i;
71
        trigger <= '0';
72
        if trigger = '1' then o_i <= '1'; end if;
73
        if '0' & cnt=target then o_i <= '0'; end if;
74
        if cnt(bits-1 downto 0) = ones then target <= '0' & set; trigger <= '1'; end if;
75
end if;
76
end process;
77
end generate;
78
 
79
dithered: if dithering > 0  generate
80
 
81
reversed_cnt_top <= reverse_and_rebase_bit_order(cnt(bits-1 downto bits-dithering));
82
 
83
doit:process(clk)
84
begin
85
if rising_edge(clk) then
86
        if (reversed_cnt_top < set(dithering-1 downto 0)) then inc <= '1'; else inc <= '0'; end if;
87
        cnt <= cnt + 1;
88
        o_i <= o_i;
89
        trigger <= '0';
90
        if trigger = '1' then o_i <= '1'; end if;
91
        if ('0' & cnt((bits-dithering)-1 downto 0)) = target then o_i <= '0'; end if;
92
        if cnt((bits-dithering)-1 downto 0) = ones then target <= ('0' & set(bits-1 downto dithering)) + inc; trigger <= '1'; end if;
93
end if;
94
end process;
95
end generate;
96
 
97
end Behavioral;
98
 

powered by: WebSVN 2.1.0

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