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

Subversion Repositories pwm_with_dithering

[/] [pwm_with_dithering/] [trunk/] [src/] [pwm_reg.vhd] - Blame information for rev 3

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:47:02 01/27/2021 
6
-- Design Name:          PWM
7
-- Module Name:    pwm_reg - Behavioral 
8
-- Target Devices: None / non-specific
9
-- Tool versions:  None / non-specific
10
-- Description:          Dithered PWM. Using register to hold target during op. Clock
11
--                                               frequency is not maximal. Not sensitive to input 
12
--                                               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_reg 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_reg;
33
 
34
architecture Behavioral of pwm_reg 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
inc <= '1' when (reversed_cnt_top < set(dithering-1 downto 0)) else '0';
82
target_i <= ('0' & set(bits-1 downto dithering)) + inc;
83
 
84
doit:process(clk)
85
begin
86
if rising_edge(clk) then
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 <= target_i; 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.